-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathImmutableSet.java
37 lines (32 loc) · 1.13 KB
/
ImmutableSet.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
package com.tazkiyatech.utils.collections;
import androidx.annotation.NonNull;
import java.util.Arrays;
import java.util.Collection;
import java.util.Collections;
import java.util.Set;
import java.util.TreeSet;
public class ImmutableSet {
/**
* Creates an immutable copy of the provided {@link Collection}.
*
* @param collection the {@link Collection} to copy.
* @param <T> the type of the elements in the provided {@link Collection}.
* @return an immutable copy of the provided {@link Collection}.
*/
@NonNull
public static <T> Set<T> copyOf(@NonNull Collection<T> collection) {
return Collections.unmodifiableSet(new TreeSet<>(collection));
}
/**
* Creates an immutable {@link Set} containing the given items.
*
* @param items the items to be contained in the {@link Set}.
* @param <T> the type of the elements that the {@link Set} will be composed of.
* @return an immutable {@link Set} containing the given items.
*/
@SafeVarargs
@NonNull
public static <T> Set<T> of(@NonNull T... items) {
return copyOf(Arrays.asList(items));
}
}