001/* 002 * Copyright (C) 2008 The Guava Authors 003 * 004 * Licensed under the Apache License, Version 2.0 (the "License"); 005 * you may not use this file except in compliance with the License. 006 * You may obtain a copy of the License at 007 * 008 * http://www.apache.org/licenses/LICENSE-2.0 009 * 010 * Unless required by applicable law or agreed to in writing, software 011 * distributed under the License is distributed on an "AS IS" BASIS, 012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 013 * See the License for the specific language governing permissions and 014 * limitations under the License. 015 */ 016 017package com.google.common.collect.testing; 018 019import com.google.common.annotations.GwtCompatible; 020import java.util.Arrays; 021import java.util.Iterator; 022import java.util.List; 023import java.util.Map.Entry; 024 025/** 026 * A container class for the five sample elements we need for testing. 027 * 028 * @author Kevin Bourrillion 029 */ 030@GwtCompatible 031public class SampleElements<E> implements Iterable<E> { 032 // TODO: rename e3, e4 => missing1, missing2 033 private final E e0; 034 private final E e1; 035 private final E e2; 036 private final E e3; 037 private final E e4; 038 039 public SampleElements(E e0, E e1, E e2, E e3, E e4) { 040 this.e0 = e0; 041 this.e1 = e1; 042 this.e2 = e2; 043 this.e3 = e3; 044 this.e4 = e4; 045 } 046 047 @Override 048 public Iterator<E> iterator() { 049 return asList().iterator(); 050 } 051 052 public List<E> asList() { 053 return Arrays.asList(e0(), e1(), e2(), e3(), e4()); 054 } 055 056 public static class Strings extends SampleElements<String> { 057 public Strings() { 058 // elements aren't sorted, to better test SortedSet iteration ordering 059 super("b", "a", "c", "d", "e"); 060 } 061 062 // for testing SortedSet and SortedMap methods 063 public static final String BEFORE_FIRST = "\0"; 064 public static final String BEFORE_FIRST_2 = "\0\0"; 065 public static final String MIN_ELEMENT = "a"; 066 public static final String AFTER_LAST = "z"; 067 public static final String AFTER_LAST_2 = "zz"; 068 } 069 070 public static class Chars extends SampleElements<Character> { 071 public Chars() { 072 // elements aren't sorted, to better test SortedSet iteration ordering 073 super('b', 'a', 'c', 'd', 'e'); 074 } 075 } 076 077 public static class Enums extends SampleElements<AnEnum> { 078 public Enums() { 079 // elements aren't sorted, to better test SortedSet iteration ordering 080 super(AnEnum.B, AnEnum.A, AnEnum.C, AnEnum.D, AnEnum.E); 081 } 082 } 083 084 public static class Ints extends SampleElements<Integer> { 085 public Ints() { 086 // elements aren't sorted, to better test SortedSet iteration ordering 087 super(1, 0, 2, 3, 4); 088 } 089 } 090 091 public static <K, V> SampleElements<Entry<K, V>> mapEntries( 092 SampleElements<K> keys, SampleElements<V> values) { 093 return new SampleElements<>( 094 Helpers.mapEntry(keys.e0(), values.e0()), 095 Helpers.mapEntry(keys.e1(), values.e1()), 096 Helpers.mapEntry(keys.e2(), values.e2()), 097 Helpers.mapEntry(keys.e3(), values.e3()), 098 Helpers.mapEntry(keys.e4(), values.e4())); 099 } 100 101 public E e0() { 102 return e0; 103 } 104 105 public E e1() { 106 return e1; 107 } 108 109 public E e2() { 110 return e2; 111 } 112 113 public E e3() { 114 return e3; 115 } 116 117 public E e4() { 118 return e4; 119 } 120 121 public static class Unhashables extends SampleElements<UnhashableObject> { 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> { 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 141 Collider(int value) { 142 this.value = value; 143 } 144 145 @Override 146 public boolean equals(Object obj) { 147 return obj instanceof Collider && ((Collider) obj).value == value; 148 } 149 150 @Override 151 public int hashCode() { 152 return 1; // evil! 153 } 154 } 155}