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 39 if (elements.length == 1) { 40 return Collections.singleton(elements[0]); 41 } 42 43 LinkedHashSet<E> set = new LinkedHashSet<E>(elements.length * 4 / 3 + 1); 44 Collections.addAll(set, elements); 45 return set; 46 } 47 48 /** 49 * Returns {@code true} if the specified {@code Collection} is {@code null} or {@link Collection#isEmpty empty}, 50 * {@code false} otherwise. 51 * 52 * @param c the collection to check 53 * @return {@code true} if the specified {@code Collection} is {@code null} or {@link Collection#isEmpty empty}, 54 * {@code false} otherwise. 55 * @since 1.0 56 */ 57 public static boolean isEmpty(Collection c) { 58 return c == null || c.isEmpty(); 59 } 60 61 /** 62 * Returns {@code true} if the specified {@code Map} is {@code null} or {@link Map#isEmpty empty}, 63 * {@code false} otherwise. 64 * 65 * @param m the {@code Map} to check 66 * @return {@code true} if the specified {@code Map} is {@code null} or {@link Map#isEmpty empty}, 67 * {@code false} otherwise. 68 * @since 1.0 69 */ 70 public static boolean isEmpty(Map m) { 71 return m == null || m.isEmpty(); 72 } 73 74 /** 75 * Returns the size of the specified collection or {@code 0} if the collection is {@code null}. 76 * 77 * @param c the collection to check 78 * @return the size of the specified collection or {@code 0} if the collection is {@code null}. 79 * @since 1.2 80 */ 81 public static int size(Collection c) { 82 return c != null ? c.size() : 0; 83 } 84 85 /** 86 * Returns the size of the specified map or {@code 0} if the map is {@code null}. 87 * 88 * @param m the map to check 89 * @return the size of the specified map or {@code 0} if the map is {@code null}. 90 * @since 1.2 91 */ 92 public static int size(Map m) { 93 return m != null ? m.size() : 0; 94 } 95 96 97 /** 98 * Returns {@code true} if the specified {@code PrincipalCollection} is {@code null} or 99 * {@link PrincipalCollection#isEmpty empty}, {@code false} otherwise. 100 * 101 * @param principals the principals to check. 102 * @return {@code true} if the specified {@code PrincipalCollection} is {@code null} or 103 * {@link PrincipalCollection#isEmpty empty}, {@code false} otherwise. 104 * @since 1.0 105 */ 106 public static boolean isEmpty(PrincipalCollection principals) { 107 return principals == null || principals.isEmpty(); 108 } 109 110 public static <E> List<E> asList(E... elements) { 111 if (elements == null || elements.length == 0) { 112 return Collections.emptyList(); 113 } 114 115 // Integer overflow does not occur when a large array is passed in because the list array already exists 116 return Arrays.asList(elements); 117 } 118 119 /*public static <E> Deque<E> asDeque(E... elements) { 120 if (elements == null || elements.length == 0) { 121 return new ArrayDeque<E>(); 122 } 123 // Avoid integer overflow when a large array is passed in 124 int capacity = computeListCapacity(elements.length); 125 ArrayDeque<E> deque = new ArrayDeque<E>(capacity); 126 Collections.addAll(deque, elements); 127 return deque; 128 }*/ 129 130 static int computeListCapacity(int arraySize) { 131 return (int) Math.min(5L + arraySize + (arraySize / 10), Integer.MAX_VALUE); 132 } 133 }