2
* Copyright (C) 2009 The Guava Authors
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
8
* http://www.apache.org/licenses/LICENSE-2.0
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.
17
package com.google.common.collect.testing;
19
import com.google.common.annotations.GwtCompatible;
20
import java.util.Arrays;
21
import java.util.Collection;
22
import java.util.Iterator;
23
import org.checkerframework.checker.nullness.qual.Nullable;
26
* An implementation of {@code Iterable} which throws an exception on all invocations of the {@link
27
* #iterator()} method after the first, and whose iterator is always unmodifiable.
29
* <p>The {@code Iterable} specification does not make it absolutely clear what should happen on a
30
* second invocation, so implementors have made various choices, including:
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} implementations
36
* have, of returning a new, independent iterator
39
* <p>Because of this situation, any public method accepting an iterable should invoke the {@code
40
* iterator} method only once, and should be tested using this class. Exceptions to this rule should
41
* be clearly documented.
43
* <p>Note that although your APIs should be liberal in what they accept, your methods which
44
* <i>return</i> iterables should make every attempt to return ones of the robust variety.
46
* <p>This testing utility is not thread-safe.
48
* @author Kevin Bourrillion
51
public final class MinimalIterable<E extends @Nullable Object> implements Iterable<E> {
52
/** Returns an iterable whose iterator returns the given elements in order. */
53
public static <E extends @Nullable Object> MinimalIterable<E> of(E... elements) {
54
// Make sure to get an unmodifiable iterator
55
return new MinimalIterable<>(Arrays.asList(elements).iterator());
59
* Returns an iterable whose iterator returns the given elements in order. The elements are copied
60
* out of the source collection at the time this method is called.
62
@SuppressWarnings("unchecked") // Es come in, Es go out
63
public static <E extends @Nullable Object> MinimalIterable<E> from(Collection<E> elements) {
64
return (MinimalIterable) of(elements.toArray());
67
private @Nullable Iterator<E> iterator;
69
private MinimalIterable(Iterator<E> iterator) {
70
this.iterator = iterator;
74
public Iterator<E> iterator() {
75
if (iterator == null) {
76
// TODO: throw something else? Do we worry that people's code and tests
77
// might be relying on this particular type of exception?
78
throw new IllegalStateException();