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