11d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert/* 21d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert * Copyright (C) 2009 The Guava Authors 31d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert * 41d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert * Licensed under the Apache License, Version 2.0 (the "License"); 51d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert * you may not use this file except in compliance with the License. 61d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert * You may obtain a copy of the License at 71d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert * 81d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert * http://www.apache.org/licenses/LICENSE-2.0 91d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert * 101d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert * Unless required by applicable law or agreed to in writing, software 111d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert * distributed under the License is distributed on an "AS IS" BASIS, 121d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 131d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert * See the License for the specific language governing permissions and 141d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert * limitations under the License. 151d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert */ 161d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert 171d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringertpackage com.google.common.collect.testing; 181d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert 197dd252788645e940eada959bdde927426e2531c9Paul Duffinimport com.google.common.annotations.GwtCompatible; 207dd252788645e940eada959bdde927426e2531c9Paul Duffin 211d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringertimport java.util.Arrays; 221d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringertimport java.util.Collection; 231d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringertimport java.util.Iterator; 241d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert 251d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert/** 261d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert * An implementation of {@code Iterable} which throws an exception on all 271d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert * invocations of the {@link #iterator()} method after the first, and whose 281d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert * iterator is always unmodifiable. 291d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert * 301d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert * <p>The {@code Iterable} specification does not make it absolutely clear what 311d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert * should happen on a second invocation, so implementors have made various 321d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert * choices, including: 331d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert * 341d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert * <ul> 351d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert * <li>returning the same iterator again 361d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert * <li>throwing an exception of some kind 371d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert * <li>or the usual, <i>robust</i> behavior, which all known {@link Collection} 381d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert * implementations have, of returning a new, independent iterator 391d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert * </ul> 401d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert * 410888a09821a98ac0680fad765217302858e70fa4Paul Duffin * <p>Because of this situation, any public method accepting an iterable should 421d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert * invoke the {@code iterator} method only once, and should be tested using this 431d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert * class. Exceptions to this rule should be clearly documented. 441d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert * 451d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert * <p>Note that although your APIs should be liberal in what they accept, your 461d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert * methods which <i>return</i> iterables should make every attempt to return 471d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert * ones of the robust variety. 481d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert * 491d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert * <p>This testing utility is not thread-safe. 501d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert * 511d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert * @author Kevin Bourrillion 521d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert */ 537dd252788645e940eada959bdde927426e2531c9Paul Duffin@GwtCompatible 541d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringertpublic final class MinimalIterable<E> implements Iterable<E> { 551d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert /** 561d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert * Returns an iterable whose iterator returns the given elements in order. 571d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert */ 581d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert public static <E> MinimalIterable<E> of(E... elements) { 591d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert // Make sure to get an unmodifiable iterator 601d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert return new MinimalIterable<E>(Arrays.asList(elements).iterator()); 611d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert } 621d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert 631d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert /** 641d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert * Returns an iterable whose iterator returns the given elements in order. 651d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert * The elements are copied out of the source collection at the time this 661d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert * method is called. 671d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert */ 681d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert @SuppressWarnings("unchecked") // Es come in, Es go out 691d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert public static <E> MinimalIterable<E> from(final Collection<E> elements) { 701d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert return (MinimalIterable) of(elements.toArray()); 711d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert } 721d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert 731d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert private Iterator<E> iterator; 741d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert 751d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert private MinimalIterable(Iterator<E> iterator) { 761d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert this.iterator = iterator; 771d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert } 781d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert 791d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert @Override 801d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert public Iterator<E> iterator() { 811d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert if (iterator == null) { 821d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert // TODO: throw something else? Do we worry that people's code and tests 831d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert // might be relying on this particular type of exception? 841d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert throw new IllegalStateException(); 851d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert } 861d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert try { 871d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert return iterator; 881d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert } finally { 891d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert iterator = null; 901d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert } 911d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert } 921d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert} 93