• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2007 The Android Open Source Project
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 com.google.android.collect;
18 
19 import android.compat.annotation.UnsupportedAppUsage;
20 import android.os.Build;
21 import android.util.ArraySet;
22 
23 import java.util.Collections;
24 import java.util.EnumSet;
25 import java.util.HashSet;
26 import java.util.SortedSet;
27 import java.util.TreeSet;
28 
29 /**
30  * Provides static methods for creating mutable {@code Set} instances easily and
31  * other static methods for working with Sets.
32  *
33  */
34 public class Sets {
35 
36     /**
37      * Creates an empty {@code HashSet} instance.
38      *
39      * <p><b>Note:</b> if {@code E} is an {@link Enum} type, use {@link
40      * EnumSet#noneOf} instead.
41      *
42      * <p><b>Note:</b> if you only need an <i>immutable</i> empty Set,
43      * use {@link Collections#emptySet} instead.
44      *
45      * @return a newly-created, initially-empty {@code HashSet}
46      */
47     @UnsupportedAppUsage
newHashSet()48     public static <K> HashSet<K> newHashSet() {
49         return new HashSet<K>();
50     }
51 
52     /**
53      * Creates a {@code HashSet} instance containing the given elements.
54      *
55      * <p><b>Note:</b> due to a bug in javac 1.5.0_06, we cannot support the
56      * following:
57      *
58      * <p>{@code Set<Base> set = Sets.newHashSet(sub1, sub2);}
59      *
60      * <p>where {@code sub1} and {@code sub2} are references to subtypes of {@code
61      * Base}, not of {@code Base} itself. To get around this, you must use:
62      *
63      * <p>{@code Set<Base> set = Sets.<Base>newHashSet(sub1, sub2);}
64      *
65      * @param elements the elements that the set should contain
66      * @return a newly-created {@code HashSet} containing those elements (minus
67      *     duplicates)
68      */
69     @UnsupportedAppUsage
newHashSet(E... elements)70     public static <E> HashSet<E> newHashSet(E... elements) {
71         int capacity = elements.length * 4 / 3 + 1;
72         HashSet<E> set = new HashSet<E>(capacity);
73         Collections.addAll(set, elements);
74         return set;
75     }
76 
77     /**
78      * Creates an empty {@code SortedSet} instance.
79      *
80      * @return a newly-created, initially-empty {@code SortedSet}.
81      */
82     @UnsupportedAppUsage
newSortedSet()83     public static <E> SortedSet<E> newSortedSet() {
84         return new TreeSet<E>();
85     }
86 
87     /**
88      * Creates a {@code SortedSet} instance containing the given elements.
89      *
90      * @param elements the elements that the set should contain
91      * @return a newly-created {@code SortedSet} containing those elements (minus
92      *     duplicates)
93      */
newSortedSet(E... elements)94     public static <E> SortedSet<E> newSortedSet(E... elements) {
95         SortedSet<E> set = new TreeSet<E>();
96         Collections.addAll(set, elements);
97         return set;
98     }
99 
100     /**
101      * Creates a {@code ArraySet} instance.
102      */
103     @UnsupportedAppUsage
newArraySet()104     public static <E> ArraySet<E> newArraySet() {
105         return new ArraySet<E>();
106     }
107 
108     /**
109      * Creates a {@code ArraySet} instance containing the given elements.
110      */
111     @UnsupportedAppUsage(maxTargetSdk = Build.VERSION_CODES.R, trackingBug = 170729553)
newArraySet(E... elements)112     public static <E> ArraySet<E> newArraySet(E... elements) {
113         int capacity = elements.length * 4 / 3 + 1;
114         ArraySet<E> set = new ArraySet<E>(capacity);
115         Collections.addAll(set, elements);
116         return set;
117     }
118 }
119