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  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 }