KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > core > databinding > observable > set > AbstractObservableSet


1 /*******************************************************************************
2  * Copyright (c) 2006, 2007 IBM Corporation and others.
3  * All rights reserved. This program and the accompanying materials
4  * are made available under the terms of the Eclipse Public License v1.0
5  * which accompanies this distribution, and is available at
6  * http://www.eclipse.org/legal/epl-v10.html
7  *
8  * Contributors:
9  * IBM Corporation - initial API and implementation
10  *******************************************************************************/

11
12 package org.eclipse.core.databinding.observable.set;
13
14 import java.util.Collection JavaDoc;
15 import java.util.Iterator JavaDoc;
16 import java.util.Set JavaDoc;
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 /**
24  *
25  * Abstract implementation of {@link IObservableSet}.
26  *
27  * <p>
28  * This class is thread safe. All state accessing methods must be invoked from
29  * the {@link Realm#isCurrent() current realm}. Methods for adding and removing
30  * listeners may be invoked from any thread.
31  * </p>
32  *
33  * @since 1.0
34  */

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 JavaDoc getWrappedSet();
75     
76     protected void fireSetChange(SetDiff diff) {
77         // fire general change event first
78
super.fireChange();
79
80         changeSupport.fireEvent(new SetChangeEvent(this, diff));
81     }
82     
83     public boolean contains(Object JavaDoc o) {
84         getterCalled();
85         return getWrappedSet().contains(o);
86     }
87
88     public boolean containsAll(Collection JavaDoc c) {
89         getterCalled();
90         return getWrappedSet().containsAll(c);
91     }
92
93     public boolean equals(Object JavaDoc 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 JavaDoc iterator() {
109         getterCalled();
110         final Iterator JavaDoc wrappedIterator = getWrappedSet().iterator();
111         return new Iterator JavaDoc() {
112
113             public void remove() {
114                 throw new UnsupportedOperationException JavaDoc();
115             }
116
117             public boolean hasNext() {
118                 ObservableTracker.getterCalled(AbstractObservableSet.this);
119                 return wrappedIterator.hasNext();
120             }
121
122             public Object JavaDoc 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 JavaDoc[] toArray() {
135         getterCalled();
136         return getWrappedSet().toArray();
137     }
138
139     public Object JavaDoc[] toArray(Object JavaDoc[] a) {
140         getterCalled();
141         return getWrappedSet().toArray(a);
142     }
143
144     public String JavaDoc toString() {
145         getterCalled();
146         return getWrappedSet().toString();
147     }
148
149     protected void getterCalled() {
150         ObservableTracker.getterCalled(this);
151     }
152
153     public boolean add(Object JavaDoc o) {
154         throw new UnsupportedOperationException JavaDoc();
155     }
156
157     public boolean addAll(Collection JavaDoc c) {
158         throw new UnsupportedOperationException JavaDoc();
159     }
160
161     public boolean remove(Object JavaDoc o) {
162         throw new UnsupportedOperationException JavaDoc();
163     }
164
165     public boolean removeAll(Collection JavaDoc c) {
166         throw new UnsupportedOperationException JavaDoc();
167     }
168
169     public boolean retainAll(Collection JavaDoc c) {
170         throw new UnsupportedOperationException JavaDoc();
171     }
172
173     public void clear() {
174         throw new UnsupportedOperationException JavaDoc();
175     }
176
177     /**
178      * @return Returns the stale state.
179      */

180     public boolean isStale() {
181         checkRealm();
182         return stale;
183     }
184
185     /**
186      * @param stale
187      * The stale state to set. This will fire a stale event if the
188      * given boolean is true and this observable set was not already
189      * stale.
190      */

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 JavaDoc("fireChange should not be called, use fireSetChange() instead"); //$NON-NLS-1$
203
}
204     
205     /* (non-Javadoc)
206      * @see org.eclipse.jface.provisional.databinding.observable.AbstractObservable#dispose()
207      */

208     public synchronized void dispose() {
209         super.dispose();
210         changeSupport.dispose();
211         changeSupport = null;
212     }
213     
214 }
215
Popular Tags