@Override public boolean removeAll(Collection<?> c) { if (!collectionSupport.hasListeners()) return collection.removeAll(c); boolean changed = false; for (Object o : c) changed = remove(o) || changed; return changed; } @Override
public void clear() { if (!collectionSupport.hasListeners()) { collection.clear(); return; } Collection<T> copy = new ArrayList<T>(collection); collection.clear(); collectionSupport.fireRemoved(this, copy); } public boolean contains(Object o) {
@Override public void clear() { if (collectionSupport != null && collectionSupport.hasListeners()) { try { currentlyCausingChange = true; Collection<V> removedElements = new ArrayList<V>(AdaptedCollection.this); collection.clear(); collectionSupport.fireRemoved(AdaptedCollection.this, removedElements, 0, removedElements.size() - 1); } finally { currentlyCausingChange = false; } } else { collection.clear(); } }
@SuppressWarnings("unchecked") public boolean removeAll(Collection<?> c) { if (!collectionSupport.hasListeners()) return collection.removeAll(c); Collection<T> removedElements = new ArrayList<T>(c.size()); for (Object t : c) { boolean removed = collection.remove(t); if (removed) removedElements.add((T)t); } collectionSupport.fireRemoved(this, removedElements); return !removedElements.isEmpty(); } public boolean retainAll(Collection<?> c) {
@Override public boolean retainAll(Collection<?> c) { if (!collectionSupport.hasListeners()) return collection.retainAll(c); Collection<Map.Entry<K,V>> removedElements = new ArrayList<Map.Entry<K,V>>(c.size()); for (Iterator<Map.Entry<K, V>> it = map.entrySet().iterator(); it.hasNext();) { Map.Entry<K, V> e = it.next(); if (!c.contains(e.getKey())) { it.remove(); removedElements.add(e); } } propagateRemoved(removedElements); return !removedElements.isEmpty(); } @Override
public boolean retainAll(Collection<?> c) { if (!collectionSupport.hasListeners()) return collection.retainAll(c); Collection<T> removedElements = new ArrayList<T>(c.size()); for (Iterator<T> it = iterator(); it.hasNext();) { T e = it.next(); if (!c.contains(e)) { it.remove(); removedElements.add(e); } } collectionSupport.fireRemoved(this, removedElements); return !removedElements.isEmpty(); } public int size() {
@SuppressWarnings("unchecked") protected boolean removeWithoutBackWardAdapter(Object value) { try { currentlyCausingChange = true; if (collection.remove(backwardAdapter.adapt((V)value))) { if (collectionSupport != null && collectionSupport.hasListeners()) collectionSupport.fireRemoved(this, (Collection<V>)Collections.singleton(value)); return true; } return false; } finally { currentlyCausingChange = false; } }
public boolean addAll(Collection<? extends T> c) { if (!collectionSupport.hasListeners()) return collection.addAll(c); int max = c.size(); Collection<T> addedElements = new ArrayList<T>(max), updatedElements = new ArrayList<T>(max); for (T t : c) { (collection.add(t) ? addedElements : updatedElements).add(t); } int firstIndex = collection instanceof List<?> ? size() : -1, lastIndex = firstIndex < 0 ? -1 : firstIndex + addedElements.size() - 1; collectionSupport.fireAdded(this, addedElements, firstIndex, lastIndex); collectionSupport.fireUpdated(this, updatedElements, firstIndex, lastIndex); return !addedElements.isEmpty(); } public void clear() {
@Override public boolean add(V value) { if (backwardAdapter == null) throw new UnsupportedOperationException("No backward adapter in this AdapterCollection"); try { currentlyCausingChange = true; if (collection.add(backwardAdapter.adapt(value))) { if (collectionSupport != null && collectionSupport.hasListeners()) collectionSupport.fireAdded(this, Collections.singleton(value)); return true; } return false; } finally { currentlyCausingChange = false; } }
@SuppressWarnings("unchecked") @Override public boolean remove(Object value) { if (backwardAdapter == null) { // Give index of removed element in collection event when it does not cost more // (simplifies subclassing as List, as for the remove method there is only the need to reimplement removeWithoutBackWardAdapter) int i = 0; for (Iterator<U> it = collection.iterator(); it.hasNext();) { if (forwardAdapter.adapt(it.next()).equals(value)) { try { currentlyCausingChange = true; it.remove(); if (collectionSupport != null && collectionSupport.hasListeners()) collectionSupport.fireRemoved(this, (Collection<V>)Collections.singleton(value), i, i); return true; } finally { currentlyCausingChange = false; } } i++; } return false; } else { return removeWithoutBackWardAdapter(value); } }
public void collectionChanged(CollectionEvent<U> e) { // Do not propagate the event if we triggered it if (currentlyCausingChange) return; // Only propagate if someone is listening (CollectionSupport already tries not to fire anything when there is no listener, but here we are trying to avoid to create the wrapped elements collection) if (collectionSupport == null || !collectionSupport.hasListeners()) return; // Adapt the collection of changed / added / removed elements in the event collectionSupport.fireEvent( AdaptedCollection.this, new AdaptedCollection<U, V>(e.getElements(), AdaptedCollection.this.forwardAdapter, AdaptedCollection.this.backwardAdapter), e.getType(), e.getFirstIndex(), e.getLastIndex()); } });