|
|
@@ -0,0 +1,111 @@
|
|
|
+/*
|
|
|
+ * @author Ranides Atterwim <ranides@gmail.com>
|
|
|
+ * @copyright Ranides Atterwim
|
|
|
+ * @license WTFPL
|
|
|
+ * @url http://ranides.net/projects/assira
|
|
|
+ */
|
|
|
+package net.ranides.assira.collection.iterators;
|
|
|
+
|
|
|
+import net.ranides.assira.functional.Functions.EachFunction;
|
|
|
+import net.ranides.assira.functional.checked.CheckedSupplier;
|
|
|
+import net.ranides.assira.generic.CompareUtils;
|
|
|
+
|
|
|
+import java.util.*;
|
|
|
+import java.util.function.BiPredicate;
|
|
|
+import java.util.function.Function;
|
|
|
+import java.util.function.Predicate;
|
|
|
+import java.util.stream.Stream;
|
|
|
+import java.util.stream.StreamSupport;
|
|
|
+
|
|
|
+/**
|
|
|
+ *
|
|
|
+ * @author Ranides Atterwim <ranides@gmail.com>
|
|
|
+ */
|
|
|
+public final class IterableUtils {
|
|
|
+
|
|
|
+ private IterableUtils() {
|
|
|
+ /* utility class */
|
|
|
+ }
|
|
|
+
|
|
|
+ public static <T> Optional<T> first(Iterable<? extends T> values) {
|
|
|
+ return IteratorUtils.first(values.iterator());
|
|
|
+ }
|
|
|
+
|
|
|
+ public static <T> Optional<T> first(Iterable<? extends T> values, Predicate<? super T> predicate) {
|
|
|
+ return IteratorUtils.first(values.iterator(), predicate);
|
|
|
+ }
|
|
|
+
|
|
|
+ public static <T> Optional<T> last(Iterable<? extends T> values) {
|
|
|
+ return IteratorUtils.last(values.iterator());
|
|
|
+ }
|
|
|
+
|
|
|
+ public static <T> Optional<T> at(Iterable<? extends T> values, int index) {
|
|
|
+ return IteratorUtils.at(values.iterator(), index);
|
|
|
+ }
|
|
|
+
|
|
|
+ public static <T> Optional<T> last(Iterable<? extends T> values, Predicate<? super T> predicate) {
|
|
|
+ return IteratorUtils.last(values.iterator(), predicate);
|
|
|
+ }
|
|
|
+
|
|
|
+ public static int size(Iterable<?> values) {
|
|
|
+ return IteratorUtils.size(values.iterator());
|
|
|
+ }
|
|
|
+
|
|
|
+ public static <T> Iterable<T> filter(Iterable<? extends T> values, Predicate<? super T> predicate) {
|
|
|
+ return () -> IteratorUtils.filter(values.iterator(), predicate);
|
|
|
+ }
|
|
|
+
|
|
|
+ public static <T> Iterable<T> limit(Iterable<? extends T> values, Predicate<? super T> predicate) {
|
|
|
+ return () -> IteratorUtils.limit(values.iterator(), predicate);
|
|
|
+ }
|
|
|
+
|
|
|
+ public static <S,T> Iterable<T> map(Iterable<? extends S> values, EachFunction<? super S,? extends T> function) {
|
|
|
+ return () -> IteratorUtils.map(values.iterator(), function);
|
|
|
+ }
|
|
|
+
|
|
|
+ public static <S,T> Iterable<T> map(Iterable<? extends S> values, Function<? super S,? extends T> function) {
|
|
|
+ return () -> IteratorUtils.map(values.iterator(), function);
|
|
|
+ }
|
|
|
+
|
|
|
+ public static <T> Iterable<T> flat(Iterable<? extends Iterable<? extends T>> values) {
|
|
|
+ return () -> IteratorUtils.flat(map(values, Iterable::iterator).iterator());
|
|
|
+ }
|
|
|
+
|
|
|
+ @SafeVarargs
|
|
|
+ public static <T> Iterable<T> concat(Iterable<? extends T>... values) {
|
|
|
+ return flat(Arrays.asList(values));
|
|
|
+ }
|
|
|
+
|
|
|
+ public static <T> Stream<T> stream(Iterable<? extends T> values) {
|
|
|
+ return IteratorUtils.stream(values.iterator());
|
|
|
+ }
|
|
|
+
|
|
|
+ public static <T, C extends Collection<? super T>> C collect(Iterable<? extends T> values, C target) {
|
|
|
+ return IteratorUtils.collect(values.iterator(), target);
|
|
|
+ }
|
|
|
+
|
|
|
+ public static <T> boolean equals(Iterable<? extends T> a, Iterable<? extends T> b) {
|
|
|
+ return equals(a, b, CompareUtils::equals);
|
|
|
+ }
|
|
|
+
|
|
|
+ public static <T> boolean equals(Iterable<? extends T> a, Iterable<? extends T> b, BiPredicate<? super T,? super T> predicate) {
|
|
|
+ return IteratorUtils.equals(a.iterator(), b.iterator(), predicate);
|
|
|
+ }
|
|
|
+
|
|
|
+ public static <T> int compare(Iterable<? extends T> a, Iterable<? extends T> b) {
|
|
|
+ return compare(a, b, CompareUtils.comparator());
|
|
|
+ }
|
|
|
+
|
|
|
+ public static <T> int compare(Iterable<? extends T> a, Iterable<? extends T> b, Comparator<? super T> cmp) {
|
|
|
+ return IteratorUtils.compare(a.iterator(), b.iterator(), cmp);
|
|
|
+ }
|
|
|
+
|
|
|
+ public static <T> void remove(Iterable<? extends T> values, Predicate<? super T> predicate) {
|
|
|
+ IteratorUtils.remove(values.iterator(), predicate);
|
|
|
+ }
|
|
|
+
|
|
|
+ public static <T> Iterable<T> iterable(CheckedSupplier<Optional<T>, ?> generator) {
|
|
|
+ return () -> IteratorUtils.iterator(generator);
|
|
|
+ }
|
|
|
+
|
|
|
+}
|