1 package org.hibernate.util; 3 import java.lang.reflect.Array ; 4 import java.util.ArrayList ; 5 import java.util.Arrays ; 6 import java.util.Collection ; 7 import java.util.Iterator ; 8 import java.util.List ; 9 10 import org.hibernate.LockMode; 11 import org.hibernate.type.Type; 12 13 public final class ArrayHelper { 14 15 21 22 public static int indexOf(Object [] array, Object object) { 23 for ( int i=0; i<array.length; i++ ) { 24 if ( array[i].equals(object) ) return i; 25 } 26 return -1; 27 } 28 29 34 35 public static String [] toStringArray(Object [] objects) { 36 int length=objects.length; 37 String [] result = new String [length]; 38 for (int i=0; i<length; i++) { 39 result[i] = objects[i].toString(); 40 } 41 return result; 42 } 43 44 public static String [] fillArray(String value, int length) { 45 String [] result = new String [length]; 46 Arrays.fill(result, value); 47 return result; 48 } 49 50 public static int[] fillArray(int value, int length) { 51 int[] result = new int[length]; 52 Arrays.fill(result, value); 53 return result; 54 } 55 56 public static LockMode[] fillArray(LockMode lockMode, int length) { 57 LockMode[] array = new LockMode[length]; 58 Arrays.fill(array, lockMode); 59 return array; 60 } 61 62 public static String [] toStringArray(Collection coll) { 63 return (String []) coll.toArray(EMPTY_STRING_ARRAY); 64 } 65 66 public static String [][] to2DStringArray(Collection coll) { 67 return (String [][]) coll.toArray( new String [ coll.size() ][] ); 68 } 69 70 public static int[][] to2DIntArray(Collection coll) { 71 return (int[][]) coll.toArray( new int[ coll.size() ][] ); 72 } 73 74 public static Type[] toTypeArray(Collection coll) { 75 return (Type[]) coll.toArray(EMPTY_TYPE_ARRAY); 76 } 77 78 public static int[] toIntArray(Collection coll) { 79 Iterator iter = coll.iterator(); 80 int[] arr = new int[ coll.size() ]; 81 int i=0; 82 while( iter.hasNext() ) { 83 arr[i++] = ( (Integer ) iter.next() ).intValue(); 84 } 85 return arr; 86 } 87 88 public static boolean[] toBooleanArray(Collection coll) { 89 Iterator iter = coll.iterator(); 90 boolean[] arr = new boolean[ coll.size() ]; 91 int i=0; 92 while( iter.hasNext() ) { 93 arr[i++] = ( (Boolean ) iter.next() ).booleanValue(); 94 } 95 return arr; 96 } 97 98 public static Object [] typecast(Object [] array, Object [] to) { 99 return java.util.Arrays.asList(array).toArray(to); 100 } 101 102 public static List toList(Object array) { 104 if ( array instanceof Object [] ) return Arrays.asList( (Object []) array ); int size = Array.getLength(array); 106 ArrayList list = new ArrayList (size); 107 for (int i=0; i<size; i++) { 108 list.add( Array.get(array, i) ); 109 } 110 return list; 111 } 112 113 public static String [] slice(String [] strings, int begin, int length) { 114 String [] result = new String [length]; 115 for ( int i=0; i<length; i++ ) { 116 result[i] = strings[begin+i]; 117 } 118 return result; 119 } 120 121 public static Object [] slice(Object [] objects, int begin, int length) { 122 Object [] result = new Object [length]; 123 for ( int i=0; i<length; i++ ) { 124 result[i] = objects[begin+i]; 125 } 126 return result; 127 } 128 129 public static List toList(Iterator iter) { 130 List list = new ArrayList (); 131 while ( iter.hasNext() ) { 132 list.add( iter.next() ); 133 } 134 return list; 135 } 136 137 public static String [] join(String [] x, String [] y) { 138 String [] result = new String [ x.length + y.length ]; 139 for ( int i=0; i<x.length; i++ ) result[i] = x[i]; 140 for ( int i=0; i<y.length; i++ ) result[i+x.length] = y[i]; 141 return result; 142 } 143 144 public static String [] join(String [] x, String [] y, boolean[] use) { 145 String [] result = new String [ x.length + countTrue(use) ]; 146 for ( int i=0; i<x.length; i++ ) result[i] = x[i]; 147 int k = x.length; 148 for ( int i=0; i<y.length; i++ ) { 149 if ( use[i] ) result[k++] = y[i]; 150 } 151 return result; 152 } 153 154 public static int[] join(int[] x, int[] y) { 155 int[] result = new int[ x.length + y.length ]; 156 for ( int i=0; i<x.length; i++ ) result[i] = x[i]; 157 for ( int i=0; i<y.length; i++ ) result[i+x.length] = y[i]; 158 return result; 159 } 160 161 public static final boolean[] TRUE = { true }; 162 public static final boolean[] FALSE = { false }; 163 164 private ArrayHelper() {} 165 166 public static String toString( Object [] array ) { 167 StringBuffer sb = new StringBuffer (); 168 sb.append("["); 169 for (int i = 0; i < array.length; i++) { 170 sb.append( array[i] ); 171 if( i<array.length-1 ) sb.append(","); 172 } 173 sb.append("]"); 174 return sb.toString(); 175 } 176 177 public static boolean isAllNegative(int[] array) { 178 for ( int i=0; i<array.length; i++ ) { 179 if ( array[i] >=0 ) return false; 180 } 181 return true; 182 } 183 184 public static boolean isAllTrue(boolean[] array) { 185 for ( int i=0; i<array.length; i++ ) { 186 if ( !array[i] ) return false; 187 } 188 return true; 189 } 190 191 public static int countTrue(boolean[] array) { 192 int result=0; 193 for ( int i=0; i<array.length; i++ ) { 194 if ( array[i] ) result++; 195 } 196 return result; 197 } 198 199 206 207 public static boolean isAllFalse(boolean[] array) { 208 for ( int i=0; i<array.length; i++ ) { 209 if ( array[i] ) return false; 210 } 211 return true; 212 } 213 214 public static void addAll(Collection collection, Object [] array) { 215 for ( int i=0; i<array.length; i++ ) { 216 collection.add( array[i] ); 217 } 218 } 219 220 public static final String [] EMPTY_STRING_ARRAY = {}; 221 public static final int[] EMPTY_INT_ARRAY = {}; 222 public static final boolean[] EMPTY_BOOLEAN_ARRAY = {}; 223 public static final Class [] EMPTY_CLASS_ARRAY = {}; 224 public static final Object [] EMPTY_OBJECT_ARRAY = {}; 225 public static final Type[] EMPTY_TYPE_ARRAY = {}; 226 227 public static int[] getBatchSizes(int maxBatchSize) { 228 int batchSize = maxBatchSize; 229 int n=1; 230 while ( batchSize>1 ) { 231 batchSize = getNextBatchSize(batchSize); 232 n++; 233 } 234 int[] result = new int[n]; 235 batchSize = maxBatchSize; 236 for ( int i=0; i<n; i++ ) { 237 result[i] = batchSize; 238 batchSize = getNextBatchSize(batchSize); 239 } 240 return result; 241 } 242 243 private static int getNextBatchSize(int batchSize) { 244 if (batchSize<=10) { 245 return batchSize-1; } 247 else if (batchSize/2 < 10) { 248 return 10; 249 } 250 else { 251 return batchSize / 2; 252 } 253 } 254 255 private static int SEED = 23; 256 private static int PRIME_NUMER = 37; 257 258 261 public static int hash(Object [] array) { 262 int length = array.length; 263 int seed = SEED; 264 for (int index = 0 ; index < length ; index++) { 265 seed = hash( seed, array[index] == null ? 0 : array[index].hashCode() ); 266 } 267 return seed; 268 } 269 270 273 public static int hash(char[] array) { 274 int length = array.length; 275 int seed = SEED; 276 for (int index = 0 ; index < length ; index++) { 277 seed = hash( seed, (int) array[index] ) ; 278 } 279 return seed; 280 } 281 282 285 public static int hash(byte[] bytes) { 286 int length = bytes.length; 287 int seed = SEED; 288 for (int index = 0 ; index < length ; index++) { 289 seed = hash( seed, (int) bytes[index] ) ; 290 } 291 return seed; 292 } 293 294 private static int hash(int seed, int i) { 295 return PRIME_NUMER * seed + i; 296 } 297 298 301 public static boolean isEquals(Object [] o1, Object [] o2) { 302 if (o1 == o2) return true; 303 if (o1 == null || o2 == null) return false; 304 int length = o1.length; 305 if (length != o2.length) return false; 306 for (int index = 0 ; index < length ; index++) { 307 if ( ! o1[index].equals( o2[index] ) ) return false; 308 } 309 return true; 310 } 311 312 315 public static boolean isEquals(char[] o1, char[] o2) { 316 if (o1 == o2) return true; 317 if (o1 == null || o2 == null) return false; 318 int length = o1.length; 319 if (length != o2.length) return false; 320 for (int index = 0 ; index < length ; index++) { 321 if ( ! ( o1[index] == o2[index] ) ) return false; 322 } 323 return true; 324 } 325 326 329 public static boolean isEquals(byte[] b1, byte[] b2) { 330 if (b1 == b2) return true; 331 if (b1 == null || b2 == null) return false; 332 int length = b1.length; 333 if (length != b2.length) return false; 334 for (int index = 0 ; index < length ; index++) { 335 if ( ! ( b1[index] == b2[index] ) ) return false; 336 } 337 return true; 338 } 339 } 340 341 342 343 344 345 346 | Popular Tags |