org.d2ab.iterable.Iterables Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of sequence Show documentation
Show all versions of sequence Show documentation
A lightweight alternative to Java 8 sequential Stream
/*
* Copyright 2016 Daniel Skogquist Åborg
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.d2ab.iterable;
import org.d2ab.util.Pair;
import java.util.Iterator;
import java.util.function.Predicate;
import java.util.stream.Stream;
import static java.util.Arrays.asList;
import static java.util.Objects.requireNonNull;
public class Iterables {
private Iterables() {
}
public static Iterable from(Iterator iterator) {
requireNonNull(iterator);
return () -> iterator;
}
@SafeVarargs
public static Iterable from(T... objects) {
return asList(requireNonNull(objects));
}
public static Iterable from(Stream stream) {
return requireNonNull(stream)::iterator;
}
/**
* Converts a container of some kind into a possibly once-only {@link Iterable}.
*
* @param container the non-null container to turn into an {@link Iterable}, can be one of {@link Iterable}, {@link
* Iterator}, {@link Stream} or {@code Array}.
*
* @return the container as an iterable.
*
* @throws ClassCastException if the container is not one of {@link Iterable}, {@link Iterator}, {@link Stream} or
* {@code Array}
*/
@SuppressWarnings("unchecked")
public static Iterable from(Object container) {
requireNonNull(container);
if (container instanceof Iterable)
return (Iterable) container;
else if (container instanceof Iterator)
return from((Iterator) container);
else if (container instanceof Stream)
return from((Stream) container);
else if (container instanceof Object[])
return from((T[]) container);
else if (container instanceof Pair)
return from((Iterable) ((Pair) container)::iterator);
else
throw new ClassCastException(
"Required an Iterable, Iterator, Array or Stream but got: " + container.getClass());
}
/**
* @return true if all elements in this {@code Sequence} satisfy the given predicate, false otherwise.
*/
public static boolean all(Iterable iterable, Predicate super T> predicate) {
for (T each : iterable) {
if (!predicate.test(each))
return false;
}
return true;
}
/**
* @return true if no elements in this {@code Sequence} satisfy the given predicate, false otherwise.
*/
public static boolean none(Iterable iterable, Predicate super T> predicate) {
return !any(iterable, predicate);
}
/**
* @return true if any element in this {@code Sequence} satisfies the given predicate, false otherwise.
*/
public static boolean any(Iterable iterable, Predicate super T> predicate) {
for (T each : iterable) {
if (predicate.test(each))
return true;
}
return false;
}
public static void removeAll(Iterable iterable) {
for (Iterator iterator = iterable.iterator(); iterator.hasNext(); ) {
iterator.next();
iterator.remove();
}
}
}