001    /*
002     * Licensed to the Apache Software Foundation (ASF) under one
003     * or more contributor license agreements.  See the NOTICE file
004     * distributed with this work for additional information
005     * regarding copyright ownership.  The ASF licenses this file
006     * to you under the Apache License, Version 2.0 (the
007     * "License"); you may not use this file except in compliance
008     * with the License.  You may obtain a copy of the License at
009     *
010     *     http://www.apache.org/licenses/LICENSE-2.0
011     *
012     * Unless required by applicable law or agreed to in writing,
013     * software distributed under the License is distributed on an
014     * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
015     * KIND, either express or implied.  See the License for the
016     * specific language governing permissions and limitations
017     * under the License.
018     */
019    package org.apache.shiro.util;
020    
021    import org.apache.shiro.subject.PrincipalCollection;
022    
023    import java.util.*;
024    
025    /**
026     * Static helper class for use dealing with Collections.
027     *
028     * @author Jeremy Haile
029     * @author Les Hazlewood
030     * @since 0.9
031     */
032    public class CollectionUtils {
033    
034        //TODO - complete JavaDoc
035    
036        public static <E> Set<E> asSet(E... elements) {
037            if (elements == null || elements.length == 0) {
038                return Collections.emptySet();
039            }
040            LinkedHashSet<E> set = new LinkedHashSet<E>(elements.length * 4 / 3 + 1);
041            Collections.addAll(set, elements);
042            return set;
043        }
044    
045        /**
046         * Returns {@code true} if the specified {@code Collection} is {@code null} or {@link Collection#isEmpty empty},
047         * {@code false} otherwise.
048         *
049         * @param c the collection to check
050         * @return {@code true} if the specified {@code Collection} is {@code null} or {@link Collection#isEmpty empty},
051         *         {@code false} otherwise.
052         * @since 1.0
053         */
054        public static boolean isEmpty(Collection c) {
055            return c == null || c.isEmpty();
056        }
057    
058        /**
059         * Returns {@code true} if the specified {@code Map} is {@code null} or {@link Map#isEmpty empty},
060         * {@code false} otherwise.
061         *
062         * @param m the {@code Map} to check
063         * @return {@code true} if the specified {@code Map} is {@code null} or {@link Map#isEmpty empty},
064         *         {@code false} otherwise.
065         * @since 1.0
066         */
067        public static boolean isEmpty(Map m) {
068            return m == null || m.isEmpty();
069        }
070    
071        /**
072         * Returns {@code true} if the specified {@code PrincipalCollection} is {@code null} or
073         * {@link PrincipalCollection#isEmpty empty}, {@code false} otherwise.
074         *
075         * @param principals the principals to check.
076         * @return {@code true} if the specified {@code PrincipalCollection} is {@code null} or
077         *         {@link PrincipalCollection#isEmpty empty}, {@code false} otherwise.
078         * @since 1.0
079         */
080        public static boolean isEmpty(PrincipalCollection principals) {
081            return principals == null || principals.isEmpty();
082        }
083    
084        public static <E> List<E> asList(E... elements) {
085            if (elements == null || elements.length == 0) {
086                return Collections.emptyList();
087            }
088            // Avoid integer overflow when a large array is passed in
089            int capacity = computeListCapacity(elements.length);
090            ArrayList<E> list = new ArrayList<E>(capacity);
091            Collections.addAll(list, elements);
092            return list;
093        }
094    
095        /*public static <E> Deque<E> asDeque(E... elements) {
096            if (elements == null || elements.length == 0) {
097                return new ArrayDeque<E>();
098            }
099            // Avoid integer overflow when a large array is passed in
100            int capacity = computeListCapacity(elements.length);
101            ArrayDeque<E> deque = new ArrayDeque<E>(capacity);
102            Collections.addAll(deque, elements);
103            return deque;
104        }*/
105    
106        static int computeListCapacity(int arraySize) {
107            return (int) Math.min(5L + arraySize + (arraySize / 10), Integer.MAX_VALUE);
108        }
109    }