Coverage Summary for Class: MinimalIterable (com.google.common.collect.testing)

Class Class, % Method, % Line, %
MinimalIterable 100% (1/1) 100% (4/4) 90% (9/10)


1 /* 2  * Copyright (C) 2009 The Guava Authors 3  * 4  * Licensed under the Apache License, Version 2.0 (the "License"); 5  * you may not use this file except in compliance with the License. 6  * You may obtain a copy of the License at 7  * 8  * http://www.apache.org/licenses/LICENSE-2.0 9  * 10  * Unless required by applicable law or agreed to in writing, software 11  * distributed under the License is distributed on an "AS IS" BASIS, 12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13  * See the License for the specific language governing permissions and 14  * limitations under the License. 15  */ 16  17 package com.google.common.collect.testing; 18  19 import com.google.common.annotations.GwtCompatible; 20 import java.util.Arrays; 21 import java.util.Collection; 22 import java.util.Iterator; 23  24 /** 25  * An implementation of {@code Iterable} which throws an exception on all invocations of the {@link 26  * #iterator()} method after the first, and whose iterator is always unmodifiable. 27  * 28  * <p>The {@code Iterable} specification does not make it absolutely clear what should happen on a 29  * second invocation, so implementors have made various choices, including: 30  * 31  * <ul> 32  * <li>returning the same iterator again 33  * <li>throwing an exception of some kind 34  * <li>or the usual, <i>robust</i> behavior, which all known {@link Collection} implementations 35  * have, of returning a new, independent iterator 36  * </ul> 37  * 38  * <p>Because of this situation, any public method accepting an iterable should invoke the {@code 39  * iterator} method only once, and should be tested using this class. Exceptions to this rule should 40  * be clearly documented. 41  * 42  * <p>Note that although your APIs should be liberal in what they accept, your methods which 43  * <i>return</i> iterables should make every attempt to return ones of the robust variety. 44  * 45  * <p>This testing utility is not thread-safe. 46  * 47  * @author Kevin Bourrillion 48  */ 49 @GwtCompatible 50 public final class MinimalIterable<E> implements Iterable<E> { 51  /** Returns an iterable whose iterator returns the given elements in order. */ 52  public static <E> MinimalIterable<E> of(E... elements) { 53  // Make sure to get an unmodifiable iterator 54  return new MinimalIterable<E>(Arrays.asList(elements).iterator()); 55  } 56  57  /** 58  * Returns an iterable whose iterator returns the given elements in order. The elements are copied 59  * out of the source collection at the time this method is called. 60  */ 61  @SuppressWarnings("unchecked") // Es come in, Es go out 62  public static <E> MinimalIterable<E> from(final Collection<E> elements) { 63  return (MinimalIterable) of(elements.toArray()); 64  } 65  66  private Iterator<E> iterator; 67  68  private MinimalIterable(Iterator<E> iterator) { 69  this.iterator = iterator; 70  } 71  72  @Override 73  public Iterator<E> iterator() { 74  if (iterator == null) { 75  // TODO: throw something else? Do we worry that people's code and tests 76  // might be relying on this particular type of exception? 77  throw new IllegalStateException(); 78  } 79  try { 80  return iterator; 81  } finally { 82  iterator = null; 83  } 84  } 85 }