1 23 24 28 29 34 35 package com.sun.enterprise.admin.util; 36 37 import java.lang.reflect.Array ; 38 39 43 public final class ArrayConversion 44 { 45 private 46 ArrayConversion( ) 47 { 48 } 50 51 private static Object [] 52 convert( Object simpleArray ) 53 { 54 final String className = simpleArray.getClass().getName(); 55 56 58 final Class theClass = ClassUtil.getArrayElementClass( simpleArray.getClass() ); 59 60 final int numItems = Array.getLength( simpleArray ); 61 62 final Class elementClass = ClassUtil.PrimitiveClassToObjectClass( theClass ); 63 64 final Object [] result = (Object [])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 [] 75 toAppropriateType( Object array) 76 { 77 return( (Object [])convert( array ) ); 78 } 79 80 81 public static Boolean [] 82 toBooleans( boolean [] array ) 83 { 84 return( (Boolean [])convert( array ) ); 85 } 86 87 public static Character [] 88 toCharacters( char [] array ) 89 { 90 return( (Character [])convert( array ) ); 91 } 92 93 public static Byte [] 94 toBytes( byte [] array ) 95 { 96 return( (Byte [])convert( array ) ); 97 } 98 99 public static Short [] 100 toShorts( short [] array ) 101 { 102 return( (Short [])convert( array ) ); 103 } 104 105 public static Integer [] 106 toIntegers( int [] array ) 107 { 108 return( (Integer [])convert( array ) ); 109 } 110 111 public static Long [] 112 toLongs( long [] array ) 113 { 114 return( (Long [])convert( array ) ); 115 } 116 117 public static Float [] 118 toFloats( float [] array ) 119 { 120 return( (Float [])convert( array ) ); 121 } 122 123 public static Double [] 124 toDoubles( double [] array ) 125 { 126 return( (Double [])convert( array ) ); 127 } 128 129 130 public static Object [] 131 createObjectArrayType( final Class elementType, final int size ) 132 throws Exception 133 { 134 final Object [] result = (Object []) Array.newInstance( elementType, size ); 135 136 return( result ); 137 } 138 139 140 public static java.util.Set 141 toSet( Object [] array ) 142 { 143 java.util.Set 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 (); 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 [] 165 setToArray( final java.util.Set s ) 166 { 167 final java.util.Iterator iter = s.iterator(); 168 169 final Object [] out = new Object [ s.size() ]; 170 171 return( setToArray( s, out ) ); 172 } 173 174 public static Object [] 175 setToArray( final java.util.Set s, Object [] out ) 176 { 177 final java.util.Iterator iter = s.iterator(); 178 179 if ( out.length != s.size() ) 180 { 181 throw new IllegalArgumentException (); 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 |