KickJava   Java API By Example, From Geeks To Geeks.

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


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.ObservableTracker;
20 import org.eclipse.core.databinding.observable.Realm;
21
22 /**
23  *
24  * Abstract implementation of {@link IObservableSet}.
25  *
26  * <p>
27  * This class is thread safe. All state accessing methods must be invoked from
28  * the {@link Realm#isCurrent() current realm}. Methods for adding and removing
29  * listeners may be invoked from any thread.
30  * </p>
31  *
32  * @since 1.0
33  *
34  */

35 public abstract class ObservableSet extends AbstractObservable implements
36         IObservableSet {
37
38     protected Set JavaDoc wrappedSet;
39
40     private boolean stale = false;
41
42     protected Object JavaDoc elementType;
43
44     protected ObservableSet(Set JavaDoc wrappedSet, Object JavaDoc elementType) {
45         this(Realm.getDefault(), wrappedSet, elementType);
46     }
47
48     protected ObservableSet(Realm realm, Set JavaDoc wrappedSet, Object JavaDoc 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         // fire general change event first
64
super.fireChange();
65
66         fireEvent(new SetChangeEvent(this, diff));
67     }
68     
69     public boolean contains(Object JavaDoc o) {
70         getterCalled();
71         return wrappedSet.contains(o);
72     }
73
74     public boolean containsAll(Collection JavaDoc c) {
75         getterCalled();
76         return wrappedSet.containsAll(c);
77     }
78
79     public boolean equals(Object JavaDoc 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 JavaDoc iterator() {
95         getterCalled();
96         final Iterator JavaDoc wrappedIterator = wrappedSet.iterator();
97         return new Iterator JavaDoc() {
98
99             public void remove() {
100                 throw new UnsupportedOperationException JavaDoc();
101             }
102
103             public boolean hasNext() {
104                 ObservableTracker.getterCalled(ObservableSet.this);
105                 return wrappedIterator.hasNext();
106             }
107
108             public Object JavaDoc 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 JavaDoc[] toArray() {
121         getterCalled();
122         return wrappedSet.toArray();
123     }
124
125     public Object JavaDoc[] toArray(Object JavaDoc[] a) {
126         getterCalled();
127         return wrappedSet.toArray(a);
128     }
129
130     public String JavaDoc toString() {
131         getterCalled();
132         return wrappedSet.toString();
133     }
134
135     protected void getterCalled() {
136         ObservableTracker.getterCalled(this);
137     }
138
139     public boolean add(Object JavaDoc o) {
140         throw new UnsupportedOperationException JavaDoc();
141     }
142
143     public boolean addAll(Collection JavaDoc c) {
144         throw new UnsupportedOperationException JavaDoc();
145     }
146
147     public boolean remove(Object JavaDoc o) {
148         throw new UnsupportedOperationException JavaDoc();
149     }
150
151     public boolean removeAll(Collection JavaDoc c) {
152         throw new UnsupportedOperationException JavaDoc();
153     }
154
155     public boolean retainAll(Collection JavaDoc c) {
156         throw new UnsupportedOperationException JavaDoc();
157     }
158
159     public void clear() {
160         throw new UnsupportedOperationException JavaDoc();
161     }
162
163     /**
164      * @return Returns the stale state.
165      */

166     public boolean isStale() {
167         checkRealm();
168         return stale;
169     }
170
171     /**
172      * @param stale
173      * The stale state to set. This will fire a stale event if the
174      * given boolean is true and this observable set was not already
175      * stale.
176      */

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     /**
187      * @param wrappedSet The wrappedSet to set.
188      */

189     protected void setWrappedSet(Set JavaDoc wrappedSet) {
190         this.wrappedSet = wrappedSet;
191     }
192
193     protected void fireChange() {
194         throw new RuntimeException JavaDoc("fireChange should not be called, use fireSetChange() instead"); //$NON-NLS-1$
195
}
196     
197     /* (non-Javadoc)
198      * @see org.eclipse.jface.provisional.databinding.observable.AbstractObservable#dispose()
199      */

200     public synchronized void dispose() {
201         super.dispose();
202     }
203     
204     public Object JavaDoc getElementType() {
205         return elementType;
206     }
207 }
208
Popular Tags