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