Package net.sourceforge.pmd.util
Class IteratorUtil
- java.lang.Object
-
- net.sourceforge.pmd.util.IteratorUtil
-
-
Nested Class Summary
Nested Classes Modifier and Type Class Description static class
IteratorUtil.AbstractIterator<T>
Note, that this iterator doesn't support theremove
operation.static class
IteratorUtil.AbstractPausingIterator<T>
-
Method Summary
All Methods Static Methods Concrete Methods Modifier and Type Method Description static void
advance(Iterator<?> iterator, int n)
Advancen
times.static <T> boolean
allMatch(Iterator<? extends T> iterator, Predicate<? super T> pred)
Returns whether all elements match the predicate.static <T> boolean
anyMatch(Iterator<? extends T> iterator, Predicate<? super T> pred)
Returns whether some element match the predicate.static <T> Iterable<T>
asReversed(List<T> lst)
static <T> Iterator<T>
coerceWildcard(Iterator<? extends T> it)
Coerce an iterator with a wildcard.static <T> Iterator<T>
concat(Iterator<? extends T> as, Iterator<? extends T> bs)
static int
count(Iterator<?> it)
Counts the items in this iterator, exhausting it.static <T> Iterator<T>
distinct(Iterator<? extends T> iter)
static <T> Iterator<T>
drop(Iterator<? extends T> source, int n)
Produce an iterator whose first element is the nth element of the given source.static <T> Iterator<@NonNull T>
dropLast(Iterator<? extends @Nullable T> it, int n)
Remove the last n elements of the iterator.static <T> Iterator<T>
filter(Iterator<? extends T> it, Predicate<? super T> filter)
static <T> Iterator<@NonNull T>
filterNotNull(Iterator<? extends T> it)
static <T,R>
Iterator<R>flatMap(Iterator<? extends T> iter, Function<? super T,? extends @Nullable Iterator<? extends R>> f)
static <R> Iterator<R>
flatMapWithSelf(Iterator<? extends R> iter, Function<? super R,? extends @Nullable Iterator<? extends R>> f)
Like flatMap, but yields each element of the input iterator before yielding the results of the mapper function.static <T> Iterator<@NonNull T>
generate(@Nullable T seed, Function<? super @NonNull T,? extends @Nullable T> stepper)
Returns an iterator that applies a stepping function to the previous value yielded.static <T> @Nullable T
getNth(Iterator<? extends T> iterator, int n)
Returns the nth element of this iterator, or null if the iterator is shorter.static <T> Iterator<T>
iterate(T... elements)
static <T> @Nullable T
last(Iterator<? extends T> iterator)
static <T,R>
Iterator<R>map(Iterator<? extends T> iter, Function<? super T,? extends R> mapper)
static <T,R>
Iterable<R>mapIterator(Iterable<? extends T> iter, Function<? super Iterator<? extends T>,? extends Iterator<R>> mapper)
Apply a transform on the iterator of an iterable.static <T,R>
Iterator<@NonNull R>mapNotNull(Iterator<? extends T> it, Function<? super T,? extends R> mapper)
static <T> boolean
noneMatch(Iterator<? extends T> iterator, Predicate<? super T> pred)
Returns whether no elements match the predicate.static <T> Iterator<T>
peek(Iterator<? extends T> iter, Consumer<? super T> action)
static <T> Iterator<T>
reverse(Iterator<T> it)
static <T> Iterator<T>
singletonIterator(T value)
static <T> Iterator<T>
take(Iterator<? extends T> iterator, int n)
Limit the number of elements yielded by this iterator to the given number.static <T> Iterator<T>
takeWhile(Iterator<T> iter, Predicate<? super T> predicate)
static <T> Iterable<T>
toIterable(Iterator<T> it)
static <T> List<T>
toList(Iterator<? extends T> it)
static <T> List<@NonNull T>
toNonNullList(Iterator<? extends @Nullable T> it)
static <T> Stream<T>
toStream(Iterable<T> iter)
static <T> Stream<T>
toStream(Iterator<? extends T> iter)
-
-
-
Method Detail
-
takeWhile
public static <T> Iterator<T> takeWhile(Iterator<T> iter, Predicate<? super T> predicate)
-
flatMap
public static <T,R> Iterator<R> flatMap(Iterator<? extends T> iter, Function<? super T,? extends @Nullable Iterator<? extends R>> f)
-
flatMapWithSelf
public static <R> Iterator<R> flatMapWithSelf(Iterator<? extends R> iter, Function<? super R,? extends @Nullable Iterator<? extends R>> f)
Like flatMap, but yields each element of the input iterator before yielding the results of the mapper function. Null elements of the input iterator are both yielded by the returned iterator and passed to the stepper. If the stepper returns null, that result is ignored.
-
mapNotNull
public static <T,R> Iterator<@NonNull R> mapNotNull(Iterator<? extends T> it, Function<? super T,? extends R> mapper)
-
map
public static <T,R> Iterator<R> map(Iterator<? extends T> iter, Function<? super T,? extends R> mapper)
-
mapIterator
public static <T,R> Iterable<R> mapIterator(Iterable<? extends T> iter, Function<? super Iterator<? extends T>,? extends Iterator<R>> mapper)
Apply a transform on the iterator of an iterable.
-
iterate
@SafeVarargs public static <T> Iterator<T> iterate(T... elements)
-
dropLast
public static <T> Iterator<@NonNull T> dropLast(Iterator<? extends @Nullable T> it, int n)
Remove the last n elements of the iterator. This uses n elements as a lookahead.
-
coerceWildcard
public static <T> Iterator<T> coerceWildcard(Iterator<? extends T> it)
Coerce an iterator with a wildcard. This is safe because the Iterator interface is covariant (notListIterator
though).
-
count
public static int count(Iterator<?> it)
Counts the items in this iterator, exhausting it.
-
last
public static <T> @Nullable T last(Iterator<? extends T> iterator)
-
getNth
public static <T> @Nullable T getNth(Iterator<? extends T> iterator, int n)
Returns the nth element of this iterator, or null if the iterator is shorter.- Throws:
IllegalArgumentException
- If n is negative
-
advance
public static void advance(Iterator<?> iterator, int n)
Advancen
times.
-
take
public static <T> Iterator<T> take(Iterator<? extends T> iterator, int n)
Limit the number of elements yielded by this iterator to the given number.
-
drop
public static <T> Iterator<T> drop(Iterator<? extends T> source, int n)
Produce an iterator whose first element is the nth element of the given source.
-
generate
public static <T> Iterator<@NonNull T> generate(@Nullable T seed, Function<? super @NonNull T,? extends @Nullable T> stepper)
Returns an iterator that applies a stepping function to the previous value yielded. Iteration stops on the first null value returned by the stepper.- Type Parameters:
T
- Type of values- Parameters:
seed
- First value. If null then the iterator is emptystepper
- Step function
-
anyMatch
public static <T> boolean anyMatch(Iterator<? extends T> iterator, Predicate<? super T> pred)
Returns whether some element match the predicate. If empty thenfalse
is returned.
-
allMatch
public static <T> boolean allMatch(Iterator<? extends T> iterator, Predicate<? super T> pred)
Returns whether all elements match the predicate. If empty thentrue
is returned.
-
noneMatch
public static <T> boolean noneMatch(Iterator<? extends T> iterator, Predicate<? super T> pred)
Returns whether no elements match the predicate. If empty thentrue
is returned.
-
singletonIterator
public static <T> Iterator<T> singletonIterator(T value)
-
-