1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19 package org.apache.hadoop.hbase.util;
20
21 import java.util.ArrayList;
22 import java.util.Arrays;
23
24 public class ArrayUtils {
25
26 public static int length(byte[] a) {
27 if (a == null) {
28 return 0;
29 }
30 return a.length;
31 }
32
33 public static int length(long[] a) {
34 if (a == null) {
35 return 0;
36 }
37 return a.length;
38 }
39
40 public static int length(Object[] a) {
41 if (a == null) {
42 return 0;
43 }
44 return a.length;
45 }
46
47 public static boolean isEmpty(byte[] a) {
48 if (a == null) {
49 return true;
50 }
51 if (a.length == 0) {
52 return true;
53 }
54 return false;
55 }
56
57 public static boolean isEmpty(long[] a) {
58 if (a == null) {
59 return true;
60 }
61 if (a.length == 0) {
62 return true;
63 }
64 return false;
65 }
66
67 public static boolean isEmpty(Object[] a) {
68 if (a == null) {
69 return true;
70 }
71 if (a.length == 0) {
72 return true;
73 }
74 return false;
75 }
76
77 public static long getFirst(long[] a) {
78 return a[0];
79 }
80
81 public static long getLast(long[] a) {
82 return a[a.length - 1];
83 }
84
85 public static int getTotalLengthOfArrays(Iterable<byte[]> arrays) {
86 if (arrays == null) {
87 return 0;
88 }
89 int length = 0;
90 for (byte[] bytes : arrays) {
91 length += length(bytes);
92 }
93 return length;
94 }
95
96 public static ArrayList<Long> toList(long[] array){
97 int length = length(array);
98 ArrayList<Long> list = new ArrayList<Long>(length);
99 for(int i=0; i < length; ++i){
100 list.add(array[i]);
101 }
102 return list;
103 }
104
105 public static byte[] growIfNecessary(byte[] array, int minLength, int numAdditionalBytes) {
106 if(array.length >= minLength){
107 return array;
108 }
109 return Arrays.copyOf(array, minLength + numAdditionalBytes);
110 }
111
112 public static int[] growIfNecessary(int[] array, int minLength, int numAdditionalInts) {
113 if(array.length >= minLength){
114 return array;
115 }
116 return Arrays.copyOf(array, minLength + numAdditionalInts);
117 }
118
119 public static long[] growIfNecessary(long[] array, int minLength, int numAdditionalLongs) {
120 if(array.length >= minLength){
121 return array;
122 }
123 return Arrays.copyOf(array, minLength + numAdditionalLongs);
124 }
125
126 }