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