boolean removeEntriesIf(Predicate<? super Entry<K, Collection<V>>> predicate) { Iterator<Entry<K, Collection<V>>> entryIterator = unfiltered.asMap().entrySet().iterator(); boolean changed = false; while (entryIterator.hasNext()) { Entry<K, Collection<V>> entry = entryIterator.next(); K key = entry.getKey(); Collection<V> collection = filterCollection(entry.getValue(), new ValuePredicate(key)); if (!collection.isEmpty() && predicate.apply(Maps.immutableEntry(key, collection))) { if (collection.size() == entry.getValue().size()) { entryIterator.remove(); } else { collection.clear(); } changed = true; } } return changed; }
@CollectionFeature.Require({SUPPORTS_REMOVE, FAILS_FAST_ON_CONCURRENT_MODIFICATION}) @CollectionSize.Require(SEVERAL) public void testClearConcurrentWithIteration() { try { Iterator<E> iterator = collection.iterator(); collection.clear(); iterator.next(); /* * We prefer for iterators to fail immediately on hasNext, but ArrayList * and LinkedList will notably return true on hasNext here! */ fail("Expected ConcurrentModificationException"); } catch (ConcurrentModificationException expected) { // success } } }
public void handleMembershipChange(Collection<Request> requests) { Collection<Address> leaving_mbrs=new LinkedHashSet<>(requests.size()); requests.forEach(r -> { if(r.type == Request.SUSPECT) suspected_mbrs.add(r.mbr); else if(r.type == Request.LEAVE) leaving_mbrs.add(r.mbr); }); if(suspected_mbrs.isEmpty() && leaving_mbrs.isEmpty()) return; if(wouldIBeCoordinator(leaving_mbrs)) { log.debug("%s: members are %s, coord=%s: I'm the new coordinator", gms.local_addr, gms.members, gms.local_addr); gms.becomeCoordinator(); Collection<Request> leavingOrSuspectedMembers=new LinkedHashSet<>(); leaving_mbrs.forEach(mbr -> leavingOrSuspectedMembers.add(new Request(Request.LEAVE, mbr))); suspected_mbrs.forEach(mbr -> { leavingOrSuspectedMembers.add(new Request(Request.SUSPECT, mbr)); gms.ack_collector.suspect(mbr); }); suspected_mbrs.clear(); gms.getViewHandler().add(leavingOrSuspectedMembers); } }
/** * Clear the map. * <p> * This clears each collection in the map, and so may be slow. */ public void clear() { // For gc, clear each list in the map Set pairs = super.entrySet(); Iterator pairsIterator = pairs.iterator(); while (pairsIterator.hasNext()) { Map.Entry keyValuePair = (Map.Entry) pairsIterator.next(); Collection coll = (Collection) keyValuePair.getValue(); coll.clear(); } super.clear(); }
public void setItems(Collection<BaseDanmaku> items) { if (mDuplicateMergingEnabled && mSortType != ST_BY_LIST) { synchronized (this.mLockObject) { this.items.clear(); this.items.addAll(items); items = this.items; } } else { this.items = items; } if (items instanceof List) { mSortType = ST_BY_LIST; } mSize.set(items == null ? 0 : items.size()); }
/** * Tests {@link Set#equals(Object)}. */ public void testSetEquals() { resetEmpty(); assertEquals("Empty sets should be equal", getSet(), getConfirmedSet()); verify(); Collection set2 = makeConfirmedCollection(); set2.add("foo"); assertTrue("Empty set shouldn't equal nonempty set", !getSet().equals(set2)); resetFull(); assertEquals("Full sets should be equal", getSet(), getConfirmedSet()); verify(); set2.clear(); set2.addAll(Arrays.asList(getOtherElements())); assertTrue("Sets with different contents shouldn't be equal", !getSet().equals(set2)); }
/** * {@inheritDoc} * * <p>The returned collection is immutable. */ @Override public Collection<V> replaceValues(@Nullable K key, Iterable<? extends V> values) { Iterator<? extends V> iterator = values.iterator(); if (!iterator.hasNext()) { return removeAll(key); } // TODO(lowasser): investigate atomic failure? Collection<V> collection = getOrCreateCollection(key); Collection<V> oldValues = createCollection(); oldValues.addAll(collection); totalSize -= collection.size(); collection.clear(); while (iterator.hasNext()) { if (collection.add(iterator.next())) { totalSize++; } } return unmodifiableCollectionSubclass(oldValues); }
Collection<V> values = multimap.asMap().entrySet().iterator().next().getValue(); K key = multimap.keySet().iterator().next(); assertCollectionIsUnmodifiable(multimap.get(key), sampleValue); assertMultimapRemainsUnmodified(multimap, originalEntries); K presentKey = multimap.keySet().iterator().next(); try { multimap.asMap().get(presentKey).remove(sampleValue); multimap.asMap().values().iterator().next().remove(sampleValue); fail("asMap().values().iterator().next().remove() succeeded on unmodifiable multimap"); } catch (UnsupportedOperationException expected) { ((Collection<?>) multimap.asMap().values().toArray()[0]).clear(); fail("asMap().values().toArray()[0].clear() succeeded on unmodifiable multimap"); } catch (UnsupportedOperationException expected) {
/** * Factory method to create a transforming collection that will transform * existing contents of the specified collection. * <p> * If there are any elements already in the collection being decorated, they * will be transformed by this method. * Contrast this with {@link #transformingCollection(Collection, Transformer)}. * * @param <E> the type of the elements in the collection * @param collection the collection to decorate, must not be null * @param transformer the transformer to use for conversion, must not be null * @return a new transformed Collection * @throws NullPointerException if collection or transformer is null * @since 4.0 */ public static <E> TransformedCollection<E> transformedCollection(final Collection<E> collection, final Transformer<? super E, ? extends E> transformer) { final TransformedCollection<E> decorated = new TransformedCollection<>(collection, transformer); // null collection & transformer are disallowed by the constructor call above if (collection.size() > 0) { @SuppressWarnings("unchecked") // collection is of type E final E[] values = (E[]) collection.toArray(); // NOPMD - false positive for generics collection.clear(); for (final E value : values) { decorated.decorated().add(transformer.transform(value)); } } return decorated; }
/** * Clear the map. * <p> * This clears each collection in the map, and so may be slow. */ public void clear() { // For gc, clear each list in the map Set pairs = super.entrySet(); Iterator pairsIterator = pairs.iterator(); while (pairsIterator.hasNext()) { Map.Entry keyValuePair = (Map.Entry) pairsIterator.next(); Collection coll = (Collection) keyValuePair.getValue(); coll.clear(); } super.clear(); }
/** * {@inheritDoc} * * <p>The returned collection is immutable. */ @Override public Collection<V> removeAll(@Nullable Object key) { Collection<V> collection = map.remove(key); if (collection == null) { return createUnmodifiableEmptyCollection(); } Collection<V> output = createCollection(); output.addAll(collection); totalSize -= collection.size(); collection.clear(); return unmodifiableCollectionSubclass(output); }
boolean removeEntriesIf(Predicate<? super Entry<K, Collection<V>>> predicate) { Iterator<Entry<K, Collection<V>>> entryIterator = unfiltered.asMap().entrySet().iterator(); boolean changed = false; while (entryIterator.hasNext()) { Entry<K, Collection<V>> entry = entryIterator.next(); K key = entry.getKey(); Collection<V> collection = filterCollection(entry.getValue(), new ValuePredicate(key)); if (!collection.isEmpty() && predicate.apply(Maps.immutableEntry(key, collection))) { if (collection.size() == entry.getValue().size()) { entryIterator.remove(); } else { collection.clear(); } changed = true; } } return changed; }