1 11 12 package org.eclipse.jface.internal.databinding.provisional.observable.set; 13 14 import java.util.ArrayList ; 15 import java.util.Collection ; 16 import java.util.HashSet ; 17 import java.util.Iterator ; 18 import java.util.Set ; 19 20 import org.eclipse.jface.internal.databinding.provisional.observable.AbstractObservable; 21 import org.eclipse.jface.internal.databinding.provisional.observable.ObservableTracker; 22 23 30 public abstract class ObservableSet extends AbstractObservable implements 31 IObservableSet { 32 33 protected Set wrappedSet; 34 35 private boolean stale = false; 36 37 private Object setChangeListeners; 38 39 protected Object elementType; 40 41 protected ObservableSet(Set wrappedSet, Object elementType) { 42 this.wrappedSet = wrappedSet; 43 this.elementType = elementType; 44 } 45 46 public void addSetChangeListener(ISetChangeListener listener) { 47 if (setChangeListeners == null) { 48 boolean hadListeners = hasListeners(); 49 setChangeListeners = listener; 50 if (!hadListeners) { 51 firstListenerAdded(); 52 } 53 return; 54 } 55 56 Collection listenerList; 57 if (setChangeListeners instanceof Collection ) { 58 listenerList = (Collection ) setChangeListeners; 59 } else { 60 ISetChangeListener l = (ISetChangeListener) setChangeListeners; 61 62 listenerList = new ArrayList (); 63 listenerList.add(l); 64 setChangeListeners = listenerList; 65 } 66 67 if (listenerList.size() > 16) { 68 HashSet listenerSet = new HashSet (); 69 listenerSet.addAll(listenerList); 70 setChangeListeners = listenerList; 71 } 72 73 listenerList.add(listener); 74 } 75 76 public void removeSetChangeListener(ISetChangeListener listener) { 77 78 if (setChangeListeners == listener) { 79 setChangeListeners = null; 80 if (!hasListeners()) { 81 lastListenerRemoved(); 82 } 83 return; 84 } 85 86 if (setChangeListeners instanceof Collection ) { 87 Collection listenerList = (Collection ) setChangeListeners; 88 listenerList.remove(listener); 89 if (listenerList.size() == 0) { 90 setChangeListeners = null; 91 if (!hasListeners()) { 92 lastListenerRemoved(); 93 } 94 } 95 } 96 } 97 98 101 protected boolean hasListeners() { 102 return super.hasListeners() || setChangeListeners!=null; 103 } 104 105 protected void fireSetChange(SetDiff diff) { 106 super.fireChange(); 108 109 if (setChangeListeners == null) { 110 return; 111 } 112 113 if (setChangeListeners instanceof ISetChangeListener) { 114 ((ISetChangeListener) setChangeListeners).handleSetChange(this, diff); 115 return; 116 } 117 118 Collection changeListenerCollection = (Collection ) setChangeListeners; 119 120 ISetChangeListener[] listeners = (ISetChangeListener[]) (changeListenerCollection) 121 .toArray(new ISetChangeListener[changeListenerCollection.size()]); 122 for (int i = 0; i < listeners.length; i++) { 123 listeners[i].handleSetChange(this, diff); 124 } 125 } 126 127 public boolean contains(Object o) { 128 getterCalled(); 129 return wrappedSet.contains(o); 130 } 131 132 public boolean containsAll(Collection c) { 133 getterCalled(); 134 return wrappedSet.containsAll(c); 135 } 136 137 public boolean equals(Object o) { 138 getterCalled(); 139 return wrappedSet.equals(o); 140 } 141 142 public int hashCode() { 143 getterCalled(); 144 return wrappedSet.hashCode(); 145 } 146 147 public boolean isEmpty() { 148 getterCalled(); 149 return wrappedSet.isEmpty(); 150 } 151 152 public Iterator iterator() { 153 final Iterator wrappedIterator = wrappedSet.iterator(); 154 return new Iterator () { 155 156 public void remove() { 157 throw new UnsupportedOperationException (); 158 } 159 160 public boolean hasNext() { 161 ObservableTracker.getterCalled(ObservableSet.this); 162 return wrappedIterator.hasNext(); 163 } 164 165 public Object next() { 166 ObservableTracker.getterCalled(ObservableSet.this); 167 return wrappedIterator.next(); 168 } 169 }; 170 } 171 172 public int size() { 173 getterCalled(); 174 return wrappedSet.size(); 175 } 176 177 public Object [] toArray() { 178 getterCalled(); 179 return wrappedSet.toArray(); 180 } 181 182 public Object [] toArray(Object [] a) { 183 getterCalled(); 184 return wrappedSet.toArray(a); 185 } 186 187 public String toString() { 188 getterCalled(); 189 return wrappedSet.toString(); 190 } 191 192 protected void getterCalled() { 193 ObservableTracker.getterCalled(this); 194 } 195 196 public boolean add(Object o) { 197 throw new UnsupportedOperationException (); 198 } 199 200 public boolean addAll(Collection c) { 201 throw new UnsupportedOperationException (); 202 } 203 204 public boolean remove(Object o) { 205 throw new UnsupportedOperationException (); 206 } 207 208 public boolean removeAll(Collection c) { 209 throw new UnsupportedOperationException (); 210 } 211 212 public boolean retainAll(Collection c) { 213 throw new UnsupportedOperationException (); 214 } 215 216 public void clear() { 217 throw new UnsupportedOperationException (); 218 } 219 220 223 public boolean isStale() { 224 return stale; 225 } 226 227 233 public void setStale(boolean stale) { 234 boolean wasStale = this.stale; 235 this.stale = stale; 236 if (!wasStale && stale) { 237 fireStale(); 238 } 239 } 240 241 244 protected void setWrappedSet(Set wrappedSet) { 245 this.wrappedSet = wrappedSet; 246 } 247 248 protected void fireChange() { 249 throw new RuntimeException ("fireChange should not be called, use fireSetChange() instead"); } 251 252 255 public void dispose() { 256 setChangeListeners = null; 257 super.dispose(); 258 } 259 260 public Object getElementType() { 261 return elementType; 262 } 263 } 264 | Popular Tags |