public void add(int index, T element) { list.add(index, element); collectionSupport.fireAdded(this,Collections.singleton(element), index, index); } public boolean addAll(int index, Collection<? extends T> c) {
void propagateAdded(K key, V value) { if (keySet != null) keySet.collectionSupport.fireAdded(keySet, Collections.singleton(key)); if (entrySet != null) { entrySet.collectionSupport.fireAdded(entrySet, Collections.singleton((Map.Entry<K,V>)new UnmodifiableEntry<K,V>(key, value))); } if (values != null) { valuesSupport.fireAdded(values, Collections.singleton(value)); } } void propagateRemoved(Collection<Map.Entry<K, V>> entries) {
public boolean addAll(int index, Collection<? extends T> c) { int initSize = list.size(); if (!list.addAll(index, c)) { if (list.size() != initSize) throw new UnsupportedOperationException("Does not support listeners-enabled proxying of addAll(int, Collection) methods that are not atomical."); return false; } collectionSupport.fireAdded(this, new ArrayList<T>(c), initSize, initSize + c.size() - 1); return true; } public T get(int index) {
public boolean add(T o) { boolean added = collection.add(o); int index = collection instanceof List<?> ? size() : -1; if (added) { collectionSupport.fireAdded(this,Collections.singleton(o), index, index); } else { collectionSupport.fireUpdated(this,Collections.singleton(o), index, index); } return added; } public boolean addAll(Collection<? extends T> c) {
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; } }
void propagateUpdated(K key, V oldValue, V newValue) { if (keySet != null) keySet.collectionSupport.fireUpdated(keySet, Collections.singleton(key)); boolean different = !oldValue.equals(newValue); if (entrySet != null) { Collection<Map.Entry<K, V>> newEntries = Collections.singleton((Map.Entry<K, V>)new UnmodifiableEntry<K,V>(key, newValue)); if (different) { Collection<Map.Entry<K, V>> oldEntries = Collections.singleton((Map.Entry<K, V>)new UnmodifiableEntry<K,V>(key, oldValue)); entrySet.collectionSupport.fireRemoved(entrySet, oldEntries); entrySet.collectionSupport.fireAdded(entrySet, newEntries); } else { entrySet.collectionSupport.fireUpdated(entrySet, newEntries); } } if (values != null) { if (different) { valuesSupport.fireRemoved(values, Collections.singleton(oldValue)); valuesSupport.fireAdded(values, Collections.singleton(newValue)); } else { valuesSupport.fireUpdated(values, Collections.singleton(newValue)); } } }
@Override public void collectionChanged(CollectionEvent<T> e) { switch(e.getType()){ case ADDED: ls.fireAdded(CompoundListenableCollection.this, e.getElements()); break; case REMOVED: ls.fireRemoved(CompoundListenableCollection.this, e.getElements()); break; case UPDATED: ls.fireUpdated(CompoundListenableCollection.this, e.getElements()); break; default: break; }//end EventTypes }}; private final CollectionListener<ListenableCollection<T>> outerListener = new CollectionListener<ListenableCollection<T>>(){
@Override public void collectionChanged(CollectionEvent<ListenableCollection<T>> e) { switch(e.getType()){ case ADDED: for(ListenableCollection<T> coll:e.getElements()){ delegate.addComponent(coll); coll.addCollectionListener(innerListener); ls.fireAdded(CompoundListenableCollection.this, coll); }//end for(collections added) break; case REMOVED: for(ListenableCollection<T> coll:e.getElements()){ delegate.removeComponent(coll); coll.removeCollectionListener(innerListener); ls.fireRemoved(CompoundListenableCollection.this, coll); }//end for(collections removed) break; case UPDATED: // ???? break; default: break; }//end EventTypes }};