1 23 24 29 30 package com.sun.cli.util; 31 32 import java.lang.reflect.Array ; 33 34 38 public final class ArrayConversion 39 { 40 private 41 ArrayConversion( ) 42 { 43 } 45 46 private static Object [] 47 convert( Object simpleArray ) 48 { 49 final String className = simpleArray.getClass().getName(); 50 51 53 final Class theClass = ClassUtil.getArrayElementClass( simpleArray.getClass() ); 54 55 final int numItems = Array.getLength( simpleArray ); 56 57 final Class elementClass = ClassUtil.PrimitiveClassToObjectClass( theClass ); 58 59 final Object [] result = (Object [])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 [] 70 toAppropriateType( Object array) 71 { 72 return( (Object [])convert( array ) ); 73 } 74 75 76 public static Boolean [] 77 toBooleans( boolean [] array ) 78 { 79 return( (Boolean [])convert( array ) ); 80 } 81 82 public static Character [] 83 toCharacters( char [] array ) 84 { 85 return( (Character [])convert( array ) ); 86 } 87 88 public static Byte [] 89 toBytes( byte [] array ) 90 { 91 return( (Byte [])convert( array ) ); 92 } 93 94 public static Short [] 95 toShorts( short [] array ) 96 { 97 return( (Short [])convert( array ) ); 98 } 99 100 public static Integer [] 101 toIntegers( int [] array ) 102 { 103 return( (Integer [])convert( array ) ); 104 } 105 106 public static Long [] 107 toLongs( long [] array ) 108 { 109 return( (Long [])convert( array ) ); 110 } 111 112 public static Float [] 113 toFloats( float [] array ) 114 { 115 return( (Float [])convert( array ) ); 116 } 117 118 public static Double [] 119 toDoubles( double [] array ) 120 { 121 return( (Double [])convert( array ) ); 122 } 123 124 125 public static Object [] 126 createObjectArrayType( final Class elementType, final int size ) 127 throws Exception 128 { 129 final Object [] result = (Object []) Array.newInstance( elementType, size ); 130 131 return( result ); 132 } 133 134 135 136 137 public static java.util.Set 138 toSet( Object [] array ) 139 { 140 java.util.Set 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 (); 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 |