net.sf.staccatocommons.iterators.thriter.Thriterators Maven / Gradle / Ivy
/**
* Copyright (c) 2010-2012, The StaccatoCommons Team
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License as published by
* the Free Software Foundation; version 3 of the License.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Lesser General Public License for more details.
*/
package net.sf.staccatocommons.iterators.thriter;
import java.util.Iterator;
import net.sf.staccatocommons.defs.Thunk;
import net.sf.staccatocommons.iterators.ArrayThriterator;
import net.sf.staccatocommons.iterators.EmptyThriterator;
import net.sf.staccatocommons.iterators.SingleThriterator;
import net.sf.staccatocommons.iterators.delayed.DelayedSingleIterator;
import net.sf.staccatocommons.restrictions.Constant;
import net.sf.staccatocommons.restrictions.check.NonNull;
/**
* @author flbulgarelli
*
*/
public class Thriterators {
private Thriterators() {}
/**
* Answers a {@link Thriterator} that retrieves no elements, that is,
* thriterator.isEmpty()
is always true
*
* @param
* @return a constant empty {@link Thriterator}
*/
@Constant
public static Thriterator empty() {
return EmptyThriterator.empty();
}
/**
* Answers a {@link Thriterator} that retrieves the given element
*
* @param
* @param element
* the element to retrieve
* @return a new {@link Thriterator}
*/
@NonNull
public static Thriterator from(A element) {
return new SingleThriterator(element);
}
/**
* Answers a {@link Thriterator} that retrieves elements from the given array
*
* @param
* @param elements
* @return a new {@link Thriterator}
*/
@NonNull
public static Thriterator from(@NonNull A... elements) {
return new ArrayThriterator(elements);
}
/**
* Answers a {@link Thriterator} that retrieves the given thunk's value
*
* @param
* @param thunk
* the thunk whose element is retrieved
* @return a new {@link Thriterator}
*/
public static Thriterator from(@NonNull Thunk thunk) {
return new DelayedSingleIterator(thunk);
}
/**
* Answers a {@link Thriterator} that wraps the given {@link Iterator}. If it
* is already a {@link Thriterator}, this method just returns its argument.
*
* @param
* @param iter
* @return a new {@link Thriterator} that wraps the given iterator, if it is
* not already a {@link Thriterator}. The given iter
,
* otherwise
*/
@NonNull
public static Thriterator from(@NonNull Iterator extends A> iter) {
if (iter instanceof Thriterator)
return (Thriterator) iter;
return new IteratorThriterator(iter);
}
}