• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2008 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.Arrays;
21 import java.util.Iterator;
22 import java.util.List;
23 import java.util.Map.Entry;
24 import org.checkerframework.checker.nullness.qual.Nullable;
25 
26 /**
27  * A container class for the five sample elements we need for testing.
28  *
29  * @author Kevin Bourrillion
30  */
31 @GwtCompatible
32 @ElementTypesAreNonnullByDefault
33 public class SampleElements<E extends @Nullable Object> implements Iterable<E> {
34   // TODO: rename e3, e4 => missing1, missing2
35   private final E e0;
36   private final E e1;
37   private final E e2;
38   private final E e3;
39   private final E e4;
40 
SampleElements(E e0, E e1, E e2, E e3, E e4)41   public SampleElements(E e0, E e1, E e2, E e3, E e4) {
42     this.e0 = e0;
43     this.e1 = e1;
44     this.e2 = e2;
45     this.e3 = e3;
46     this.e4 = e4;
47   }
48 
49   @Override
iterator()50   public Iterator<E> iterator() {
51     return asList().iterator();
52   }
53 
asList()54   public List<E> asList() {
55     return Arrays.asList(e0(), e1(), e2(), e3(), e4());
56   }
57 
58   public static class Strings extends SampleElements<String> {
Strings()59     public Strings() {
60       // elements aren't sorted, to better test SortedSet iteration ordering
61       super("b", "a", "c", "d", "e");
62     }
63 
64     // for testing SortedSet and SortedMap methods
65     public static final String BEFORE_FIRST = "\0";
66     public static final String BEFORE_FIRST_2 = "\0\0";
67     public static final String MIN_ELEMENT = "a";
68     public static final String AFTER_LAST = "z";
69     public static final String AFTER_LAST_2 = "zz";
70   }
71 
72   public static class Chars extends SampleElements<Character> {
Chars()73     public Chars() {
74       // elements aren't sorted, to better test SortedSet iteration ordering
75       super('b', 'a', 'c', 'd', 'e');
76     }
77   }
78 
79   public static class Enums extends SampleElements<AnEnum> {
Enums()80     public Enums() {
81       // elements aren't sorted, to better test SortedSet iteration ordering
82       super(AnEnum.B, AnEnum.A, AnEnum.C, AnEnum.D, AnEnum.E);
83     }
84   }
85 
86   public static class Ints extends SampleElements<Integer> {
Ints()87     public Ints() {
88       // elements aren't sorted, to better test SortedSet iteration ordering
89       super(1, 0, 2, 3, 4);
90     }
91   }
92 
93   public static <K extends @Nullable Object, V extends @Nullable Object>
mapEntries(SampleElements<K> keys, SampleElements<V> values)94       SampleElements<Entry<K, V>> mapEntries(SampleElements<K> keys, SampleElements<V> values) {
95     return new SampleElements<>(
96         Helpers.mapEntry(keys.e0(), values.e0()),
97         Helpers.mapEntry(keys.e1(), values.e1()),
98         Helpers.mapEntry(keys.e2(), values.e2()),
99         Helpers.mapEntry(keys.e3(), values.e3()),
100         Helpers.mapEntry(keys.e4(), values.e4()));
101   }
102 
e0()103   public E e0() {
104     return e0;
105   }
106 
e1()107   public E e1() {
108     return e1;
109   }
110 
e2()111   public E e2() {
112     return e2;
113   }
114 
e3()115   public E e3() {
116     return e3;
117   }
118 
e4()119   public E e4() {
120     return e4;
121   }
122 
123   public static class Unhashables extends SampleElements<UnhashableObject> {
Unhashables()124     public Unhashables() {
125       super(
126           new UnhashableObject(1),
127           new UnhashableObject(2),
128           new UnhashableObject(3),
129           new UnhashableObject(4),
130           new UnhashableObject(5));
131     }
132   }
133 
134   public static class Colliders extends SampleElements<Object> {
Colliders()135     public Colliders() {
136       super(new Collider(1), new Collider(2), new Collider(3), new Collider(4), new Collider(5));
137     }
138   }
139 
140   private static class Collider {
141     final int value;
142 
Collider(int value)143     Collider(int value) {
144       this.value = value;
145     }
146 
147     @Override
equals(@ullable Object obj)148     public boolean equals(@Nullable Object obj) {
149       return obj instanceof Collider && ((Collider) obj).value == value;
150     }
151 
152     @Override
hashCode()153     public int hashCode() {
154       return 1; // evil!
155     }
156   }
157 }
158