xref: /aosp_15_r20/external/dagger2/java/dagger/internal/DaggerCollections.java (revision f585d8a307d0621d6060bd7e80091fdcbf94fe27)
1 /*
2  * Copyright (C) 2014 The Dagger Authors.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 package dagger.internal;
18 
19 import java.util.ArrayList;
20 import java.util.Collections;
21 import java.util.HashSet;
22 import java.util.LinkedHashMap;
23 import java.util.List;
24 import java.util.Set;
25 
26 /**
27  * Collection utility methods in service of Dagger internal classes. <em>Do not use</em> in client
28  * code.
29  */
30 public final class DaggerCollections {
31   /**
32    * The maximum value for a signed 32-bit integer that is equal to a power of 2.
33    */
34   private static final int MAX_POWER_OF_TWO = 1 << (Integer.SIZE - 2);
35 
DaggerCollections()36   private DaggerCollections() {}
37 
38   /**
39    * Returns a new list that is pre-sized to {@code size}, or {@link Collections#emptyList()} if
40    * empty. The list returned is never intended to grow beyond {@code size}, so adding to a list
41    * when the size is 0 is an error.
42    */
presizedList(int size)43   public static <T> List<T> presizedList(int size) {
44     if (size == 0) {
45       return Collections.emptyList();
46     }
47     return new ArrayList<T>(size);
48   }
49 
50   /**
51    * Returns true if at least one pair of items in {@code list} are equals.
52    */
hasDuplicates(List<?> list)53   public static boolean hasDuplicates(List<?> list) {
54     if (list.size() < 2) {
55       return false;
56     }
57     Set<Object> asSet = new HashSet<Object>(list);
58     return list.size() != asSet.size();
59   }
60 
61   /**
62    * Creates a {@link HashSet} instance, with a high enough "intial capcity" that it <em>should</em>
63    * hold {@code expectedSize} elements without growth.
64    */
newHashSetWithExpectedSize(int expectedSize)65   static <T> HashSet<T> newHashSetWithExpectedSize(int expectedSize) {
66     return new HashSet<T>(calculateInitialCapacity(expectedSize));
67   }
68 
69   /**
70    * Creates a {@link LinkedHashMap} instance, with a high enough "initial capacity" that it
71    * <em>should</em> hold {@code expectedSize} elements without growth.
72    */
newLinkedHashMapWithExpectedSize(int expectedSize)73   public static <K, V> LinkedHashMap<K, V> newLinkedHashMapWithExpectedSize(int expectedSize) {
74     return new LinkedHashMap<K, V>(calculateInitialCapacity(expectedSize));
75   }
76 
calculateInitialCapacity(int expectedSize)77   private static int calculateInitialCapacity(int expectedSize) {
78     if (expectedSize < 3) {
79       return expectedSize + 1;
80     }
81     if (expectedSize < MAX_POWER_OF_TWO) {
82       // This is the calculation used in JDK8 to resize when a putAll
83       // happens; it seems to be the most conservative calculation we
84       // can make.  0.75 is the default load factor.
85       return (int) (expectedSize / 0.75F + 1.0F);
86     }
87     return Integer.MAX_VALUE; // any large value
88   }
89 }
90