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