KickJava   Java API By Example, From Geeks To Geeks.

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


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  
24 package com.sun.appserv.management.util.misc;
25
26 import java.util.Collection JavaDoc;
27 import java.util.Iterator JavaDoc;
28 import java.util.Arrays JavaDoc;
29 import java.lang.reflect.Array JavaDoc;
30
31 import com.sun.appserv.management.util.stringifier.SmartStringifier;
32
33 /**
34     Various helper utilities for Collections.
35  */

36 public final class CollectionUtil
37 {
38         private
39     CollectionUtil( )
40     {
41         // disallow instantiation
42
}
43     
44     /**
45         @return a String
46      */

47         public static String JavaDoc
48     toString(
49         final Collection JavaDoc c,
50         final String JavaDoc delim )
51     {
52         final String JavaDoc[] strings = toStringArray( c );
53         Arrays.sort( strings );
54         
55         return StringUtil.toString( delim, (Object JavaDoc[])strings );
56     }
57     
58         public static String JavaDoc
59     toString( final Collection JavaDoc c )
60     {
61         return toString( c, ", " );
62     }
63
64     
65         public static <T> T
66     getSingleton( final Collection JavaDoc<T> s )
67     {
68         if ( s.size() != 1 )
69         {
70             throw new IllegalArgumentException JavaDoc();
71         }
72         return( s.iterator().next() );
73     }
74     
75     /**
76         Add all items in an array to a set.
77      */

78         public static <T> void
79     addArray(
80         final Collection JavaDoc<T> c,
81         final T[] array )
82     {
83         for( int i = 0; i < array.length; ++i )
84         {
85             c.add( array[ i ] );
86         }
87     }
88     
89     /**
90         @return String[]
91      */

92         public static String JavaDoc[]
93     toStringArray( final Collection JavaDoc c )
94     {
95         final String JavaDoc[] strings = new String JavaDoc[ c.size() ];
96         
97         int i = 0;
98         for( final Object JavaDoc o : c )
99         {
100             strings[ i ] = SmartStringifier.toString( o );
101             ++i;
102         }
103         
104         return( strings );
105     }
106     
107     /**
108         @param c the Collection
109         @param elementClass the type of the element, must be non-primitive
110         @return array of <elementClass>[] elements
111      */

112         public static <T> T[]
113     toArray(
114         final Collection JavaDoc<? extends T> c,
115         final Class JavaDoc<T> elementClass )
116     {
117         final T[] items = ArrayUtil.newArray( elementClass, c.size() );
118         
119         c.toArray( items );
120         
121         return items;
122     }
123     
124     
125     /**
126         @return true if all elements are String, and there is at least one element
127      */

128         public static boolean
129     isAllStrings( final Collection JavaDoc<?> c )
130     {
131         return IteratorUtil.getUniformClass( c.iterator() ) == String JavaDoc.class;
132     }
133 }
134
135
136
137
138
139
140
141
142
143
144
145
146
147
Popular Tags