2
* ProGuard -- shrinking, optimization, obfuscation, and preverification
5
* Copyright (c) 2002-2013 Eric Lafortune (eric@graphics.cornell.edu)
7
* This program is free software; you can redistribute it and/or modify it
8
* under the terms of the GNU General Public License as published by the Free
9
* Software Foundation; either version 2 of the License, or (at your option)
12
* This program is distributed in the hope that it will be useful, but WITHOUT
13
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
14
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
17
* You should have received a copy of the GNU General Public License along
18
* with this program; if not, write to the Free Software Foundation, Inc.,
19
* 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
21
package proguard.util;
23
import java.lang.reflect.Array;
24
import java.util.Arrays;
27
* This class contains utility methods operating on arrays.
29
public class ArrayUtil
32
* Returns whether the elements of the two given arrays are the same.
33
* @param array1 the first array.
34
* @param array2 the second array.
35
* @param size the size of the arrays to be checked.
36
* @return whether the elements are the same.
38
public static boolean equal(byte[] array1, byte[] array2, int size)
40
for (int index = 0; index < size; index++)
42
if (array1[index] != array2[index])
53
* Returns whether the elements of the two given arrays are the same.
54
* @param array1 the first array.
55
* @param array2 the second array.
56
* @param size the size of the arrays to be checked.
57
* @return whether the elements are the same.
59
public static boolean equal(short[] array1, short[] array2, int size)
61
for (int index = 0; index < size; index++)
63
if (array1[index] != array2[index])
74
* Returns whether the elements of the two given arrays are the same.
75
* @param array1 the first array.
76
* @param array2 the second array.
77
* @param size the size of the arrays to be checked.
78
* @return whether the elements are the same.
80
public static boolean equal(int[] array1, int[] array2, int size)
82
for (int index = 0; index < size; index++)
84
if (array1[index] != array2[index])
95
* Returns whether the elements of the two given arrays are the same.
96
* @param array1 the first array.
97
* @param array2 the second array.
98
* @param size the size of the arrays to be checked.
99
* @return whether the elements are the same.
101
public static boolean equal(Object[] array1, Object[] array2, int size)
103
for (int index = 0; index < size; index++)
105
if (!array1[index].equals(array2[index]))
116
* Returns whether the elements of the two given arrays are the same, or
117
* both arrays are null.
118
* @param array1 the first array.
119
* @param array2 the second array.
120
* @return whether the elements are the same.
122
public static boolean equalOrNull(Object[] array1, Object[] array2)
124
return array1 == null ? array2 == null :
125
equalOrNull(array1, array2, array1.length);
130
* Returns whether the elements of the two given arrays are the same, or
131
* both arrays are null.
132
* @param array1 the first array.
133
* @param array2 the second array.
134
* @param size the size of the arrays to be checked.
135
* @return whether the elements are the same.
137
public static boolean equalOrNull(Object[] array1, Object[] array2, int size)
139
return array1 == null ? array2 == null :
141
equalOrNull(array1, array2, size);
146
* Returns a hash code for the elements of the given array.
147
* @param array the array.
148
* @param size the size of the array to be taken into account.
149
* @return a hash code.
151
public static int hashCode(byte[] array, int size)
155
for (int index = 0; index < size; index++)
157
hashCode ^= array[index];
165
* Returns a hash code for the elements of the given array.
166
* @param array the array.
167
* @param size the size of the array to be taken into account.
168
* @return a hash code.
170
public static int hashCode(short[] array, int size)
174
for (int index = 0; index < size; index++)
176
hashCode ^= array[index];
184
* Returns a hash code for the elements of the given array.
185
* @param array the array.
186
* @param size the size of the array to be taken into account.
187
* @return a hash code.
189
public static int hashCode(int[] array, int size)
193
for (int index = 0; index < size; index++)
195
hashCode ^= array[index];
203
* Returns a hash code for the elements of the given array.
204
* @param array the array.
205
* @param size the size of the array to be taken into account.
206
* @return a hash code.
208
public static int hashCode(Object[] array, int size)
212
for (int index = 0; index < size; index++)
214
hashCode ^= array[index].hashCode();
222
* Returns a hash code for the elements of the given array, or 0 if it is
224
* @param array the array.
225
* @return a hash code.
227
public static int hashCodeOrNull(Object[] array)
229
return array == null ? 0 : hashCode(array, array.length);
234
* Returns a hash code for the elements of the given array, or 0 if it is
236
* @param array the array.
237
* @param size the size of the array to be taken into account.
238
* @return a hash code.
240
public static int hashCodeOrNull(Object[] array, int size)
242
return array == null ? 0 : hashCode(array, size);
247
* Compares the elements of the two given arrays.
248
* @param array1 the first array.
249
* @param size1 the size of the first array.
250
* @param array2 the second array.
251
* @param size2 the size of the second array.
252
* @return 0 if all elements are the same,
253
* -1 if the first different element in the first array is smaller
254
* than the corresponding element in the second array,
255
* or 1 if it is larger.
257
public static int compare(byte[] array1, int size1,
258
byte[] array2, int size2)
260
int minSize = Math.min(size1, size2);
262
for (int index = 0; index < minSize; index++)
264
if (array1[index] < array2[index])
268
else if (array1[index] > array2[index])
274
return size1 < size2 ? -1 :
281
* Compares the elements of the two given arrays.
282
* @param array1 the first array.
283
* @param size1 the size of the first array.
284
* @param array2 the second array.
285
* @param size2 the size of the second array.
286
* @return 0 if all elements are the same,
287
* -1 if the first different element in the first array is smaller
288
* than the corresponding element in the second array,
289
* or 1 if it is larger.
291
public static int compare(short[] array1, int size1,
292
short[] array2, int size2)
294
int minSize = Math.min(size1, size2);
296
for (int index = 0; index < minSize; index++)
298
if (array1[index] < array2[index])
302
else if (array1[index] > array2[index])
308
return size1 < size2 ? -1 :
315
* Compares the elements of the two given arrays.
316
* @param array1 the first array.
317
* @param size1 the size of the first array.
318
* @param array2 the second array.
319
* @param size2 the size of the second array.
320
* @return 0 if all elements are the same,
321
* -1 if the first different element in the first array is smaller
322
* than the corresponding element in the second array,
323
* or 1 if it is larger.
325
public static int compare(int[] array1, int size1,
326
int[] array2, int size2)
328
int minSize = Math.min(size1, size2);
330
for (int index = 0; index < minSize; index++)
332
if (array1[index] < array2[index])
336
else if (array1[index] > array2[index])
342
return size1 < size2 ? -1 :
349
* Compares the elements of the two given arrays.
350
* @param array1 the first array.
351
* @param size1 the size of the first array.
352
* @param array2 the second array.
353
* @param size2 the size of the second array.
354
* @return 0 if all elements are the same,
355
* -1 if the first different element in the first array is smaller
356
* than the corresponding element in the second array,
357
* or 1 if it is larger.
359
public static int compare(Comparable[] array1, int size1,
360
Comparable[] array2, int size2)
362
int minSize = Math.min(size1, size2);
364
for (int index = 0; index < minSize; index++)
366
int comparison = ObjectUtil.compare(array1[index], array2[index]);
373
return size1 < size2 ? -1 :
380
* Ensures the given array has a given size.
381
* @param array the array.
382
* @param size the target size of the array.
383
* @return the original array, or a copy if it had to be extended.
385
public static boolean[] extendArray(boolean[] array, int size)
387
// Reuse the existing array if possible.
388
if (array.length >= size)
393
// Otherwise create and initialize a new array.
394
boolean[] newArray = new boolean[size];
396
System.arraycopy(array, 0,
405
* Ensures the given array has a given size.
406
* @param array the array.
407
* @param size the target size of the array.
408
* @param initialValue the initial value of the elements.
409
* @return the original array, or a copy if it had to be
412
public static boolean[] ensureArraySize(boolean[] array,
414
boolean initialValue)
416
// Is the existing array large enough?
417
if (array.length >= size)
419
// Reinitialize the existing array.
420
Arrays.fill(array, 0, size, initialValue);
424
// Otherwise create and initialize a new array.
425
array = new boolean[size];
429
Arrays.fill(array, 0, size, initialValue);
438
* Adds the given element to the given array.
439
* The array is extended if necessary.
440
* @param array the array.
441
* @param size the original size of the array.
442
* @param element the element to be added.
443
* @return the original array, or a copy if it had to be extended.
445
public static byte[] add(byte[] array, int size, byte element)
447
array = extendArray(array, size + 1);
449
array[size] = element;
456
* Inserts the given element in the given array.
457
* The array is extended if necessary.
458
* @param array the array.
459
* @param size the original size of the array.
460
* @param index the index at which the element is to be added.
461
* @param element the element to be added.
462
* @return the original array, or a copy if it had to be extended.
464
public static byte[] insert(byte[] array, int size, int index, byte element)
466
array = extendArray(array, size + 1);
468
// Move the last part.
469
System.arraycopy(array, index,
473
array[index] = element;
480
* Removes the specified element from the given array.
481
* @param array the array.
482
* @param size the original size of the array.
483
* @param index the index of the element to be removed.
485
public static void remove(byte[] array, int size, int index)
487
System.arraycopy(array, index + 1,
489
array.length - index - 1);
496
* Ensures the given array has a given size.
497
* @param array the array.
498
* @param size the target size of the array.
499
* @return the original array, or a copy if it had to be extended.
501
public static byte[] extendArray(byte[] array, int size)
503
// Reuse the existing array if possible.
504
if (array.length >= size)
509
// Otherwise create and initialize a new array.
510
byte[] newArray = new byte[size];
512
System.arraycopy(array, 0,
521
* Ensures the given array has a given size.
522
* @param array the array.
523
* @param size the target size of the array.
524
* @param initialValue the initial value of the elements.
525
* @return the original array, or a copy if it had to be
528
public static byte[] ensureArraySize(byte[] array,
532
// Is the existing array large enough?
533
if (array.length >= size)
535
// Reinitialize the existing array.
536
Arrays.fill(array, 0, size, initialValue);
540
// Otherwise create and initialize a new array.
541
array = new byte[size];
543
if (initialValue != 0)
545
Arrays.fill(array, 0, size, initialValue);
554
* Adds the given element to the given array.
555
* The array is extended if necessary.
556
* @param array the array.
557
* @param size the original size of the array.
558
* @param element the element to be added.
559
* @return the original array, or a copy if it had to be extended.
561
public static short[] add(short[] array, int size, short element)
563
array = extendArray(array, size + 1);
565
array[size] = element;
572
* Inserts the given element in the given array.
573
* The array is extended if necessary.
574
* @param array the array.
575
* @param size the original size of the array.
576
* @param index the index at which the element is to be added.
577
* @param element the element to be added.
578
* @return the original array, or a copy if it had to be extended.
580
public static short[] insert(short[] array, int size, int index, short element)
582
array = extendArray(array, size + 1);
584
// Move the last part.
585
System.arraycopy(array, index,
589
array[index] = element;
596
* Removes the specified element from the given array.
597
* @param array the array.
598
* @param size the original size of the array.
599
* @param index the index of the element to be removed.
601
public static void remove(short[] array, int size, int index)
603
System.arraycopy(array, index + 1,
605
array.length - index - 1);
612
* Ensures the given array has a given size.
613
* @param array the array.
614
* @param size the target size of the array.
615
* @return the original array, or a copy if it had to be extended.
617
public static short[] extendArray(short[] array, int size)
619
// Reuse the existing array if possible.
620
if (array.length >= size)
625
// Otherwise create and initialize a new array.
626
short[] newArray = new short[size];
628
System.arraycopy(array, 0,
637
* Ensures the given array has a given size.
638
* @param array the array.
639
* @param size the target size of the array.
640
* @param initialValue the initial value of the elements.
641
* @return the original array, or a copy if it had to be
644
public static short[] ensureArraySize(short[] array,
648
// Is the existing array large enough?
649
if (array.length >= size)
651
// Reinitialize the existing array.
652
Arrays.fill(array, 0, size, initialValue);
656
// Otherwise create and initialize a new array.
657
array = new short[size];
659
if (initialValue != 0)
661
Arrays.fill(array, 0, size, initialValue);
670
* Adds the given element to the given array.
671
* The array is extended if necessary.
672
* @param array the array.
673
* @param size the original size of the array.
674
* @param element the element to be added.
675
* @return the original array, or a copy if it had to be extended.
677
public static int[] add(int[] array, int size, int element)
679
array = extendArray(array, size + 1);
681
array[size] = element;
688
* Inserts the given element in the given array.
689
* The array is extended if necessary.
690
* @param array the array.
691
* @param size the original size of the array.
692
* @param index the index at which the element is to be added.
693
* @param element the element to be added.
694
* @return the original array, or a copy if it had to be extended.
696
public static int[] insert(int[] array, int size, int index, int element)
698
array = extendArray(array, size + 1);
700
// Move the last part.
701
System.arraycopy(array, index,
705
array[index] = element;
712
* Removes the specified element from the given array.
713
* @param array the array.
714
* @param size the original size of the array.
715
* @param index the index of the element to be removed.
717
public static void remove(int[] array, int size, int index)
719
System.arraycopy(array, index + 1,
721
array.length - index - 1);
728
* Ensures the given array has a given size.
729
* @param array the array.
730
* @param size the target size of the array.
731
* @return the original array, or a copy if it had to be extended.
733
public static int[] extendArray(int[] array, int size)
735
// Reuse the existing array if possible.
736
if (array.length >= size)
741
// Otherwise create and initialize a new array.
742
int[] newArray = new int[size];
744
System.arraycopy(array, 0,
753
* Ensures the given array has a given size.
754
* @param array the array.
755
* @param size the target size of the array.
756
* @param initialValue the initial value of the elements.
757
* @return the original array, or a copy if it had to be
760
public static int[] ensureArraySize(int[] array,
764
// Is the existing array large enough?
765
if (array.length >= size)
767
// Reinitialize the existing array.
768
Arrays.fill(array, 0, size, initialValue);
772
// Otherwise create and initialize a new array.
773
array = new int[size];
775
if (initialValue != 0)
777
Arrays.fill(array, 0, size, initialValue);
786
* Adds the given element to the given array.
787
* The array is extended if necessary.
788
* @param array the array.
789
* @param size the original size of the array.
790
* @param element the element to be added.
791
* @return the original array, or a copy if it had to be extended.
793
public static long[] add(long[] array, int size, long element)
795
array = extendArray(array, size + 1);
797
array[size] = element;
804
* Inserts the given element in the given array.
805
* The array is extended if necessary.
806
* @param array the array.
807
* @param size the original size of the array.
808
* @param index the index at which the element is to be added.
809
* @param element the element to be added.
810
* @return the original array, or a copy if it had to be extended.
812
public static long[] insert(long[] array, int size, int index, long element)
814
array = extendArray(array, size + 1);
816
// Move the last part.
817
System.arraycopy(array, index,
821
array[index] = element;
828
* Removes the specified element from the given array.
829
* @param array the array.
830
* @param size the original size of the array.
831
* @param index the index of the element to be removed.
833
public static void remove(long[] array, int size, int index)
835
System.arraycopy(array, index + 1,
837
array.length - index - 1);
844
* Ensures the given array has a given size.
845
* @param array the array.
846
* @param size the target size of the array.
847
* @return the original array, or a copy if it had to be extended.
849
public static long[] extendArray(long[] array, int size)
851
// Reuse the existing array if possible.
852
if (array.length >= size)
857
// Otherwise create and initialize a new array.
858
long[] newArray = new long[size];
860
System.arraycopy(array, 0,
869
* Ensures the given array has a given size.
870
* @param array the array.
871
* @param size the target size of the array.
872
* @param initialValue the initial value of the elements.
873
* @return the original array, or a copy if it had to be
876
public static long[] ensureArraySize(long[] array,
880
// Is the existing array large enough?
881
if (array.length >= size)
883
// Reinitialize the existing array.
884
Arrays.fill(array, 0, size, initialValue);
888
// Otherwise create and initialize a new array.
889
array = new long[size];
891
if (initialValue != 0L)
893
Arrays.fill(array, 0, size, initialValue);
902
* Adds the given element to the given array.
903
* The array is extended if necessary.
904
* @param array the array.
905
* @param size the original size of the array.
906
* @param element the element to be added.
907
* @return the original array, or a copy if it had to be extended.
909
public static Object[] add(Object[] array, int size, Object element)
911
array = extendArray(array, size + 1);
913
array[size] = element;
920
* Inserts the given element in the given array.
921
* The array is extended if necessary.
922
* @param array the array.
923
* @param size the original size of the array.
924
* @param index the index at which the element is to be added.
925
* @param element the element to be added.
926
* @return the original array, or a copy if it had to be extended.
928
public static Object[] insert(Object[] array, int size, int index, Object element)
930
array = extendArray(array, size + 1);
932
// Move the last part.
933
System.arraycopy(array, index,
937
array[index] = element;
944
* Removes the specified element from the given array.
945
* @param array the array.
946
* @param size the original size of the array.
947
* @param index the index of the element to be removed.
949
public static void remove(Object[] array, int size, int index)
951
System.arraycopy(array, index + 1,
953
array.length - index - 1);
955
array[size - 1] = null;
960
* Ensures the given array has a given size.
961
* @param array the array.
962
* @param size the target size of the array.
963
* @return the original array, or a copy if it had to be extended.
965
public static Object[] extendArray(Object[] array, int size)
967
// Reuse the existing array if possible.
968
if (array.length >= size)
973
// Otherwise create and initialize a new array.
974
Object[] newArray = (Object[])Array.newInstance(array.getClass().getComponentType(), size);
976
System.arraycopy(array, 0,
985
* Ensures the given array has a given size.
986
* @param array the array.
987
* @param size the target size of the array.
988
* @param initialValue the initial value of the elements.
989
* @return the original array, or a copy if it had to be
992
public static Object[] ensureArraySize(Object[] array,
996
// Is the existing array large enough?
997
if (array.length >= size)
999
// Reinitialize the existing array.
1000
Arrays.fill(array, 0, size, initialValue);
1004
// Otherwise create and initialize a new array.
1005
array = (Object[])Array.newInstance(array.getClass().getComponentType(), size);
1007
if (initialValue != null)
1009
Arrays.fill(array, 0, size, initialValue);