public static <U,V> Collection<V> adapt(Collection<U> col, Adapter<U, V> adapter) { return new AdaptedCollection<U, V>(col, adapter); }
@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); } }
void propagateRemoved(Collection<Map.Entry<K, V>> entries) { if (keySet != null) keySet.collectionSupport.fireRemoved(keySet, new AdaptedCollection<Map.Entry<K,V>, K>(entries, entryToKeyAdapter)); if (entrySet != null) { entrySet.collectionSupport.fireRemoved(entrySet, entries); } if (values != null) { valuesSupport.fireRemoved(values, new AdaptedCollection<Map.Entry<K,V>, V>(entries, entryToValueAdapter)); } }
public void addCollectionListener(CollectionListener<V> l) { if (collectionSupport == null) { collectionSupport = new ListenableSupport<V>(); if (collection instanceof ListenableCollection<?>) { ((ListenableCollection<U>)collection).addCollectionListener(new CollectionListener<U>() { 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()); } }); } } collectionSupport.addCollectionListener(l); }
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()); } });
relevantPositioned = PredicatedCollection.predicatedCollection( new AdaptedCollection<PositionedRenderable,Positionable>(getVisibleWorldObjectList(),Util.bidi2Backward(castingAdapter),Util.bidi2Forward(castingAdapter)), new InstanceofPredicate(PositionedRenderable.class)); setCamera(camera);