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; 024import org.checkerframework.checker.nullness.qual.Nullable; 025 026/** 027 * A container class for the five sample elements we need for testing. 028 * 029 * @author Kevin Bourrillion 030 */ 031@GwtCompatible 032@ElementTypesAreNonnullByDefault 033public class SampleElements<E extends @Nullable Object> implements Iterable<E> { 034 // TODO: rename e3, e4 => missing1, missing2 035 private final E e0; 036 private final E e1; 037 private final E e2; 038 private final E e3; 039 private final E e4; 040 041 public SampleElements(E e0, E e1, E e2, E e3, E e4) { 042 this.e0 = e0; 043 this.e1 = e1; 044 this.e2 = e2; 045 this.e3 = e3; 046 this.e4 = e4; 047 } 048 049 @Override 050 public Iterator<E> iterator() { 051 return asList().iterator(); 052 } 053 054 public List<E> asList() { 055 return Arrays.asList(e0(), e1(), e2(), e3(), e4()); 056 } 057 058 public static class Strings extends SampleElements<String> { 059 public Strings() { 060 // elements aren't sorted, to better test SortedSet iteration ordering 061 super("b", "a", "c", "d", "e"); 062 } 063 064 // for testing SortedSet and SortedMap methods 065 public static final String BEFORE_FIRST = "\0"; 066 public static final String BEFORE_FIRST_2 = "\0\0"; 067 public static final String MIN_ELEMENT = "a"; 068 public static final String AFTER_LAST = "z"; 069 public static final String AFTER_LAST_2 = "zz"; 070 } 071 072 public static class Chars extends SampleElements<Character> { 073 public Chars() { 074 // elements aren't sorted, to better test SortedSet iteration ordering 075 super('b', 'a', 'c', 'd', 'e'); 076 } 077 } 078 079 public static class Enums extends SampleElements<AnEnum> { 080 public Enums() { 081 // elements aren't sorted, to better test SortedSet iteration ordering 082 super(AnEnum.B, AnEnum.A, AnEnum.C, AnEnum.D, AnEnum.E); 083 } 084 } 085 086 public static class Ints extends SampleElements<Integer> { 087 public Ints() { 088 // elements aren't sorted, to better test SortedSet iteration ordering 089 super(1, 0, 2, 3, 4); 090 } 091 } 092 093 public static <K extends @Nullable Object, V extends @Nullable Object> 094 SampleElements<Entry<K, V>> mapEntries(SampleElements<K> keys, SampleElements<V> values) { 095 return new SampleElements<>( 096 Helpers.mapEntry(keys.e0(), values.e0()), 097 Helpers.mapEntry(keys.e1(), values.e1()), 098 Helpers.mapEntry(keys.e2(), values.e2()), 099 Helpers.mapEntry(keys.e3(), values.e3()), 100 Helpers.mapEntry(keys.e4(), values.e4())); 101 } 102 103 public E e0() { 104 return e0; 105 } 106 107 public E e1() { 108 return e1; 109 } 110 111 public E e2() { 112 return e2; 113 } 114 115 public E e3() { 116 return e3; 117 } 118 119 public E e4() { 120 return e4; 121 } 122 123 public static class Unhashables extends SampleElements<UnhashableObject> { 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> { 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 143 Collider(int value) { 144 this.value = value; 145 } 146 147 @Override 148 public boolean equals(@Nullable Object obj) { 149 return obj instanceof Collider && ((Collider) obj).value == value; 150 } 151 152 @Override 153 public int hashCode() { 154 return 1; // evil! 155 } 156 } 157}