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