• 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 
26 /**
27  * A simplistic set which implements the bare minimum so that it can be used in tests without
28  * relying on any specific Set implementations. Slow. Explicitly allows null elements so that they
29  * can be used in the testers.
30  *
31  * @author Regina O'Dell
32  */
33 @GwtCompatible
34 public class MinimalSet<E> extends MinimalCollection<E> implements Set<E> {
35 
36   @SuppressWarnings("unchecked") // empty Object[] as E[]
of(E... contents)37   public static <E> MinimalSet<E> of(E... contents) {
38     return ofClassAndContents(Object.class, (E[]) new Object[0], Arrays.asList(contents));
39   }
40 
41   @SuppressWarnings("unchecked") // empty Object[] as E[]
from(Collection<? extends E> contents)42   public static <E> MinimalSet<E> from(Collection<? extends E> contents) {
43     return ofClassAndContents(Object.class, (E[]) new Object[0], contents);
44   }
45 
ofClassAndContents( Class<? super E> type, E[] emptyArrayForContents, Iterable<? extends E> contents)46   public static <E> MinimalSet<E> ofClassAndContents(
47       Class<? super E> type, E[] emptyArrayForContents, Iterable<? extends E> contents) {
48     List<E> setContents = new ArrayList<E>();
49     for (E e : contents) {
50       if (!setContents.contains(e)) {
51         setContents.add(e);
52       }
53     }
54     return new MinimalSet<E>(type, setContents.toArray(emptyArrayForContents));
55   }
56 
MinimalSet(Class<? super E> type, E... contents)57   private MinimalSet(Class<? super E> type, E... contents) {
58     super(type, true, contents);
59   }
60 
61   /*
62    * equals() and hashCode() are more specific in the Set contract.
63    */
64 
65   @Override
equals(Object object)66   public boolean equals(Object object) {
67     if (object instanceof Set) {
68       Set<?> that = (Set<?>) object;
69       return (this.size() == that.size()) && this.containsAll(that);
70     }
71     return false;
72   }
73 
74   @Override
hashCode()75   public int hashCode() {
76     int hashCodeSum = 0;
77     for (Object o : this) {
78       hashCodeSum += (o == null) ? 0 : o.hashCode();
79     }
80     return hashCodeSum;
81   }
82 }
83