1 11 12 package org.eclipse.core.databinding.observable.set; 13 14 import java.util.Collection ; 15 import java.util.Iterator ; 16 import java.util.Set ; 17 18 import org.eclipse.core.databinding.observable.AbstractObservable; 19 import org.eclipse.core.databinding.observable.ChangeSupport; 20 import org.eclipse.core.databinding.observable.ObservableTracker; 21 import org.eclipse.core.databinding.observable.Realm; 22 23 35 public abstract class AbstractObservableSet extends AbstractObservable implements 36 IObservableSet { 37 38 private ChangeSupport changeSupport; 39 40 private boolean stale = false; 41 42 protected AbstractObservableSet() { 43 this(Realm.getDefault()); 44 } 45 46 protected void firstListenerAdded() { 47 super.firstListenerAdded(); 48 } 49 50 protected void lastListenerRemoved() { 51 super.lastListenerRemoved(); 52 } 53 54 protected AbstractObservableSet(Realm realm) { 55 super(realm); 56 changeSupport = new ChangeSupport(realm){ 57 protected void firstListenerAdded() { 58 AbstractObservableSet.this.firstListenerAdded(); 59 } 60 protected void lastListenerRemoved() { 61 AbstractObservableSet.this.lastListenerRemoved(); 62 } 63 }; 64 } 65 66 public synchronized void addSetChangeListener(ISetChangeListener listener) { 67 changeSupport.addListener(SetChangeEvent.TYPE, listener); 68 } 69 70 public synchronized void removeSetChangeListener(ISetChangeListener listener) { 71 changeSupport.removeListener(SetChangeEvent.TYPE, listener); 72 } 73 74 protected abstract Set getWrappedSet(); 75 76 protected void fireSetChange(SetDiff diff) { 77 super.fireChange(); 79 80 changeSupport.fireEvent(new SetChangeEvent(this, diff)); 81 } 82 83 public boolean contains(Object o) { 84 getterCalled(); 85 return getWrappedSet().contains(o); 86 } 87 88 public boolean containsAll(Collection c) { 89 getterCalled(); 90 return getWrappedSet().containsAll(c); 91 } 92 93 public boolean equals(Object o) { 94 getterCalled(); 95 return getWrappedSet().equals(o); 96 } 97 98 public int hashCode() { 99 getterCalled(); 100 return getWrappedSet().hashCode(); 101 } 102 103 public boolean isEmpty() { 104 getterCalled(); 105 return getWrappedSet().isEmpty(); 106 } 107 108 public Iterator iterator() { 109 getterCalled(); 110 final Iterator wrappedIterator = getWrappedSet().iterator(); 111 return new Iterator () { 112 113 public void remove() { 114 throw new UnsupportedOperationException (); 115 } 116 117 public boolean hasNext() { 118 ObservableTracker.getterCalled(AbstractObservableSet.this); 119 return wrappedIterator.hasNext(); 120 } 121 122 public Object next() { 123 ObservableTracker.getterCalled(AbstractObservableSet.this); 124 return wrappedIterator.next(); 125 } 126 }; 127 } 128 129 public int size() { 130 getterCalled(); 131 return getWrappedSet().size(); 132 } 133 134 public Object [] toArray() { 135 getterCalled(); 136 return getWrappedSet().toArray(); 137 } 138 139 public Object [] toArray(Object [] a) { 140 getterCalled(); 141 return getWrappedSet().toArray(a); 142 } 143 144 public String toString() { 145 getterCalled(); 146 return getWrappedSet().toString(); 147 } 148 149 protected void getterCalled() { 150 ObservableTracker.getterCalled(this); 151 } 152 153 public boolean add(Object o) { 154 throw new UnsupportedOperationException (); 155 } 156 157 public boolean addAll(Collection c) { 158 throw new UnsupportedOperationException (); 159 } 160 161 public boolean remove(Object o) { 162 throw new UnsupportedOperationException (); 163 } 164 165 public boolean removeAll(Collection c) { 166 throw new UnsupportedOperationException (); 167 } 168 169 public boolean retainAll(Collection c) { 170 throw new UnsupportedOperationException (); 171 } 172 173 public void clear() { 174 throw new UnsupportedOperationException (); 175 } 176 177 180 public boolean isStale() { 181 checkRealm(); 182 return stale; 183 } 184 185 191 public void setStale(boolean stale) { 192 checkRealm(); 193 boolean wasStale = this.stale; 194 this.stale = stale; 195 if (!wasStale && stale) { 196 fireStale(); 197 } 198 } 199 200 201 protected void fireChange() { 202 throw new RuntimeException ("fireChange should not be called, use fireSetChange() instead"); } 204 205 208 public synchronized void dispose() { 209 super.dispose(); 210 changeSupport.dispose(); 211 changeSupport = null; 212 } 213 214 } 215 | Popular Tags |