KickJava   Java API By Example, From Geeks To Geeks.

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


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  * Brad Reynolds - bug 147515
11  *******************************************************************************/

12
13 package org.eclipse.core.databinding.observable.set;
14
15 import java.util.Collection JavaDoc;
16 import java.util.Collections JavaDoc;
17 import java.util.HashSet JavaDoc;
18 import java.util.Iterator JavaDoc;
19 import java.util.Set JavaDoc;
20
21 import org.eclipse.core.databinding.observable.Diffs;
22 import org.eclipse.core.databinding.observable.Realm;
23
24 /**
25  * Mutable (writable) 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 class WritableSet extends ObservableSet {
36
37     /**
38      * Constructs a new instance with the default realm, empty list, and a
39      * <code>null</code> element type.
40      *
41      */

42     public WritableSet() {
43         this(Realm.getDefault());
44     }
45
46     /**
47      * Construts a new instance with the default realm.
48      *
49      * @param c
50      * @param elementType
51      * can be <code>null</code>
52      */

53     public WritableSet(Collection JavaDoc c, Object JavaDoc elementType) {
54         this(Realm.getDefault(), new HashSet JavaDoc(c), elementType);
55     }
56
57     /**
58      * Constructs a new instance with an empty list and a <code>null</code> element type.
59      *
60      * @param realm
61      */

62     public WritableSet(Realm realm) {
63         this(realm, new HashSet JavaDoc(), null);
64     }
65
66     /**
67      * Constructs a new instance.
68      *
69      * @param realm
70      * @param c
71      * @param elementType
72      * can be <code>null</code>
73      */

74     public WritableSet(Realm realm, Collection JavaDoc c, Object JavaDoc elementType) {
75         super(realm, new HashSet JavaDoc(c), elementType);
76         this.elementType = elementType;
77     }
78
79     public boolean add(Object JavaDoc o) {
80         checkRealm();
81         boolean added = wrappedSet.add(o);
82         if (added) {
83             fireSetChange(Diffs.createSetDiff(Collections.singleton(o), Collections.EMPTY_SET));
84         }
85         return added;
86     }
87
88     public boolean addAll(Collection JavaDoc c) {
89         checkRealm();
90         Set JavaDoc adds = new HashSet JavaDoc();
91         Iterator JavaDoc it = c.iterator();
92         while (it.hasNext()) {
93             Object JavaDoc element = it.next();
94             if (wrappedSet.add(element)) {
95                 adds.add(element);
96             }
97         }
98         if (adds.size() > 0) {
99             fireSetChange(Diffs.createSetDiff(adds, Collections.EMPTY_SET));
100             return true;
101         }
102         return false;
103     }
104
105     public boolean remove(Object JavaDoc o) {
106         checkRealm();
107         boolean removed = wrappedSet.remove(o);
108         if (removed) {
109             fireSetChange(Diffs.createSetDiff(Collections.EMPTY_SET, Collections
110                     .singleton(o)));
111         }
112         return removed;
113     }
114
115     public boolean removeAll(Collection JavaDoc c) {
116         checkRealm();
117         Set JavaDoc removes = new HashSet JavaDoc();
118         Iterator JavaDoc it = c.iterator();
119         while (it.hasNext()) {
120             Object JavaDoc element = it.next();
121             if (wrappedSet.remove(element)) {
122                 removes.add(element);
123             }
124         }
125         if (removes.size() > 0) {
126             fireSetChange(Diffs.createSetDiff(Collections.EMPTY_SET, removes));
127             return true;
128         }
129         return false;
130     }
131
132     public boolean retainAll(Collection JavaDoc c) {
133         checkRealm();
134         Set JavaDoc removes = new HashSet JavaDoc();
135         Iterator JavaDoc it = wrappedSet.iterator();
136         while (it.hasNext()) {
137             Object JavaDoc element = it.next();
138             if (!c.contains(element)) {
139                 it.remove();
140                 removes.add(element);
141             }
142         }
143         if (removes.size() > 0) {
144             fireSetChange(Diffs.createSetDiff(Collections.EMPTY_SET, removes));
145             return true;
146         }
147         return false;
148     }
149
150     public void clear() {
151         checkRealm();
152         Set JavaDoc removes = new HashSet JavaDoc(wrappedSet);
153         wrappedSet.clear();
154         fireSetChange(Diffs.createSetDiff(Collections.EMPTY_SET, removes));
155     }
156
157     /**
158      * @param elementType can be <code>null</code>
159      * @return new instance with the default realm
160      */

161     public static WritableSet withElementType(Object JavaDoc elementType) {
162         return new WritableSet(Realm.getDefault(), new HashSet JavaDoc(), elementType);
163     }
164 }
165
Popular Tags