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