Refine search
public void add(Key<?> key, State state, Object source) { if (backingMap == null) { backingMap = Maps.newHashMap(); } // if it's an instanceof Class, it was a JIT binding, which we don't // want to retain. if (source instanceof Class || source == SourceProvider.UNKNOWN_SOURCE) { source = null; } Multiset<Object> sources = backingMap.get(key); if (sources == null) { sources = LinkedHashMultiset.create(); backingMap.put(key, sources); } Object convertedSource = Errors.convert(source); sources.add(convertedSource); // Avoid all the extra work if we can. if (state.parent() != State.NONE) { Set<KeyAndSource> keyAndSources = evictionCache.getIfPresent(state); if (keyAndSources == null) { evictionCache.put(state, keyAndSources = Sets.newHashSet()); } keyAndSources.add(new KeyAndSource(key, convertedSource)); } }
/** * Creates a new, empty {@code LinkedHashMultiset} with the specified expected number of distinct * elements. * * @param distinctElements the expected number of distinct elements * @throws IllegalArgumentException if {@code distinctElements} is negative */ public static <E> LinkedHashMultiset<E> create(int distinctElements) { return new LinkedHashMultiset<E>(distinctElements); }
private LinkedListMultimap() { keyCount = LinkedHashMultiset.create(); keyToKeyHead = Maps.newHashMap(); keyToKeyTail = Maps.newHashMap(); }
private static Map<Object, Object> accumulateMap( @NullableDecl Object k0, @NullableDecl Object v0, Object... rest) { checkArgument( rest.length % 2 == 0, "There must be an equal number of key/value pairs " + "(i.e., the number of key/value parameters (%s) must be even).", rest.length + 2); Map<Object, Object> expectedMap = Maps.newLinkedHashMap(); expectedMap.put(k0, v0); Multiset<Object> keys = LinkedHashMultiset.create(); keys.add(k0); for (int i = 0; i < rest.length; i += 2) { Object key = rest[i]; expectedMap.put(key, rest[i + 1]); keys.add(key); } checkArgument( keys.size() == expectedMap.size(), "Duplicate keys (%s) cannot be passed to containsExactly().", keys); return expectedMap; }
public void testLosesPlaceInLine() throws Exception { Multiset<String> ms = LinkedHashMultiset.create(); ms.add("a"); ms.add("b", 2); ms.add("c"); assertThat(ms.elementSet()).containsExactly("a", "b", "c").inOrder(); ms.remove("b"); assertThat(ms.elementSet()).containsExactly("a", "b", "c").inOrder(); ms.add("b"); assertThat(ms.elementSet()).containsExactly("a", "b", "c").inOrder(); ms.remove("b", 2); ms.add("b"); assertThat(ms.elementSet()).containsExactly("a", "c", "b").inOrder(); } }
private LinkedListMultimap(int expectedKeys) { keyCount = LinkedHashMultiset.create(expectedKeys); keyToKeyHead = Maps.newHashMapWithExpectedSize(expectedKeys); keyToKeyTail = Maps.newHashMapWithExpectedSize(expectedKeys); }
private static <E> ImmutableMultiset<E> copyFromElements(E... elements) { Multiset<E> multiset = LinkedHashMultiset.create(); Collections.addAll(multiset, elements); return copyFromEntries(multiset.entrySet()); }
@Override protected Multiset<String> create(String[] elements) { Multiset<String> multiset1 = LinkedHashMultiset.create(); Multiset<String> multiset2 = LinkedHashMultiset.create(); for (int i = 0; i < elements.length; i++) { String element = elements[i]; if (multiset1.contains(element) || multiset2.contains(element)) { // add to both; the one already containing it will have more multiset1.add(element); multiset2.add(element); } else if (i % 2 == 0) { multiset1.add(elements[i]); } else { multiset2.add(elements[i]); } } return Multisets.union(multiset1, multiset2); } };
@Override protected Multiset<String> create(String[] elements) { Multiset<String> multiset1 = LinkedHashMultiset.create(); Multiset<String> multiset2 = LinkedHashMultiset.create(); multiset1.add("equalIn1"); multiset1.add("fewerIn1"); multiset2.add("equalIn1"); multiset2.add("fewerIn1", 3); multiset2.add("onlyIn2", 2); for (int i = 0; i < elements.length; i++) { // add 1 more copy of each element to multiset1 than multiset2 multiset1.add(elements[i], i + 2); multiset2.add(elements[i], i + 1); } return Multisets.difference(multiset1, multiset2); } };
@Override protected Multiset<String> create(String[] elements) { Multiset<String> multiset1 = LinkedHashMultiset.create(); Multiset<String> multiset2 = LinkedHashMultiset.create(); for (int i = 0; i < elements.length; i++) { // add to either; sum should contain all if (i % 2 == 0) { multiset1.add(elements[i]); } else { multiset2.add(elements[i]); } } return Multisets.sum(multiset1, multiset2); } };
@Override protected Multiset<String> create(String[] elements) { Multiset<String> multiset1 = LinkedHashMultiset.create(); Multiset<String> multiset2 = LinkedHashMultiset.create(); multiset1.add("only1"); multiset2.add("only2"); for (int i = 0; i < elements.length; i++) { multiset1.add(elements[i]); multiset2.add(elements[elements.length - 1 - i]); } if (elements.length > 0) { multiset1.add(elements[0]); } if (elements.length > 1) { /* * When a test requests a multiset with duplicates, our plan of * "add an extra item 0 to A and an extra item 1 to B" really means * "add an extra item 0 to A and B," which isn't what we want. */ if (!Objects.equal(elements[0], elements[1])) { multiset2.add(elements[1], 2); } } return Multisets.intersection(multiset1, multiset2); } };
/** * Returns an immutable multiset containing the given elements, in the "grouped iteration order" * described in the class documentation. * * @throws NullPointerException if any of {@code elements} is null */ public static <E> ImmutableMultiset<E> copyOf(Iterable<? extends E> elements) { if (elements instanceof ImmutableMultiset) { @SuppressWarnings("unchecked") // all supported methods are covariant ImmutableMultiset<E> result = (ImmutableMultiset<E>) elements; if (!result.isPartialView()) { return result; } } Multiset<? extends E> multiset = (elements instanceof Multiset) ? Multisets.cast(elements) : LinkedHashMultiset.create(elements); return copyFromEntries(multiset.entrySet()); }
/** * Creates a new {@code LinkedHashMultiset} containing the specified elements. * * <p>This implementation is highly efficient when {@code elements} is itself a {@link Multiset}. * * @param elements the elements that the multiset should contain */ public static <E> LinkedHashMultiset<E> create(Iterable<? extends E> elements) { LinkedHashMultiset<E> multiset = create(Multisets.inferDistinctElements(elements)); Iterables.addAll(multiset, elements); return multiset; }
private void readObject(ObjectInputStream stream) throws IOException, ClassNotFoundException { stream.defaultReadObject(); int distinctElements = Serialization.readCount(stream); setBackingMap(new LinkedHashMap<E, AtomicInteger>( Maps.capacity(distinctElements))); Serialization.populateMultiset(this, stream, distinctElements); }