KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > sun > appserv > management > util > misc > GSetUtil


1 /*
2  * The contents of this file are subject to the terms
3  * of the Common Development and Distribution License
4  * (the License). You may not use this file except in
5  * compliance with the License.
6  *
7  * You can obtain a copy of the license at
8  * https://glassfish.dev.java.net/public/CDDLv1.0.html or
9  * glassfish/bootstrap/legal/CDDLv1.0.txt.
10  * See the License for the specific language governing
11  * permissions and limitations under the License.
12  *
13  * When distributing Covered Code, include this CDDL
14  * Header Notice in each file and include the License file
15  * at glassfish/bootstrap/legal/CDDLv1.0.txt.
16  * If applicable, add the following below the CDDL Header,
17  * with the fields enclosed by brackets [] replaced by
18  * you own identifying information:
19  * "Portions Copyrighted [year] [name of copyright owner]"
20  *
21  * Copyright 2006 Sun Microsystems, Inc. All rights reserved.
22  */

23 package com.sun.appserv.management.util.misc;
24
25 import java.util.Set JavaDoc;
26 import java.util.HashSet JavaDoc;
27 import java.util.Collection JavaDoc;
28 import java.util.Collections JavaDoc;
29 import java.util.Arrays JavaDoc;
30
31
32 /**
33     Utilities for working with sets using JDK 1.5 generics.
34  */

35 public final class GSetUtil
36 {
37         private
38     GSetUtil( )
39     {
40         // disallow instantiation
41
}
42     
43         public static <T> T
44     getSingleton( final Set JavaDoc<T> s )
45     {
46         if ( s.size() != 1 )
47         {
48             throw new IllegalArgumentException JavaDoc( s.toString() );
49         }
50         return( s.iterator().next() );
51     }
52     
53         public static <T> void
54     addArray(
55         final Set JavaDoc<T> set,
56         final T[] array )
57     {
58         for( final T item : array )
59         {
60             set.add( item );
61         }
62     }
63     
64         public static <T> Set JavaDoc<T>
65     newSet( final Collection JavaDoc<T> c )
66     {
67         final HashSet JavaDoc<T> set = new HashSet JavaDoc<T>();
68         
69         set.addAll( c );
70         
71         return( set );
72     }
73     
74     
75     /**
76         Create a new Set with one member.
77      */

78         public static <T> Set JavaDoc<T>
79     newSet( final T item )
80     {
81         final Set JavaDoc<T> set = new HashSet JavaDoc<T>();
82         set.add( item );
83         
84         return( set );
85     }
86     
87     /**
88         Create a new Set containing all members of another.
89         The returned Set is always a HashSet.
90      */

91         public static <T> HashSet JavaDoc<T>
92     copySet( final Set JavaDoc<? extends T> s1 )
93     {
94         final HashSet JavaDoc<T> set = new HashSet JavaDoc<T>();
95         
96         set.addAll( s1 );
97         
98         return( set );
99     }
100     
101     
102         public static <T> Set JavaDoc<? extends T>
103     newSet(
104         final T m1,
105         final T m2 )
106     {
107         final HashSet JavaDoc<T> set = new HashSet JavaDoc<T>();
108         
109         set.add( m1 );
110         set.add( m2 );
111         
112         return( set );
113     }
114     
115     /*
116         public static <T> Set<T>
117     newSet(
118         final T m1,
119         final T m2,
120         final T m3 )
121     {
122         final HashSet<T> set = new HashSet<T>();
123         
124         set.add( m1 );
125         set.add( m2 );
126         set.add( m3 );
127         
128         return( set );
129     }
130     */

131     
132         public static <T> Set JavaDoc<T>
133     newSet(
134         final T m1,
135         final T m2,
136         final T m3,
137         final T m4 )
138     {
139         final HashSet JavaDoc<T> set = new HashSet JavaDoc<T>();
140         
141         set.add( m1 );
142         set.add( m2 );
143         set.add( m3 );
144         set.add( m4 );
145         
146         return( set );
147     }
148     
149     
150     /**
151         Create a new Set containing all array elements.
152      */

153         public static <T> Set JavaDoc<T>
154     newSet( final T[] objects )
155     {
156         return( newSet( objects, 0, objects.length ) );
157     }
158     
159         public static <T,TT extends T> Set JavaDoc<T>
160     newSet( final Set JavaDoc<T> s1, final Set JavaDoc<TT> s2 )
161     {
162         final Set JavaDoc<T> both = new HashSet JavaDoc<T>();
163         both.addAll( s1 );
164         both.addAll( s2 );
165         
166         return both;
167     }
168     
169
170
171     /**
172         Create a new Set containing all array elements.
173      */

174         public static <T> Set JavaDoc<T>
175     newSet(
176         final T[] objects,
177         final int startIndex,
178         final int numItems )
179     {
180         final Set JavaDoc<T> set = new HashSet JavaDoc<T>();
181         
182         for( int i = 0; i < numItems; ++i )
183         {
184             set.add( objects[ startIndex + i ] );
185         }
186
187         return( set );
188     }
189     
190     /**
191         Convert a Set to a String[]
192      */

193         public static String JavaDoc[]
194     toStringArray( final Set JavaDoc<?> s )
195     {
196         final String JavaDoc[] strings = new String JavaDoc[ s.size() ];
197         
198         int i = 0;
199         for( final Object JavaDoc o : s )
200         {
201             strings[ i ] = "" + o;
202             ++i;
203         }
204         
205         return( strings );
206     }
207     
208         public static String JavaDoc[]
209     toSortedStringArray( final Set JavaDoc<?> s )
210     {
211         final String JavaDoc[] strings = toStringArray( s );
212         
213         Arrays.sort( strings );
214         
215         return( strings );
216     }
217     
218         public static Set JavaDoc<String JavaDoc>
219     newStringSet( final String JavaDoc... args)
220     {
221         return newUnmodifiableSet( args );
222     }
223     
224     
225         public static <T> Set JavaDoc<T>
226     newUnmodifiableSet( final T... args)
227     {
228         final Set JavaDoc<T> set = new HashSet JavaDoc<T>();
229         
230         for( final T s : args )
231         {
232             set.add( s );
233         }
234         return set;
235     }
236     
237         public static Set JavaDoc<String JavaDoc>
238     newUnmodifiableStringSet( final String JavaDoc... args)
239     {
240         return Collections.unmodifiableSet( newStringSet( args ) );
241     }
242     
243         public static Set JavaDoc<String JavaDoc>
244     newStringSet( final Object JavaDoc... args)
245     {
246         final Set JavaDoc<String JavaDoc> set = new HashSet JavaDoc<String JavaDoc>();
247         
248         for( final Object JavaDoc o : args )
249         {
250             set.add( o == null ? null : "" + o );
251         }
252         return set;
253     }
254     
255     
256     /**
257         Create a new Set with one member. Additional items
258         may be added.
259      */

260         public static <T> Set JavaDoc<T>
261     newSingletonSet( final T m1 )
262     {
263         final Set JavaDoc<T> set = new HashSet JavaDoc<T>();
264         
265         set.add( m1 );
266         
267         return( set );
268     }
269     
270     
271     /**
272         Return a new Set of all items in both set1 and set2.
273      */

274         public static <T> Set JavaDoc<T>
275     intersectSets(
276         final Set JavaDoc<T> set1,
277         final Set JavaDoc<T> set2 )
278     {
279         final Set JavaDoc<T> result = newSet( set1 );
280         result.retainAll( set2 );
281         
282         return( result );
283     }
284     
285     
286     /**
287         Return a new Set of all items in set1 not in set2.
288      */

289         public static <T> Set JavaDoc<T>
290     removeSet(
291         final Set JavaDoc<T> set1,
292         final Set JavaDoc<T> set2 )
293     {
294         final Set JavaDoc<T> result = SetUtil.newSet( set1 );
295         result.removeAll( set2 );
296         
297         return( result );
298     }
299     
300     
301     /**
302         Return a new Set of all items not common to both sets.
303      */

304         public static <T> Set JavaDoc<T>
305     newNotCommonSet(
306         final Set JavaDoc<T> set1,
307         final Set JavaDoc<T> set2 )
308     {
309         final Set JavaDoc<T> result = newSet( set1, set2 );
310         final Set JavaDoc<T> common = intersectSets( set1, set2);
311         
312         result.removeAll( common );
313         
314         return( result );
315     }
316 }
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
Popular Tags