KickJava   Java API By Example, From Geeks To Geeks.

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


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.Collections JavaDoc;
28
29 import java.lang.reflect.Array JavaDoc;
30
31 import com.sun.appserv.management.util.misc.ClassUtil;
32
33 /**
34     Provides:
35     - methods to convert arrays of primitive types to corresponding arrays of Object types
36     - conversion to/from Set
37  */

38 public final class ArrayConversion
39 {
40         private
41     ArrayConversion( )
42     {
43         // disallow instantiation
44
}
45     
46         private static Object JavaDoc []
47     convert( Object JavaDoc simpleArray )
48     {
49         if ( ! ClassUtil.objectIsPrimitiveArray( simpleArray ) )
50         {
51             throw new IllegalArgumentException JavaDoc();
52         }
53         
54         final String JavaDoc className = simpleArray.getClass().getName();
55         
56         final Class JavaDoc theClass = ClassUtil.getArrayElementClass( simpleArray.getClass() );
57         
58         final int numItems = Array.getLength( simpleArray );
59         
60         final Class JavaDoc elementClass = ClassUtil.PrimitiveClassToObjectClass( theClass );
61         
62         final Object JavaDoc [] result = (Object JavaDoc [])Array.newInstance( elementClass, numItems );
63         
64         for( int i = 0; i < numItems; ++i )
65         {
66             result[ i ] = Array.get( simpleArray, i );
67         }
68         
69         return( result );
70     }
71     
72     /**
73         Convert an an array of primitive types to an array of Objects of non-primitive
74         types eg int to Integer.
75         
76         @param array the array to convert
77      */

78         public static Object JavaDoc []
79     toAppropriateType( Object JavaDoc array)
80     {
81         return( (Object JavaDoc [])convert( array ) );
82     }
83     
84     
85         public static Boolean JavaDoc []
86     toBooleans( boolean [] array )
87     {
88         return( (Boolean JavaDoc [])convert( array ) );
89     }
90     
91         public static Character JavaDoc []
92     toCharacters( char [] array )
93     {
94         return( (Character JavaDoc [])convert( array ) );
95     }
96     
97         public static Byte JavaDoc []
98     toBytes( byte [] array )
99     {
100         return( (Byte JavaDoc [])convert( array ) );
101     }
102     
103         public static Short JavaDoc []
104     toShorts( short [] array )
105     {
106         return( (Short JavaDoc [])convert( array ) );
107     }
108     
109         public static Integer JavaDoc []
110     toIntegers( int [] array )
111     {
112         return( (Integer JavaDoc [])convert( array ) );
113     }
114     
115         public static Long JavaDoc []
116     toLongs( long [] array )
117     {
118         return( (Long JavaDoc [])convert( array ) );
119     }
120     
121         public static Float JavaDoc []
122     toFloats( float [] array )
123     {
124         return( (Float JavaDoc [])convert( array ) );
125     }
126     
127         public static Double JavaDoc []
128     toDoubles( double [] array )
129     {
130         return( (Double JavaDoc [])convert( array ) );
131     }
132     
133     
134     /**
135         Create an array whose type is elementType[] of specified size.
136         
137         @param elementType the type of each entry of the array
138         @param size the number of elements
139      */

140         public static Object JavaDoc []
141     createObjectArrayType( final Class JavaDoc elementType, final int size )
142     {
143         final Object JavaDoc [] result = (Object JavaDoc []) Array.newInstance( elementType, size );
144         
145         return( result );
146     }
147     
148         public static Object JavaDoc[]
149     subArray( final Object JavaDoc[] in, int start, int end )
150     {
151         final int count = 1 + (end - start);
152         final Object JavaDoc[] result = (Object JavaDoc[])
153             Array.newInstance( ClassUtil.getArrayElementClass( in.getClass() ), count );
154         
155         for( int i = 0; i < count; ++i )
156         {
157             result[ i ] = in[ i + start ];
158         }
159         
160         return( result );
161     }
162     
163     
164         
165         public static <T> Set JavaDoc<T>
166     toSet( T[] array )
167     {
168         Set JavaDoc<T> theSet = null;
169         if ( array.length == 0 )
170         {
171             theSet = Collections.emptySet();
172         }
173         else if ( array.length == 1 )
174         {
175             theSet = Collections.singleton( array[ 0 ] );
176         }
177         else
178         {
179             theSet = new HashSet JavaDoc<T>();
180             for( int i = 0; i < array.length; ++i )
181             {
182                 theSet.add( array[ i ] );
183             }
184         }
185         return( theSet );
186     }
187     
188     
189     /*
190         Return true if every element of the array has *exactly* the same class.
191      */

192         public static boolean
193     hasIdenticalElementClasses( final Object JavaDoc [] a )
194     {
195         boolean isUniform = true;
196         
197         if ( a.length > 0 )
198         {
199             final Class JavaDoc matchType = a[ 0 ].getClass();
200             
201             for( int i = 1; i < a.length; ++i )
202             {
203                 if ( a[ i ].getClass() != matchType )
204                 {
205                     isUniform = false;
206                     break;
207                 }
208             }
209         }
210         
211         return( isUniform );
212     }
213     
214     /**
215         Specialize the type of the array (if possible). For example, if the
216         array is an Object[] of Integer, return an Integer[] of Integer.
217         
218         @param a the array to specialize
219         @return a specialized array (if possible) otherwise the original array
220      */

221         public static Object JavaDoc []
222     specializeArray( final Object JavaDoc [] a )
223     {
224         Object JavaDoc[] result = a;
225         
226         if ( hasIdenticalElementClasses( a ) &&
227             a.length != 0 &&
228             a.getClass() == Object JavaDoc[].class )
229         {
230             result = createObjectArrayType( a[0].getClass(), a.length );
231         
232             System.arraycopy( a, 0, result, 0, a.length );
233         }
234         
235         return( result );
236     }
237     
238     /**
239         Convert a Set to an array. If specialize is true, then provide
240         the most specialized type possible via specializeArray()
241         
242         @param s the Set to convert
243         @param specialize decide whether to specialize the type or not
244      */

245         public static Object JavaDoc []
246     setToArray( final Set JavaDoc<?> s, boolean specialize )
247     {
248         Object JavaDoc [] result = setToArray( s );
249         
250         if ( specialize && result.length != 0)
251         {
252             result = specializeArray( result );
253         }
254         
255         return( result );
256     }
257     
258     /**
259         Convert a Set to an Object[].
260         
261         @param s the Set to convert
262      */

263         public static Object JavaDoc []
264     setToArray( final Set JavaDoc<?> s )
265     {
266         final Object JavaDoc [] out = new Object JavaDoc [ s.size() ];
267         
268         setToArray( s, out );
269         
270         return( out );
271     }
272     
273     
274     /**
275         Convert a Set to an Object[].
276         
277         @param s the Set to convert
278         @param out the output array, must be of size s.size()
279      */

280         public static Object JavaDoc[]
281     setToArray( final Set JavaDoc<?> s, Object JavaDoc[] out )
282     {
283         if ( out.length != s.size() )
284         {
285             throw new IllegalArgumentException JavaDoc();
286         }
287         
288         int i = 0;
289         for( final Object JavaDoc o : s )
290         {
291             out[ i ] = o;
292             ++i;
293         }
294         
295         return( out );
296     }
297     
298     
299         public static <T> Set JavaDoc<T>
300     arrayToSet( final T [] names )
301     {
302         final Set JavaDoc<T> set = new HashSet JavaDoc<T>();
303         
304         for( int i = 0; i < names.length; ++i )
305         {
306             set.add( names[ i ] );
307         }
308
309         return( set );
310     }
311 }
312
313
Popular Tags