SampleElements.java revision 3c77433663281544363151bf284b0240dfd22a42
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
17package com.google.common.collect.testing;
18
19import com.google.common.annotations.GwtCompatible;
20
21import java.util.Arrays;
22import java.util.Iterator;
23import java.util.Map;
24
25/**
26 * A container class for the five sample elements we need for testing.
27 *
28 * <p>This class is GWT compatible.
29 *
30 * @author Kevin Bourrillion
31 */
32@GwtCompatible
33public class SampleElements<E> implements Iterable<E> {
34  // TODO: rename e3, e4 => missing1, missing2
35  public final E e0;
36  public final E e1;
37  public final E e2;
38  public final E e3;
39  public final E e4;
40
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
50  public Iterator<E> iterator() {
51    return Arrays.asList(e0, e1, e2, e3, e4).iterator();
52  }
53
54  public static class Strings extends SampleElements<String> {
55    public Strings() {
56      // elements aren't sorted, to better test SortedSet iteration ordering
57      super("b", "a", "c", "d", "e");
58    }
59
60    // for testing SortedSet and SortedMap methods
61    public static final String BEFORE_FIRST = "\0";
62    public static final String BEFORE_FIRST_2 = "\0\0";
63    public static final String MIN_ELEMENT = "a";
64    public static final String AFTER_LAST = "z";
65    public static final String AFTER_LAST_2 = "zz";
66  }
67
68  public static class Chars extends SampleElements<Character> {
69    public Chars() {
70      // elements aren't sorted, to better test SortedSet iteration ordering
71      super('b', 'a', 'c', 'd', 'e');
72    }
73  }
74
75  public static class Enums extends SampleElements<AnEnum> {
76    public Enums() {
77      // elements aren't sorted, to better test SortedSet iteration ordering
78      super(AnEnum.B, AnEnum.A, AnEnum.C, AnEnum.D, AnEnum.E);
79    }
80  }
81
82  public static class Ints extends SampleElements<Integer> {
83    public Ints() {
84      // elements aren't sorted, to better test SortedSet iteration ordering
85      super(1, 0, 2, 3, 4);
86    }
87  }
88
89  public static <K, V> SampleElements<Map.Entry<K, V>> mapEntries(
90      SampleElements<K> keys, SampleElements<V> values) {
91    return new SampleElements<Map.Entry<K, V>>(
92        Helpers.mapEntry(keys.e0, values.e0),
93        Helpers.mapEntry(keys.e1, values.e1),
94        Helpers.mapEntry(keys.e2, values.e2),
95        Helpers.mapEntry(keys.e3, values.e3),
96        Helpers.mapEntry(keys.e4, values.e4));
97  }
98
99  public static class Unhashables extends SampleElements<UnhashableObject> {
100    public Unhashables() {
101      super(new UnhashableObject(1),
102          new UnhashableObject(2),
103          new UnhashableObject(3),
104          new UnhashableObject(4),
105          new UnhashableObject(5));
106    }
107  }
108
109  public static class Colliders extends SampleElements<Object> {
110    public Colliders() {
111      super(new Collider(1),
112          new Collider(2),
113          new Collider(3),
114          new Collider(4),
115          new Collider(5));
116    }
117  }
118
119  private static class Collider {
120    final int value;
121
122    Collider(int value) {
123      this.value = value;
124    }
125
126    @Override public boolean equals(Object obj) {
127      return obj instanceof Collider && ((Collider) obj).value == value;
128    }
129
130    @Override public int hashCode() {
131      return 1; // evil!
132    }
133  }
134}
135