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,
13   * software distributed under the License is distributed on an
14   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15   * KIND, either express or implied.  See the License for the
16   * specific language governing permissions and limitations
17   * under the License.
18   */
19  package org.apache.shiro.util;
20  
21  import org.apache.shiro.subject.PrincipalCollection;
22  
23  import java.util.*;
24  
25  /**
26   * Static helper class for use dealing with Collections.
27   *
28   * @since 0.9
29   */
30  public class CollectionUtils {
31  
32      //TODO - complete JavaDoc
33  
34      public static <E> Set<E> asSet(E... elements) {
35          if (elements == null || elements.length == 0) {
36              return Collections.emptySet();
37          }
38          LinkedHashSet<E> set = new LinkedHashSet<E>(elements.length * 4 / 3 + 1);
39          Collections.addAll(set, elements);
40          return set;
41      }
42  
43      /**
44       * Returns {@code true} if the specified {@code Collection} is {@code null} or {@link Collection#isEmpty empty},
45       * {@code false} otherwise.
46       *
47       * @param c the collection to check
48       * @return {@code true} if the specified {@code Collection} is {@code null} or {@link Collection#isEmpty empty},
49       *         {@code false} otherwise.
50       * @since 1.0
51       */
52      public static boolean isEmpty(Collection c) {
53          return c == null || c.isEmpty();
54      }
55  
56      /**
57       * Returns {@code true} if the specified {@code Map} is {@code null} or {@link Map#isEmpty empty},
58       * {@code false} otherwise.
59       *
60       * @param m the {@code Map} to check
61       * @return {@code true} if the specified {@code Map} is {@code null} or {@link Map#isEmpty empty},
62       *         {@code false} otherwise.
63       * @since 1.0
64       */
65      public static boolean isEmpty(Map m) {
66          return m == null || m.isEmpty();
67      }
68  
69      /**
70       * Returns the size of the specified collection or {@code 0} if the collection is {@code null}.
71       *
72       * @param c the collection to check
73       * @return the size of the specified collection or {@code 0} if the collection is {@code null}.
74       * @since 1.2
75       */
76      public static int size(Collection c) {
77          return c != null ? c.size() : 0;
78      }
79  
80      /**
81       * Returns the size of the specified map or {@code 0} if the map is {@code null}.
82       *
83       * @param m the map to check
84       * @return the size of the specified map or {@code 0} if the map is {@code null}.
85       * @since 1.2
86       */
87      public static int size(Map m) {
88          return m != null ? m.size() : 0;
89      }
90  
91  
92      /**
93       * Returns {@code true} if the specified {@code PrincipalCollection} is {@code null} or
94       * {@link PrincipalCollection#isEmpty empty}, {@code false} otherwise.
95       *
96       * @param principals the principals to check.
97       * @return {@code true} if the specified {@code PrincipalCollection} is {@code null} or
98       *         {@link PrincipalCollection#isEmpty empty}, {@code false} otherwise.
99       * @since 1.0
100      */
101     public static boolean isEmpty(PrincipalCollection principals) {
102         return principals == null || principals.isEmpty();
103     }
104 
105     public static <E> List<E> asList(E... elements) {
106         if (elements == null || elements.length == 0) {
107             return Collections.emptyList();
108         }
109         // Avoid integer overflow when a large array is passed in
110         int capacity = computeListCapacity(elements.length);
111         ArrayList<E> list = new ArrayList<E>(capacity);
112         Collections.addAll(list, elements);
113         return list;
114     }
115 
116     /*public static <E> Deque<E> asDeque(E... elements) {
117         if (elements == null || elements.length == 0) {
118             return new ArrayDeque<E>();
119         }
120         // Avoid integer overflow when a large array is passed in
121         int capacity = computeListCapacity(elements.length);
122         ArrayDeque<E> deque = new ArrayDeque<E>(capacity);
123         Collections.addAll(deque, elements);
124         return deque;
125     }*/
126 
127     static int computeListCapacity(int arraySize) {
128         return (int) Math.min(5L + arraySize + (arraySize / 10), Integer.MAX_VALUE);
129     }
130 }