1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19 package net.wotonomy.foundation;
20
21 import java.util.Collection;
22 import java.util.Enumeration;
23 import java.util.HashSet;
24 import java.util.Iterator;
25 import java.util.Set;
26 import java.util.Vector;
27
28 /***
29 * A pure java implementation of NSSet that
30 * implements Set for greater java interoperability.
31 *
32 * @author michael@mpowers.net
33 * @author $Author: cgruber $
34 * @version $Revision: 893 $
35 */
36 public class NSSet extends HashSet
37 {
38 /***
39 * Default constructor.
40 */
41 public NSSet ()
42 {
43 super();
44 }
45
46 /***
47 * Constructs a NSSet containing the objects
48 * in the specified collection.
49 */
50 public NSSet ( Collection aCollection )
51 {
52 super( aCollection );
53 }
54
55 /***
56 * Constructs a NSSet containing only
57 * the specified object.
58 */
59 public NSSet ( Object anObject )
60 {
61 super();
62 add( anObject );
63 }
64
65 /***
66 * Constructs a NSSet containing the objects
67 * in the specified array.
68 */
69 public NSSet ( Object[] anObjectArray )
70 {
71 super();
72 for ( int i = 0; i < anObjectArray.length; i++ )
73 {
74 add( anObjectArray[i] );
75 }
76 }
77
78 /***
79 * Returns an NSArray containing all objects in the set.
80 */
81 public NSArray allObjects ()
82 {
83 return new NSArray( this );
84 }
85
86 /***
87 *
88 */
89 public Object anyObject ()
90 {
91 throw new RuntimeException( "Not implemented yet." );
92 }
93
94 /***
95 * Returns whether this set contains the
96 * specified object.
97 */
98 public boolean containsObject ( Object anObject )
99 {
100 return contains( anObject );
101 }
102
103 /***
104 * Returns the number of elements in this set.
105 */
106 public int count ()
107 {
108 return size();
109 }
110
111 /***
112 * Returns whether this set has one or more
113 * elements in common with the specified set.
114 */
115 public boolean intersectsSet ( Set aSet )
116 {
117 Iterator it = aSet.iterator();
118 while ( it.hasNext() )
119 {
120 if ( this.containsObject( it.next() ) )
121 {
122 return true;
123 }
124 }
125 return false;
126
127 }
128
129 /***
130 * Returns whether this set contains the
131 * same object as the specified set.
132 */
133 public boolean isEqualToSet ( Set aSet )
134 {
135 return equals( aSet );
136 }
137
138 /***
139 * Returns whether this set is a subset
140 * of the specified set.
141 */
142 public boolean isSubsetOfSet ( Set aSet )
143 {
144 return aSet.containsAll( this );
145 }
146
147 /***
148 *
149 */
150 public Object member ( Object anObject )
151 {
152 throw new RuntimeException( "Not implemented yet." );
153 }
154
155 /***
156 * Returns an enumerator over the objects
157 * in this set.
158 */
159 public Enumeration objectEnumerator ()
160 {
161 return new Vector( this ).elements();
162 }
163
164 /***
165 * Returns a set that is the intersection
166 * of this set and the specified set.
167 */
168 public NSSet setByIntersectingSet ( Set aSet )
169 {
170 NSSet result = new NSSet( this );
171 result.retainAll( aSet );
172 return result;
173 }
174
175 /***
176 * Returns a set that contains all elements
177 * in this set that are not in the specified set.
178 */
179 public NSSet setBySubtractingSet ( Set aSet )
180 {
181 NSSet result = new NSSet( this );
182 result.removeAll( aSet );
183 return result;
184 }
185
186 /***
187 * Returns a set that is the union
188 * of this set and the specified set.
189 */
190 public NSSet setByUnioningSet ( Set aSet )
191 {
192 NSSet result = new NSSet( this );
193 result.addAll( aSet );
194 return result;
195 }
196
197 }
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212