KickJava   Java API By Example, From Geeks To Geeks.

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


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.Collection JavaDoc;
27 import java.util.Collections JavaDoc;
28 import java.util.HashSet JavaDoc;
29 import java.util.Iterator JavaDoc;
30
31 /**
32     @deprecated use GSetUtil
33  */

34 public final class SetUtil
35 {
36         private
37     SetUtil( )
38     {
39         // disallow instantiation
40
}
41     
42         public static <T> T
43     getSingleton( final Set JavaDoc<T> s )
44     {
45         if ( s.size() != 1 )
46         {
47             throw new IllegalArgumentException JavaDoc( s.toString() );
48         }
49         return( s.iterator().next() );
50     }
51     
52     /**
53         Add all items in an array to a set.
54      */

55         public static <T> void
56     addArray(
57         final Set JavaDoc<T> set,
58         final T[] array )
59     {
60         for( int i = 0; i < array.length; ++i )
61         {
62             set.add( array[ i ] );
63         }
64     }
65     
66     /**
67         Convert a Set to a String[]
68      */

69         public static String JavaDoc[]
70     toStringArray( final Set JavaDoc set )
71     {
72         final String JavaDoc[] strings = new String JavaDoc[ set.size() ];
73         
74         final Iterator JavaDoc iter = set.iterator();
75         int i = 0;
76         while ( iter.hasNext() )
77         {
78             strings[ i ] = iter.next().toString();
79             ++i;
80         }
81         
82         return( strings );
83     }
84     
85     /**
86         Convert a Set to a String[]
87      */

88         public static Object JavaDoc[]
89     toArray( final Set JavaDoc<?> set )
90     {
91         final Object JavaDoc[] names = new Object JavaDoc[ set.size() ];
92         set.toArray( names );
93         
94         return( names );
95     }
96     
97     /**
98         Create a new Set with one member.
99      */

100         public static <T> Set JavaDoc<T>
101     newSet( final Collection JavaDoc<T> c )
102     {
103         final Set JavaDoc<T> set = new HashSet JavaDoc<T>();
104         
105         set.addAll( c );
106         
107         return( set );
108     }
109     
110     
111     
112     
113     /**
114         Create a new Set with one member. Additional items
115         may be added.
116      */

117         public static <T> Set JavaDoc<T>
118     newSingletonSet( final T m1 )
119     {
120         final Set JavaDoc<T> set = new HashSet JavaDoc<T>();
121         
122         set.add( m1 );
123         
124         return( set );
125     }
126     
127     /**
128         Create a new Set containing all members of another.
129         The returned Set is always a HashSet.
130      */

131         public static <T> HashSet JavaDoc<T>
132     copySet( final Set JavaDoc<T> s1 )
133     {
134         final HashSet JavaDoc<T> set = new HashSet JavaDoc<T>();
135         
136         set.addAll( s1 );
137         
138         return( set );
139     }
140     
141     
142     /**
143         Create a new Set containing all array elements.
144      */

145         public static <T> Set JavaDoc<T>
146     newSet( final Set JavaDoc<? extends T>[] sets )
147     {
148         final Set JavaDoc<T> s = new HashSet JavaDoc<T>();
149         
150         for( int i = 0; i < sets.length; ++i )
151         {
152             s.addAll( sets[ i ] );
153         }
154
155         return( s );
156     }
157      
158      /*
159      private void
160     doIt()
161     {
162         final Set<String> strings = new HashSet<String>();
163         
164         *
165         final Set<String>[] stringSets = new Set[] { strings, strings};
166         final Set<String> test = newSet( stringSets );
167         
168         final Set<Integer> n1 = new HashSet<Integer>();
169         final Set<Float> n2 = new HashSet<Float>();
170         final Set<Number>[] numberSets = new Set[] { n1, n2 };
171         final Set<Number> nn = newSet( numberSets );
172         
173         final Set<? extends java.io.Serializable> x = newSet(s1, n1);
174         
175         *
176         final Set<String> xx = newSet(strings, strings);
177         final Set<String> xxx = newSet(strings, strings, strings);
178         final Set<String> xxxx = newSet(strings, strings, strings, strings);
179         
180         final Set<String> aa = newSet( "1", "2" );
181         final Set<String> aaa = newSet( "1", "2", "3" );
182        // final Set<String> aaaa = newSet( "1", "2", "3", new Integer(10) );
183         
184         
185         final Set<String> zzzz = newSet( "1", "2", "3", "4", "6", "7");
186     }
187     */

188    
189     
190     
191     /**
192         Create a new Set consisting of the contents of two sets.
193      */

194         public static <T> Set JavaDoc<T>
195     newSet(
196         final Set JavaDoc<? extends T> s1,
197         final Set JavaDoc<? extends T> s2)
198     {
199         final Set JavaDoc<T> result = new HashSet JavaDoc<T>();
200         result.addAll( s1 );
201         result.addAll( s2 );
202         
203         return result;
204     }
205     
206     /**
207         Create a new Set consisting of the contents of three sets.
208      */

209         public static <T> Set JavaDoc<T>
210     newSet(
211         final Set JavaDoc<? extends T> s1,
212         final Set JavaDoc<? extends T> s2,
213         final Set JavaDoc<? extends T> s3 )
214     {
215         return newSet( newSet( s1, s2 ), s3 );
216     }
217     
218     /**
219         Create a new Set consisting of the contents of four sets.
220      */

221         public static <T> Set JavaDoc<T>
222     newSet(
223         final Set JavaDoc<? extends T> s1,
224         final Set JavaDoc<? extends T> s2,
225         final Set JavaDoc<? extends T> s3,
226         final Set JavaDoc<? extends T> s4 )
227     {
228         return newSet( newSet( s1, s2 ), newSet( s3, s4) );
229     }
230
231         public static Set JavaDoc<String JavaDoc>
232     newUnmodifiableSet( final String JavaDoc[] objects )
233     {
234         return GSetUtil.newUnmodifiableStringSet( objects );
235     }
236     
237     /**
238         Create a new Set containing all array elements.
239      */

240         public static <T> Set JavaDoc<T>
241     newSet( final T... objects )
242     {
243         return( newSet( objects, 0, objects.length ) );
244     }
245
246
247     /**
248         Create a new Set containing all array elements.
249      */

250         public static <T> Set JavaDoc<T>
251     newSet(
252         final T [] objects,
253         final int startIndex,
254         final int numItems )
255     {
256         final Set JavaDoc<T> set = new HashSet JavaDoc<T>();
257         
258         for( int i = 0; i < numItems; ++i )
259         {
260             set.add( objects[ startIndex + i ] );
261         }
262
263         return( set );
264     }
265
266     /**
267         Return a new Set of all items in both set1 and set2.
268      */

269         public static <T> Set JavaDoc<T>
270     intersectSets(
271         final Set JavaDoc<T> set1,
272         final Set JavaDoc<T> set2 )
273     {
274         final Set JavaDoc<T> result = SetUtil.newSet( set1 );
275         result.retainAll( set2 );
276         
277         return( result );
278     }
279     
280     /**
281         Return a new Set of all items in set1 not in set2.
282      */

283         public static <T> Set JavaDoc<T>
284     removeSet(
285         final Set JavaDoc<T> set1,
286         final Set JavaDoc<T> set2 )
287     {
288         final Set JavaDoc<T> result = SetUtil.newSet( set1 );
289         result.removeAll( set2 );
290         
291         return( result );
292     }
293     
294     /**
295         Return a new Set of all items not common to both sets.
296      */

297         public static <T> Set JavaDoc<T>
298     newNotCommonSet(
299         final Set JavaDoc<T> set1,
300         final Set JavaDoc<T> set2 )
301     {
302         final Set JavaDoc<T> result = SetUtil.newSet( set1, set2 );
303         final Set JavaDoc<T> common = intersectSets( set1, set2);
304         
305         result.removeAll( common );
306         
307         return( result );
308     }
309
310 }
311
312
Popular Tags