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