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