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