2
* Copyright (C) 2009 The Guava Authors
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
8
* http://www.apache.org/licenses/LICENSE-2.0
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.
17
package com.google.common.collect.testing;
19
import com.google.common.annotations.GwtCompatible;
20
import java.util.ArrayList;
21
import java.util.Arrays;
22
import java.util.Collection;
25
import org.checkerframework.checker.nullness.qual.NonNull;
26
import org.checkerframework.checker.nullness.qual.Nullable;
29
* A simplistic set which implements the bare minimum so that it can be used in tests without
30
* relying on any specific Set implementations. Slow. Explicitly allows null elements so that they
31
* can be used in the testers.
33
* @author Regina O'Dell
36
@ElementTypesAreNonnullByDefault
37
public class MinimalSet<E extends @Nullable Object> extends MinimalCollection<E> implements Set<E> {
39
@SuppressWarnings("unchecked") // empty Object[] as E[]
40
public static <E extends @Nullable Object> MinimalSet<E> of(E... contents) {
41
return ofClassAndContents(Object.class, (E[]) new Object[0], Arrays.asList(contents));
44
@SuppressWarnings("unchecked") // empty Object[] as E[]
45
public static <E extends @Nullable Object> MinimalSet<E> from(Collection<? extends E> contents) {
46
return ofClassAndContents(Object.class, (E[]) new Object[0], contents);
49
public static <E extends @Nullable Object> MinimalSet<E> ofClassAndContents(
50
Class<? super @NonNull E> type, E[] emptyArrayForContents, Iterable<? extends E> contents) {
51
List<E> setContents = new ArrayList<>();
52
for (E e : contents) {
53
if (!setContents.contains(e)) {
57
return new MinimalSet<>(type, setContents.toArray(emptyArrayForContents));
60
private MinimalSet(Class<? super @NonNull E> type, E... contents) {
61
super(type, true, contents);
65
* equals() and hashCode() are more specific in the Set contract.
69
public boolean equals(@Nullable Object object) {
70
if (object instanceof Set) {
71
Set<?> that = (Set<?>) object;
72
return (this.size() == that.size()) && this.containsAll(that);
78
public int hashCode() {
80
for (Object o : this) {
81
hashCodeSum += (o == null) ? 0 : o.hashCode();