KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > hibernate > util > ArrayHelper


1 //$Id: ArrayHelper.java,v 1.15 2005/07/19 18:17:15 oneovthafew Exp $
2
package org.hibernate.util;
3 import java.lang.reflect.Array JavaDoc;
4 import java.util.ArrayList JavaDoc;
5 import java.util.Arrays JavaDoc;
6 import java.util.Collection JavaDoc;
7 import java.util.Iterator JavaDoc;
8 import java.util.List JavaDoc;
9
10 import org.hibernate.LockMode;
11 import org.hibernate.type.Type;
12
13 public final class ArrayHelper {
14     
15     /*public static boolean contains(Object[] array, Object object) {
16         for ( int i=0; i<array.length; i++ ) {
17             if ( array[i].equals(object) ) return true;
18         }
19         return false;
20     }*/

21     
22     public static int indexOf(Object JavaDoc[] array, Object JavaDoc 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     /*public static Object[] clone(Class elementClass, Object[] array) {
30         Object[] result = (Object[]) Array.newInstance( elementClass, array.length );
31         System.arraycopy(array, 0, result, 0, array.length);
32         return result;
33     }*/

34
35     public static String JavaDoc[] toStringArray(Object JavaDoc[] objects) {
36         int length=objects.length;
37         String JavaDoc[] result = new String JavaDoc[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 JavaDoc[] fillArray(String JavaDoc value, int length) {
45         String JavaDoc[] result = new String JavaDoc[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 JavaDoc[] toStringArray(Collection JavaDoc coll) {
63         return (String JavaDoc[]) coll.toArray(EMPTY_STRING_ARRAY);
64     }
65     
66     public static String JavaDoc[][] to2DStringArray(Collection JavaDoc coll) {
67         return (String JavaDoc[][]) coll.toArray( new String JavaDoc[ coll.size() ][] );
68     }
69     
70     public static int[][] to2DIntArray(Collection JavaDoc coll) {
71         return (int[][]) coll.toArray( new int[ coll.size() ][] );
72     }
73     
74     public static Type[] toTypeArray(Collection JavaDoc coll) {
75         return (Type[]) coll.toArray(EMPTY_TYPE_ARRAY);
76     }
77
78     public static int[] toIntArray(Collection JavaDoc coll) {
79         Iterator JavaDoc iter = coll.iterator();
80         int[] arr = new int[ coll.size() ];
81         int i=0;
82         while( iter.hasNext() ) {
83             arr[i++] = ( (Integer JavaDoc) iter.next() ).intValue();
84         }
85         return arr;
86     }
87
88     public static boolean[] toBooleanArray(Collection JavaDoc coll) {
89         Iterator JavaDoc iter = coll.iterator();
90         boolean[] arr = new boolean[ coll.size() ];
91         int i=0;
92         while( iter.hasNext() ) {
93             arr[i++] = ( (Boolean JavaDoc) iter.next() ).booleanValue();
94         }
95         return arr;
96     }
97
98     public static Object JavaDoc[] typecast(Object JavaDoc[] array, Object JavaDoc[] to) {
99         return java.util.Arrays.asList(array).toArray(to);
100     }
101
102     //Arrays.asList doesn't do primitive arrays
103
public static List JavaDoc toList(Object JavaDoc array) {
104         if ( array instanceof Object JavaDoc[] ) return Arrays.asList( (Object JavaDoc[]) array ); //faster?
105
int size = Array.getLength(array);
106         ArrayList JavaDoc list = new ArrayList JavaDoc(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 JavaDoc[] slice(String JavaDoc[] strings, int begin, int length) {
114         String JavaDoc[] result = new String JavaDoc[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 JavaDoc[] slice(Object JavaDoc[] objects, int begin, int length) {
122         Object JavaDoc[] result = new Object JavaDoc[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 JavaDoc toList(Iterator JavaDoc iter) {
130         List JavaDoc list = new ArrayList JavaDoc();
131         while ( iter.hasNext() ) {
132             list.add( iter.next() );
133         }
134         return list;
135     }
136
137     public static String JavaDoc[] join(String JavaDoc[] x, String JavaDoc[] y) {
138         String JavaDoc[] result = new String JavaDoc[ 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 JavaDoc[] join(String JavaDoc[] x, String JavaDoc[] y, boolean[] use) {
145         String JavaDoc[] result = new String JavaDoc[ 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 JavaDoc toString( Object JavaDoc[] array ) {
167         StringBuffer JavaDoc sb = new StringBuffer JavaDoc();
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     /*public static int countFalse(boolean[] array) {
200         int result=0;
201         for ( int i=0; i<array.length; i++ ) {
202             if ( !array[i] ) result++;
203         }
204         return result;
205     }*/

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 JavaDoc collection, Object JavaDoc[] array) {
215         for ( int i=0; i<array.length; i++ ) {
216             collection.add( array[i] );
217         }
218     }
219
220     public static final String JavaDoc[] EMPTY_STRING_ARRAY = {};
221     public static final int[] EMPTY_INT_ARRAY = {};
222     public static final boolean[] EMPTY_BOOLEAN_ARRAY = {};
223     public static final Class JavaDoc[] EMPTY_CLASS_ARRAY = {};
224     public static final Object JavaDoc[] 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; //allow 9,8,7,6,5,4,3,2,1
246
}
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     /**
259      * calculate the array hash (only the first level)
260      */

261     public static int hash(Object JavaDoc[] 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     /**
271      * calculate the array hash (only the first level)
272      */

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     /**
283      * calculate the array hash (only the first level)
284      */

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     /**
299      * Compare 2 arrays only at the first level
300      */

301     public static boolean isEquals(Object JavaDoc[] o1, Object JavaDoc[] 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     /**
313      * Compare 2 arrays only at the first level
314      */

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     /**
327      * Compare 2 arrays only at the first level
328      */

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