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 
21 import java.util.Arrays;
22 import java.util.Collection;
23 import java.util.Iterator;
24 
25 /**
26  * An implementation of {@code Iterable} which throws an exception on all
27  * invocations of the {@link #iterator()} method after the first, and whose
28  * iterator is always unmodifiable.
29  *
30  * <p>The {@code Iterable} specification does not make it absolutely clear what
31  * should happen on a second invocation, so implementors have made various
32  * choices, including:
33  *
34  * <ul>
35  * <li>returning the same iterator again
36  * <li>throwing an exception of some kind
37  * <li>or the usual, <i>robust</i> behavior, which all known {@link Collection}
38  *     implementations have, of returning a new, independent iterator
39  * </ul>
40  *
41  * <p>Because of this situation, any public method accepting an iterable should
42  * invoke the {@code iterator} method only once, and should be tested using this
43  * class. Exceptions to this rule should be clearly documented.
44  *
45  * <p>Note that although your APIs should be liberal in what they accept, your
46  * methods which <i>return</i> iterables should make every attempt to return
47  * ones of the robust variety.
48  *
49  * <p>This testing utility is not thread-safe.
50  *
51  * @author Kevin Bourrillion
52  */
53 @GwtCompatible
54 public final class MinimalIterable<E> implements Iterable<E> {
55   /**
56    * Returns an iterable whose iterator returns the given elements in order.
57    */
58   public static <E> MinimalIterable<E> of(E... elements) {
59     // Make sure to get an unmodifiable iterator
60     return new MinimalIterable<E>(Arrays.asList(elements).iterator());
61   }
62 
63   /**
64    * Returns an iterable whose iterator returns the given elements in order.
65    * The elements are copied out of the source collection at the time this
66    * method is called.
67    */
68   @SuppressWarnings("unchecked") // Es come in, Es go out
69   public static <E> MinimalIterable<E> from(final Collection<E> elements) {
70     return (MinimalIterable) of(elements.toArray());
71   }
72 
73   private Iterator<E> iterator;
74 
75   private MinimalIterable(Iterator<E> iterator) {
76     this.iterator = iterator;
77   }
78 
79   @Override
80   public Iterator<E> iterator() {
81     if (iterator == null) {
82       // TODO: throw something else? Do we worry that people's code and tests
83       // might be relying on this particular type of exception?
84       throw new IllegalStateException();
85     }
86     try {
87       return iterator;
88     } finally {
89       iterator = null;
90     }
91   }
92 }
93