| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
package com.google.common.collect.testing; |
| 18 |
|
| 19 |
import com.google.common.annotations.GwtCompatible; |
| 20 |
import java.util.ArrayList; |
| 21 |
import java.util.Arrays; |
| 22 |
import java.util.Collection; |
| 23 |
import java.util.List; |
| 24 |
import java.util.Set; |
| 25 |
import org.checkerframework.checker.nullness.qual.Nullable; |
| 26 |
|
| 27 |
|
| 28 |
|
| 29 |
|
| 30 |
|
| 31 |
|
| 32 |
|
| 33 |
|
| 34 |
@GwtCompatible |
| 35 |
public class MinimalSet<E> extends MinimalCollection<E> implements Set<E> { |
| 36 |
|
| 37 |
@SuppressWarnings("unchecked") |
| 38 |
public static <E> MinimalSet<E> of(E... contents) { |
| 39 |
return ofClassAndContents(Object.class, (E[]) new Object[0], Arrays.asList(contents)); |
| 40 |
} |
| 41 |
|
| 42 |
@SuppressWarnings("unchecked") |
| 43 |
public static <E> MinimalSet<E> from(Collection<? extends E> contents) { |
| 44 |
return ofClassAndContents(Object.class, (E[]) new Object[0], contents); |
| 45 |
} |
| 46 |
|
| 47 |
public static <E> MinimalSet<E> ofClassAndContents( |
| 48 |
Class<? super E> type, E[] emptyArrayForContents, Iterable<? extends E> contents) { |
| 49 |
List<E> setContents = new ArrayList<>(); |
| 50 |
for (E e : contents) { |
| 51 |
if (!setContents.contains(e)) { |
| 52 |
setContents.add(e); |
| 53 |
} |
| 54 |
} |
| 55 |
return new MinimalSet<>(type, setContents.toArray(emptyArrayForContents)); |
| 56 |
} |
| 57 |
|
| 58 |
private MinimalSet(Class<? super E> type, E... contents) { |
| 59 |
super(type, true, contents); |
| 60 |
} |
| 61 |
|
| 62 |
|
| 63 |
|
| 64 |
|
| 65 |
|
| 66 |
@Override |
| 67 |
public boolean equals(@Nullable Object object) { |
| 68 |
if (object instanceof Set) { |
| 69 |
Set<?> that = (Set<?>) object; |
| 70 |
return (this.size() == that.size()) && this.containsAll(that); |
| 71 |
} |
| 72 |
return false; |
| 73 |
} |
| 74 |
|
| 75 |
@Override |
| 76 |
public int hashCode() { |
| 77 |
int hashCodeSum = 0; |
| 78 |
for (Object o : this) { |
| 79 |
hashCodeSum += (o == null) ? 0 : o.hashCode(); |
| 80 |
} |
| 81 |
return hashCodeSum; |
| 82 |
} |
| 83 |
} |