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 module hunt.shiro.util.CollectionUtils;
20 
21 import hunt.shiro.subject.PrincipalCollection;
22 
23 import hunt.util.ArrayHelper;
24 import hunt.collection;
25 import std.range;
26 
27 /**
28  * Static helper class for use dealing with Collections.
29  *
30  */
31 class CollectionUtils {
32 
33      static Set!(E) asSet(E)(E[] elements... ) {
34         if (elements.empty) {
35             return Collections.emptySet!(E)();
36         }
37 
38         if (elements.length == 1) {
39             return Collections.singleton!(E)(elements[0]);
40         }
41 
42         LinkedHashSet!(E) set = new LinkedHashSet!(E)(cast(int)elements.length * 4 / 3 + 1);
43         set.addAll(elements);
44         return set;
45     }
46 
47     /**
48      * Returns {@code true} if the specified {@code Collection} is {@code null} or {@link Collection#isEmpty empty},
49      * {@code false} otherwise.
50      *
51      * @param c the collection to check
52      * @return {@code true} if the specified {@code Collection} is {@code null} or {@link Collection#isEmpty empty},
53      *         {@code false} otherwise.
54      */
55     static bool isEmpty(T)(Collection!(T) c) {
56         return c is null || c.isEmpty();
57     }
58 
59     /**
60      * Returns {@code true} if the specified {@code Map} is {@code null} or {@link Map#isEmpty empty},
61      * {@code false} otherwise.
62      *
63      * @param m the {@code Map} to check
64      * @return {@code true} if the specified {@code Map} is {@code null} or {@link Map#isEmpty empty},
65      *         {@code false} otherwise.
66      */
67     static bool isEmpty(K, V)(Map!(K, V) m) {
68         return m  is null || m.isEmpty();
69     }
70 
71     /**
72      * Returns the size of the specified collection or {@code 0} if the collection is {@code null}.
73      *
74      * @param c the collection to check
75      * @return the size of the specified collection or {@code 0} if the collection is {@code null}.
76      */
77     static int size(T)(Collection!T c) {
78         return c !is null ? c.size() : 0;
79     }
80 
81     /**
82      * Returns the size of the specified map or {@code 0} if the map is {@code null}.
83      *
84      * @param m the map to check
85      * @return the size of the specified map or {@code 0} if the map is {@code null}.
86      */
87     static int size(K, V)(Map!(K, V) m) {
88         return m !is 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 //      * deprecated("") Use PrincipalCollection.isEmpty() directly.
100 //      */
101 //     deprecated("")
102 //      static bool isEmpty(PrincipalCollection principals) {
103 //         return principals  is null || principals.isEmpty();
104 //     }
105 
106 //      static <E> List!(E) asList(E... elements) {
107 //         if (elements  is null || elements.length == 0) {
108 //             return Collections.emptyList();
109 //         }
110 
111 //         // Integer overflow does not occur when a large array is passed in because the list array already exists
112 //         return ArrayHelper.asList(elements);
113 //     }
114 
115 //     /*public static <E> Deque!(E) asDeque(E... elements) {
116 //         if (elements  is null || elements.length == 0) {
117 //             return new ArrayDeque!(E)();
118 //         }
119 //         // Avoid integer overflow when a large array is passed in
120 //         int capacity = computeListCapacity(elements.length);
121 //         ArrayDeque!(E) deque = new ArrayDeque!(E)(capacity);
122 //         Collections.addAll(deque, elements);
123 //         return deque;
124 //     }*/
125 
126 //     static int computeListCapacity(int arraySize) {
127 //         return (int) Math.min(5L + arraySize + (arraySize / 10), Integer.MAX_VALUE);
128 //     }
129 }