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.Collection;
23  import java.util.Collections;
24  import java.util.List;
25  
26  import org.apache.hadoop.hbase.classification.InterfaceAudience;
27  
28  /**
29   * Utility methods for dealing with Collections, including treating null collections as empty.
30   */
31  @InterfaceAudience.Private
32  public class CollectionUtils {
33  
34    private static final List<Object> EMPTY_LIST = Collections.unmodifiableList(
35      new ArrayList<Object>(0));
36  
37    
38    @SuppressWarnings("unchecked")
39    public static <T> Collection<T> nullSafe(Collection<T> in) {
40      if (in == null) {
41        return (Collection<T>)EMPTY_LIST;
42      }
43      return in;
44    }
45  
46    /************************ size ************************************/
47  
48    public static <T> int nullSafeSize(Collection<T> collection) {
49      if (collection == null) {
50        return 0;
51      }
52      return collection.size();
53    }
54  
55    public static <A, B> boolean nullSafeSameSize(Collection<A> a, Collection<B> b) {
56      return nullSafeSize(a) == nullSafeSize(b);
57    }
58  
59    /*************************** empty ****************************************/
60  
61    public static <T> boolean isEmpty(Collection<T> collection) {
62      return collection == null || collection.isEmpty();
63    }
64  
65    public static <T> boolean notEmpty(Collection<T> collection) {
66      return !isEmpty(collection);
67    }
68  
69    /************************ first/last **************************/
70  
71    public static <T> T getFirst(Collection<T> collection) {
72      if (CollectionUtils.isEmpty(collection)) {
73        return null;
74      }
75      for (T t : collection) {
76        return t;
77      }
78      return null;
79    }
80    
81    /**
82     * @param list any list
83     * @return -1 if list is empty, otherwise the max index
84     */
85    public static int getLastIndex(List<?> list){
86      if(isEmpty(list)){
87        return -1;
88      }
89      return list.size() - 1;
90    }
91    
92    /**
93     * @param list
94     * @param index the index in question
95     * @return true if it is the last index or if list is empty and -1 is passed for the index param
96     */
97    public static boolean isLastIndex(List<?> list, int index){
98      return index == getLastIndex(list);
99    }
100 
101   public static <T> T getLast(List<T> list) {
102     if (isEmpty(list)) {
103       return null;
104     }
105     return list.get(list.size() - 1);
106   }
107 }