1 23 package com.sun.appserv.management.util.misc; 24 25 import java.util.Set ; 26 import java.util.HashSet ; 27 import java.util.Collections ; 28 29 import java.lang.reflect.Array ; 30 31 import com.sun.appserv.management.util.misc.ClassUtil; 32 33 38 public final class ArrayConversion 39 { 40 private 41 ArrayConversion( ) 42 { 43 } 45 46 private static Object [] 47 convert( Object simpleArray ) 48 { 49 if ( ! ClassUtil.objectIsPrimitiveArray( simpleArray ) ) 50 { 51 throw new IllegalArgumentException (); 52 } 53 54 final String className = simpleArray.getClass().getName(); 55 56 final Class theClass = ClassUtil.getArrayElementClass( simpleArray.getClass() ); 57 58 final int numItems = Array.getLength( simpleArray ); 59 60 final Class elementClass = ClassUtil.PrimitiveClassToObjectClass( theClass ); 61 62 final Object [] result = (Object [])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 78 public static Object [] 79 toAppropriateType( Object array) 80 { 81 return( (Object [])convert( array ) ); 82 } 83 84 85 public static Boolean [] 86 toBooleans( boolean [] array ) 87 { 88 return( (Boolean [])convert( array ) ); 89 } 90 91 public static Character [] 92 toCharacters( char [] array ) 93 { 94 return( (Character [])convert( array ) ); 95 } 96 97 public static Byte [] 98 toBytes( byte [] array ) 99 { 100 return( (Byte [])convert( array ) ); 101 } 102 103 public static Short [] 104 toShorts( short [] array ) 105 { 106 return( (Short [])convert( array ) ); 107 } 108 109 public static Integer [] 110 toIntegers( int [] array ) 111 { 112 return( (Integer [])convert( array ) ); 113 } 114 115 public static Long [] 116 toLongs( long [] array ) 117 { 118 return( (Long [])convert( array ) ); 119 } 120 121 public static Float [] 122 toFloats( float [] array ) 123 { 124 return( (Float [])convert( array ) ); 125 } 126 127 public static Double [] 128 toDoubles( double [] array ) 129 { 130 return( (Double [])convert( array ) ); 131 } 132 133 134 140 public static Object [] 141 createObjectArrayType( final Class elementType, final int size ) 142 { 143 final Object [] result = (Object []) Array.newInstance( elementType, size ); 144 145 return( result ); 146 } 147 148 public static Object [] 149 subArray( final Object [] in, int start, int end ) 150 { 151 final int count = 1 + (end - start); 152 final Object [] result = (Object []) 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 <T> 166 toSet( T[] array ) 167 { 168 Set <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 <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 192 public static boolean 193 hasIdenticalElementClasses( final Object [] a ) 194 { 195 boolean isUniform = true; 196 197 if ( a.length > 0 ) 198 { 199 final Class 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 221 public static Object [] 222 specializeArray( final Object [] a ) 223 { 224 Object [] result = a; 225 226 if ( hasIdenticalElementClasses( a ) && 227 a.length != 0 && 228 a.getClass() == Object [].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 245 public static Object [] 246 setToArray( final Set <?> s, boolean specialize ) 247 { 248 Object [] result = setToArray( s ); 249 250 if ( specialize && result.length != 0) 251 { 252 result = specializeArray( result ); 253 } 254 255 return( result ); 256 } 257 258 263 public static Object [] 264 setToArray( final Set <?> s ) 265 { 266 final Object [] out = new Object [ s.size() ]; 267 268 setToArray( s, out ); 269 270 return( out ); 271 } 272 273 274 280 public static Object [] 281 setToArray( final Set <?> s, Object [] out ) 282 { 283 if ( out.length != s.size() ) 284 { 285 throw new IllegalArgumentException (); 286 } 287 288 int i = 0; 289 for( final Object o : s ) 290 { 291 out[ i ] = o; 292 ++i; 293 } 294 295 return( out ); 296 } 297 298 299 public static <T> Set <T> 300 arrayToSet( final T [] names ) 301 { 302 final Set <T> set = new HashSet <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 |