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