• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2009 The Guava Authors
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.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  * A simplistic set which implements the bare minimum so that it can be used in tests without
29  * relying on any specific Set implementations. Slow. Explicitly allows null elements so that they
30  * can be used in the testers.
31  *
32  * @author Regina O'Dell
33  */
34 @GwtCompatible
35 public class MinimalSet<E> extends MinimalCollection<E> implements Set<E> {
36 
37   @SuppressWarnings("unchecked") // empty Object[] as E[]
of(E... contents)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") // empty Object[] as E[]
from(Collection<? extends E> contents)43   public static <E> MinimalSet<E> from(Collection<? extends E> contents) {
44     return ofClassAndContents(Object.class, (E[]) new Object[0], contents);
45   }
46 
ofClassAndContents( Class<? super E> type, E[] emptyArrayForContents, Iterable<? extends E> contents)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 
MinimalSet(Class<? super E> type, E... contents)58   private MinimalSet(Class<? super E> type, E... contents) {
59     super(type, true, contents);
60   }
61 
62   /*
63    * equals() and hashCode() are more specific in the Set contract.
64    */
65 
66   @Override
equals(@ullable Object object)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
hashCode()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 }
84