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