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