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