KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > sun > cli > util > 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  
24 /*
25  * $Header: /cvs/glassfish/admin-cli/cli-api/src/java/com/sun/cli/util/ArrayConversion.java,v 1.3 2005/12/25 03:45:57 tcfujii Exp $
26  * $Revision: 1.3 $
27  * $Date: 2005/12/25 03:45:57 $
28  */

29  
30 package com.sun.cli.util;
31
32 import java.lang.reflect.Array JavaDoc;
33
34 /*
35     Provides methods to convert arrays of primitive types to corresponding
36     arrays of Object types.
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         final String JavaDoc className = simpleArray.getClass().getName();
50         
51         //final String memberClassName = ClassUtil.getArrayMemberClassName( className );
52

53         final Class JavaDoc theClass = ClassUtil.getArrayElementClass( simpleArray.getClass() );
54         
55         final int numItems = Array.getLength( simpleArray );
56         
57         final Class JavaDoc elementClass = ClassUtil.PrimitiveClassToObjectClass( theClass );
58         
59         final Object JavaDoc [] result = (Object JavaDoc [])Array.newInstance( elementClass, numItems );
60         
61         for( int i = 0; i < numItems; ++i )
62         {
63             result[ i ] = Array.get( simpleArray, i );
64         }
65         
66         return( result );
67     }
68     
69         public static Object JavaDoc []
70     toAppropriateType( Object JavaDoc array)
71     {
72         return( (Object JavaDoc [])convert( array ) );
73     }
74     
75     
76         public static Boolean JavaDoc []
77     toBooleans( boolean [] array )
78     {
79         return( (Boolean JavaDoc [])convert( array ) );
80     }
81     
82         public static Character JavaDoc []
83     toCharacters( char [] array )
84     {
85         return( (Character JavaDoc [])convert( array ) );
86     }
87     
88         public static Byte JavaDoc []
89     toBytes( byte [] array )
90     {
91         return( (Byte JavaDoc [])convert( array ) );
92     }
93     
94         public static Short JavaDoc []
95     toShorts( short [] array )
96     {
97         return( (Short JavaDoc [])convert( array ) );
98     }
99     
100         public static Integer JavaDoc []
101     toIntegers( int [] array )
102     {
103         return( (Integer JavaDoc [])convert( array ) );
104     }
105     
106         public static Long JavaDoc []
107     toLongs( long [] array )
108     {
109         return( (Long JavaDoc [])convert( array ) );
110     }
111     
112         public static Float JavaDoc []
113     toFloats( float [] array )
114     {
115         return( (Float JavaDoc [])convert( array ) );
116     }
117     
118         public static Double JavaDoc []
119     toDoubles( double [] array )
120     {
121         return( (Double JavaDoc [])convert( array ) );
122     }
123     
124     
125         public static Object JavaDoc []
126     createObjectArrayType( final Class JavaDoc elementType, final int size )
127         throws Exception JavaDoc
128     {
129         final Object JavaDoc [] result = (Object JavaDoc []) Array.newInstance( elementType, size );
130         
131         return( result );
132     }
133     
134     
135     
136         
137         public static java.util.Set JavaDoc
138     toSet( Object JavaDoc [] array )
139     {
140         java.util.Set JavaDoc theSet = null;
141         if ( array.length == 0 )
142         {
143             theSet = java.util.Collections.EMPTY_SET;
144         }
145         else if ( array.length == 1 )
146         {
147             theSet = java.util.Collections.singleton( array[ 0 ] );
148         }
149         else
150         {
151             theSet = new java.util.HashSet JavaDoc();
152             for( int i = 0; i < array.length; ++i )
153             {
154                 theSet.add( array[ i ] );
155             }
156         }
157         return( theSet );
158     }
159 }
160
161
Popular Tags