001/*
002 * Copyright (C) 2009 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.Collection;
022import java.util.Iterator;
023import org.checkerframework.checker.nullness.qual.Nullable;
024
025/**
026 * An implementation of {@code Iterable} which throws an exception on all invocations of the {@link
027 * #iterator()} method after the first, and whose iterator is always unmodifiable.
028 *
029 * <p>The {@code Iterable} specification does not make it absolutely clear what should happen on a
030 * second invocation, so implementors have made various choices, including:
031 *
032 * <ul>
033 *   <li>returning the same iterator again
034 *   <li>throwing an exception of some kind
035 *   <li>or the usual, <i>robust</i> behavior, which all known {@link Collection} implementations
036 *       have, of returning a new, independent iterator
037 * </ul>
038 *
039 * <p>Because of this situation, any public method accepting an iterable should invoke the {@code
040 * iterator} method only once, and should be tested using this class. Exceptions to this rule should
041 * be clearly documented.
042 *
043 * <p>Note that although your APIs should be liberal in what they accept, your methods which
044 * <i>return</i> iterables should make every attempt to return ones of the robust variety.
045 *
046 * <p>This testing utility is not thread-safe.
047 *
048 * @author Kevin Bourrillion
049 */
050@GwtCompatible
051public final class MinimalIterable<E extends @Nullable Object> implements Iterable<E> {
052  /** Returns an iterable whose iterator returns the given elements in order. */
053  public static <E extends @Nullable Object> MinimalIterable<E> of(E... elements) {
054    // Make sure to get an unmodifiable iterator
055    return new MinimalIterable<>(Arrays.asList(elements).iterator());
056  }
057
058  /**
059   * Returns an iterable whose iterator returns the given elements in order. The elements are copied
060   * out of the source collection at the time this method is called.
061   */
062  @SuppressWarnings("unchecked") // Es come in, Es go out
063  public static <E extends @Nullable Object> MinimalIterable<E> from(Collection<E> elements) {
064    return (MinimalIterable) of(elements.toArray());
065  }
066
067  private @Nullable Iterator<E> iterator;
068
069  private MinimalIterable(Iterator<E> iterator) {
070    this.iterator = iterator;
071  }
072
073  @Override
074  public Iterator<E> iterator() {
075    if (iterator == null) {
076      // TODO: throw something else? Do we worry that people's code and tests
077      // might be relying on this particular type of exception?
078      throw new IllegalStateException();
079    }
080    try {
081      return iterator;
082    } finally {
083      iterator = null;
084    }
085  }
086}