KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > sun > enterprise > admin > 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  * Copyright 2004-2005 Sun Microsystems, Inc. All rights reserved.
26  * Use is subject to license terms.
27  */

28  
29 /*
30  * $Header: /cvs/glassfish/admin-core/util/src/java/com/sun/enterprise/admin/util/ArrayConversion.java,v 1.2 2005/12/25 03:53:04 tcfujii Exp $
31  * $Revision: 1.2 $
32  * $Date: 2005/12/25 03:53:04 $
33  */

34  
35 package com.sun.enterprise.admin.util;
36
37 import java.lang.reflect.Array JavaDoc;
38
39 /*
40     Provides methods to convert arrays of primitive types to corresponding
41     arrays of Object types.
42  */

43 public final class ArrayConversion
44 {
45         private
46     ArrayConversion( )
47     {
48         // disallow instantiation
49
}
50     
51         private static Object JavaDoc []
52     convert( Object JavaDoc simpleArray )
53     {
54         final String JavaDoc className = simpleArray.getClass().getName();
55         
56         //final String memberClassName = ClassUtil.getArrayMemberClassName( className );
57

58         final Class JavaDoc theClass = ClassUtil.getArrayElementClass( simpleArray.getClass() );
59         
60         final int numItems = Array.getLength( simpleArray );
61         
62         final Class JavaDoc elementClass = ClassUtil.PrimitiveClassToObjectClass( theClass );
63         
64         final Object JavaDoc [] result = (Object JavaDoc [])Array.newInstance( elementClass, numItems );
65         
66         for( int i = 0; i < numItems; ++i )
67         {
68             result[ i ] = Array.get( simpleArray, i );
69         }
70         
71         return( result );
72     }
73     
74         public static Object JavaDoc []
75     toAppropriateType( Object JavaDoc array)
76     {
77         return( (Object JavaDoc [])convert( array ) );
78     }
79     
80     
81         public static Boolean JavaDoc []
82     toBooleans( boolean [] array )
83     {
84         return( (Boolean JavaDoc [])convert( array ) );
85     }
86     
87         public static Character JavaDoc []
88     toCharacters( char [] array )
89     {
90         return( (Character JavaDoc [])convert( array ) );
91     }
92     
93         public static Byte JavaDoc []
94     toBytes( byte [] array )
95     {
96         return( (Byte JavaDoc [])convert( array ) );
97     }
98     
99         public static Short JavaDoc []
100     toShorts( short [] array )
101     {
102         return( (Short JavaDoc [])convert( array ) );
103     }
104     
105         public static Integer JavaDoc []
106     toIntegers( int [] array )
107     {
108         return( (Integer JavaDoc [])convert( array ) );
109     }
110     
111         public static Long JavaDoc []
112     toLongs( long [] array )
113     {
114         return( (Long JavaDoc [])convert( array ) );
115     }
116     
117         public static Float JavaDoc []
118     toFloats( float [] array )
119     {
120         return( (Float JavaDoc [])convert( array ) );
121     }
122     
123         public static Double JavaDoc []
124     toDoubles( double [] array )
125     {
126         return( (Double JavaDoc [])convert( array ) );
127     }
128     
129     
130         public static Object JavaDoc []
131     createObjectArrayType( final Class JavaDoc elementType, final int size )
132         throws Exception JavaDoc
133     {
134         final Object JavaDoc [] result = (Object JavaDoc []) Array.newInstance( elementType, size );
135         
136         return( result );
137     }
138     
139         
140         public static java.util.Set JavaDoc
141     toSet( Object JavaDoc [] array )
142     {
143         java.util.Set JavaDoc theSet = null;
144         if ( array.length == 0 )
145         {
146             theSet = java.util.Collections.EMPTY_SET;
147         }
148         else if ( array.length == 1 )
149         {
150             theSet = java.util.Collections.singleton( array[ 0 ] );
151         }
152         else
153         {
154             theSet = new java.util.HashSet JavaDoc();
155             for( int i = 0; i < array.length; ++i )
156             {
157                 theSet.add( array[ i ] );
158             }
159         }
160         return( theSet );
161     }
162     
163     
164         public static Object JavaDoc []
165     setToArray( final java.util.Set JavaDoc s )
166     {
167         final java.util.Iterator JavaDoc iter = s.iterator();
168         
169         final Object JavaDoc [] out = new Object JavaDoc [ s.size() ];
170         
171         return( setToArray( s, out ) );
172     }
173     
174         public static Object JavaDoc []
175     setToArray( final java.util.Set JavaDoc s, Object JavaDoc [] out )
176     {
177         final java.util.Iterator JavaDoc iter = s.iterator();
178         
179         if ( out.length != s.size() )
180         {
181             throw new IllegalArgumentException JavaDoc();
182         }
183         
184         int i = 0;
185         while ( iter.hasNext() )
186         {
187             out[ i ] = iter.next();
188             ++i;
189         }
190         
191         return( out );
192     }
193 }
194
195
Popular Tags