View Javadoc

1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one
3    * or more contributor license agreements.  See the NOTICE file
4    * distributed with this work for additional information
5    * regarding copyright ownership.  The ASF licenses this file
6    * to you under the Apache License, Version 2.0 (the
7    * "License"); you may not use this file except in compliance
8    * with the License.  You may obtain a copy of the License at
9    *
10   *     http://www.apache.org/licenses/LICENSE-2.0
11   *
12   * Unless required by applicable law or agreed to in writing, software
13   * distributed under the License is distributed on an "AS IS" BASIS,
14   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15   * See the License for the specific language governing permissions and
16   * limitations under the License.
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   * A set of array utility functions that return reasonable values in cases where an array is
28   * allocated or if it is null
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 }