| 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 |
import org.checkerframework.checker.nullness.qual.Nullable; |
| 24 |
|
| 25 |
/** |
| 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. |
| 28 |
* |
| 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: |
| 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} implementations |
| 36 |
* have, of returning a new, independent iterator |
| 37 |
* </ul> |
| 38 |
* |
| 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. |
| 42 |
* |
| 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. |
| 45 |
* |
| 46 |
* <p>This testing utility is not thread-safe. |
| 47 |
* |
| 48 |
* @author Kevin Bourrillion |
| 49 |
*/ |
| 50 |
@GwtCompatible |
| 51 |
public final class MinimalIterable<E> implements Iterable<E> { |
| 52 |
/** Returns an iterable whose iterator returns the given elements in order. */ |
| 53 |
public static <E> MinimalIterable<E> of(E... elements) { |
| 54 |
// Make sure to get an unmodifiable iterator |
| 55 |
return new MinimalIterable<>(Arrays.asList(elements).iterator()); |
| 56 |
} |
| 57 |
|
| 58 |
/** |
| 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. |
| 61 |
*/ |
| 62 |
@SuppressWarnings("unchecked") // Es come in, Es go out |
| 63 |
public static <E> MinimalIterable<E> from(Collection<E> elements) { |
| 64 |
return (MinimalIterable) of(elements.toArray()); |
| 65 |
} |
| 66 |
|
| 67 |
private @Nullable Iterator<E> iterator; |
| 68 |
|
| 69 |
private MinimalIterable(Iterator<E> iterator) { |
| 70 |
this.iterator = iterator; |
| 71 |
} |
| 72 |
|
| 73 |
@Override |
| 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(); |
| 79 |
} |
| 80 |
try { |
| 81 |
return iterator; |
| 82 |
} finally { |
| 83 |
iterator = null; |
| 84 |
} |
| 85 |
} |
| 86 |
} |