KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > jface > internal > databinding > provisional > observable > set > WritableSet


1 /*******************************************************************************
2  * Copyright (c) 2006 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.jface.internal.databinding.provisional.observable.set;
13
14 import java.util.Collection JavaDoc;
15 import java.util.Collections JavaDoc;
16 import java.util.HashSet JavaDoc;
17 import java.util.Iterator JavaDoc;
18 import java.util.Set JavaDoc;
19
20 import org.eclipse.jface.internal.databinding.provisional.observable.Diffs;
21
22 /**
23  * @since 1.0
24  *
25  */

26 public class WritableSet extends ObservableSet {
27
28     /**
29      * @param wrappedSet
30      */

31     public WritableSet() {
32         this(Object JavaDoc.class);
33     }
34
35     /**
36      * @param c
37      */

38     public WritableSet(Collection JavaDoc c) {
39         this(c, Object JavaDoc.class);
40     }
41
42     /**
43      * @param c
44      * @param elementType
45      */

46     public WritableSet(Collection JavaDoc c, Object JavaDoc elementType) {
47         super(new HashSet JavaDoc(c), elementType);
48         this.elementType = elementType;
49     }
50
51     /**
52      * @param elementType
53      */

54     public WritableSet(Object JavaDoc elementType) {
55         super(new HashSet JavaDoc(), elementType);
56     }
57
58     public boolean add(Object JavaDoc o) {
59         boolean added = wrappedSet.add(o);
60         if (added) {
61             fireSetChange(Diffs.createSetDiff(Collections.singleton(o), Collections.EMPTY_SET));
62         }
63         return added;
64     }
65
66     public boolean addAll(Collection JavaDoc c) {
67         Set JavaDoc adds = new HashSet JavaDoc();
68         Iterator JavaDoc it = c.iterator();
69         while (it.hasNext()) {
70             Object JavaDoc element = it.next();
71             if (wrappedSet.add(element)) {
72                 adds.add(element);
73             }
74         }
75         if (adds.size() > 0) {
76             fireSetChange(Diffs.createSetDiff(adds, Collections.EMPTY_SET));
77             return true;
78         }
79         return false;
80     }
81
82     public boolean remove(Object JavaDoc o) {
83         boolean removed = wrappedSet.remove(o);
84         if (removed) {
85             fireSetChange(Diffs.createSetDiff(Collections.EMPTY_SET, Collections
86                     .singleton(o)));
87         }
88         return removed;
89     }
90
91     public boolean removeAll(Collection JavaDoc c) {
92         Set JavaDoc removes = new HashSet JavaDoc();
93         Iterator JavaDoc it = c.iterator();
94         while (it.hasNext()) {
95             Object JavaDoc element = it.next();
96             if (wrappedSet.remove(element)) {
97                 removes.add(element);
98             }
99         }
100         if (removes.size() > 0) {
101             fireSetChange(Diffs.createSetDiff(Collections.EMPTY_SET, removes));
102             return true;
103         }
104         return false;
105     }
106
107     public boolean retainAll(Collection JavaDoc c) {
108         Set JavaDoc removes = new HashSet JavaDoc();
109         Iterator JavaDoc it = wrappedSet.iterator();
110         while (it.hasNext()) {
111             Object JavaDoc element = it.next();
112             if (!c.contains(element)) {
113                 it.remove();
114                 removes.add(element);
115             }
116         }
117         if (removes.size() > 0) {
118             fireSetChange(Diffs.createSetDiff(Collections.EMPTY_SET, removes));
119             return true;
120         }
121         return false;
122     }
123
124     public void clear() {
125         Set JavaDoc removes = new HashSet JavaDoc(wrappedSet);
126         wrappedSet.clear();
127         fireSetChange(Diffs.createSetDiff(Collections.EMPTY_SET, removes));
128     }
129
130 }
131
Popular Tags