diff --git a/fluentinterface/src/main/java/com/iluwatar/fluentinterface/fluentiterable/FluentIterable.java b/fluentinterface/src/main/java/com/iluwatar/fluentinterface/fluentiterable/FluentIterable.java index 7bdaf274c..919cf5664 100644 --- a/fluentinterface/src/main/java/com/iluwatar/fluentinterface/fluentiterable/FluentIterable.java +++ b/fluentinterface/src/main/java/com/iluwatar/fluentinterface/fluentiterable/FluentIterable.java @@ -4,7 +4,6 @@ import java.util.ArrayList; import java.util.Iterator; import java.util.List; import java.util.Optional; -import java.util.function.Consumer; import java.util.function.Function; import java.util.function.Predicate; @@ -18,7 +17,7 @@ import java.util.function.Predicate; public interface FluentIterable extends Iterable { /** - * Filters the iteration with the given predicate. + * Filters the contents of Iterable using the given predicate, leaving only the ones which satisfy the predicate. * @param predicate the condition to test with for the filtering. If the test * is negative, the tested object is removed by the iterator. * @return a filtered FluentIterable @@ -26,13 +25,8 @@ public interface FluentIterable extends Iterable { FluentIterable filter(Predicate predicate); /** - * Uses the Iterable interface's forEach method to apply a given function - * for each object of the iterator. This is a terminating operation. - */ - void forEachDo(Consumer action); - - /** - * Evaluates the iteration and returns the first element. This is a terminating operation. + * Returns an Optional containing the first element of this iterable if present, + * else returns Optional.empty(). * @return the first element after the iteration is evaluated */ Optional first(); @@ -62,6 +56,11 @@ public interface FluentIterable extends Iterable { * @return a new FluentIterable of the new type */ FluentIterable map(Function function); + + /** + * Returns the contents of this Iterable as a List. + * @return a List representation of this Iterable + */ List asList(); /** diff --git a/fluentinterface/src/main/java/com/iluwatar/fluentinterface/fluentiterable/lazy/DecoratingIterator.java b/fluentinterface/src/main/java/com/iluwatar/fluentinterface/fluentiterable/lazy/DecoratingIterator.java index 0e5b410cc..35c4cc0ae 100644 --- a/fluentinterface/src/main/java/com/iluwatar/fluentinterface/fluentiterable/lazy/DecoratingIterator.java +++ b/fluentinterface/src/main/java/com/iluwatar/fluentinterface/fluentiterable/lazy/DecoratingIterator.java @@ -22,7 +22,7 @@ public abstract class DecoratingIterator implements Iterator { } /** - * Precomputes and caches the next element of the iteration. + * Precomputes and saves the next element of the Iterable. null is considered as end of data. * @return true if a next element is available */ @Override @@ -32,22 +32,24 @@ public abstract class DecoratingIterator implements Iterator { } /** - * Returns the next element of the iteration. This implementation caches it. - * If no next element is cached, it is computed. - * @return the next element obf the iteration + * Returns the next element of the Iterable. + * @return the next element of the Iterable, or null if not present. */ @Override public final TYPE next() { - TYPE result = next; - next = null; - result = (result == null ? fromIterator.next() : result); - return result; + if (next == null) { + return fromIterator.next(); + } else { + final TYPE result = next; + next = null; + return result; + } } /** - * Computes the next object of the iteration. Can be implemented to - * realize custom behaviour for an iteration process. - * @return + * Computes the next object of the Iterable. Can be implemented to + * realize custom behaviour for an iteration process. null is considered as end of data. + * @return the next element of the Iterable. */ public abstract TYPE computeNext(); } diff --git a/fluentinterface/src/main/java/com/iluwatar/fluentinterface/fluentiterable/lazy/LazyFluentIterable.java b/fluentinterface/src/main/java/com/iluwatar/fluentinterface/fluentiterable/lazy/LazyFluentIterable.java index c6db4d2cd..27bca1bf6 100644 --- a/fluentinterface/src/main/java/com/iluwatar/fluentinterface/fluentiterable/lazy/LazyFluentIterable.java +++ b/fluentinterface/src/main/java/com/iluwatar/fluentinterface/fluentiterable/lazy/LazyFluentIterable.java @@ -37,7 +37,7 @@ public class LazyFluentIterable implements FluentIterable { } /** - * Adds a filter operation to the operation chain and returns a new iterable. + * Filters the contents of Iterable using the given predicate, leaving only the ones which satisfy the predicate. * @param predicate the condition to test with for the filtering. If the test * is negative, the tested object is removed by the iterator. * @return a new FluentIterable object that decorates the source iterable @@ -50,53 +50,33 @@ public class LazyFluentIterable implements FluentIterable { return new DecoratingIterator(iterable.iterator()) { @Override public TYPE computeNext() { - while(true) { - if(fromIterator.hasNext()) { - TYPE candidate = fromIterator.next(); - if(!predicate.test(candidate)) { - continue; - } - return candidate; + while(fromIterator.hasNext()) { + TYPE candidate = fromIterator.next(); + if(!predicate.test(candidate)) { + continue; } - - return null; + return candidate; } + + return null; } }; } }; } - /** - * Uses the Iterable interface's forEach method to apply a given function - * for each object of the iterator. Is a terminating operation. - * @param action the action for each object - */ - @Override - public void forEachDo(Consumer action) { - Iterator newIterator = iterable.iterator(); - while(newIterator.hasNext()) { - action.accept(newIterator.next()); - } - } - /** * Can be used to collect objects from the iteration. Is a terminating operation. - * @return an option of the first object of the iteration + * @return an Optional containing the first object of this Iterable */ @Override public Optional first() { - Optional result = Optional.empty(); - List list = first(1).asList(); - if(!list.isEmpty()) { - result = Optional.of(list.get(0)); - } - - return result; + Iterator resultIterator = first(1).iterator(); + return resultIterator.hasNext() ? Optional.of(resultIterator.next()) : Optional.empty(); } /** - * Can be used to collect objects from the iteration. Is a terminating operation. + * Can be used to collect objects from the iteration. * @param count defines the number of objects to return * @return the same FluentIterable with a collection decimated to a maximum of 'count' first objects. */ @@ -126,21 +106,18 @@ public class LazyFluentIterable implements FluentIterable { /** * Can be used to collect objects from the iteration. Is a terminating operation. - * @return an option of the last object of the iteration + * @return an Optional containing the last object of this Iterable */ @Override public Optional last() { - Optional result = Optional.empty(); - List list = last(1).asList(); - if(!list.isEmpty()) { - result = Optional.of(list.get(0)); - } - - return result; + Iterator resultIterator = last(1).iterator(); + return resultIterator.hasNext() ? Optional.of(resultIterator.next()) : Optional.empty(); } /** - * Can be used to collect objects from the iteration. Is a terminating operation. + * Can be used to collect objects from the Iterable. Is a terminating operation. + * This operation is memory intensive, because the contents of this Iterable + * are collected into a List, when the next object is requested. * @param count defines the number of objects to return * @return the same FluentIterable with a collection decimated to a maximum of 'count' last objects */ @@ -193,13 +170,11 @@ public class LazyFluentIterable implements FluentIterable { Iterator oldTypeIterator = iterable.iterator(); @Override public NEW_TYPE computeNext() { - while(true) { - if(oldTypeIterator.hasNext()) { - TYPE candidate = oldTypeIterator.next(); - return function.apply(candidate); - } - return null; + while(oldTypeIterator.hasNext()) { + TYPE candidate = oldTypeIterator.next(); + return function.apply(candidate); } + return null; } }; } diff --git a/fluentinterface/src/main/java/com/iluwatar/fluentinterface/fluentiterable/simple/SimpleFluentIterable.java b/fluentinterface/src/main/java/com/iluwatar/fluentinterface/fluentiterable/simple/SimpleFluentIterable.java index efaa87bbb..a4bf77218 100644 --- a/fluentinterface/src/main/java/com/iluwatar/fluentinterface/fluentiterable/simple/SimpleFluentIterable.java +++ b/fluentinterface/src/main/java/com/iluwatar/fluentinterface/fluentiterable/simple/SimpleFluentIterable.java @@ -26,7 +26,7 @@ public class SimpleFluentIterable implements FluentIterable { } /** - * Iterates over all elements of this iterator and filters them. + * Filters the contents of Iterable using the given predicate, leaving only the ones which satisfy the predicate. * @param predicate the condition to test with for the filtering. If the test * is negative, the tested object is removed by the iterator. * @return the same FluentIterable with a filtered collection @@ -44,30 +44,17 @@ public class SimpleFluentIterable implements FluentIterable { } /** - * Uses the Iterable interface's forEach method to apply a given function - * for each object of the iterator. Is a terminating operation. - * @param action the action for each object - */ - @Override - public void forEachDo(Consumer action) { - iterable.forEach(action); - } - - /** - * Can be used to collect objects from the iteration. Is a terminating operation. - * @return an option of the first object of the iteration + * Can be used to collect objects from the Iterable. Is a terminating operation. + * @return an option of the first object of the Iterable */ @Override public final Optional first() { - List list = first(1).asList(); - if(list.isEmpty()) { - return Optional.empty(); - } - return Optional.of(list.get(0)); + Iterator resultIterator = first(1).iterator(); + return resultIterator.hasNext() ? Optional.of(resultIterator.next()) : Optional.empty(); } /** - * Can be used to collect objects from the iteration. Is a terminating operation. + * Can be used to collect objects from the Iterable. Is a terminating operation. * @param count defines the number of objects to return * @return the same FluentIterable with a collection decimated to a maximum of 'count' first objects. */ @@ -86,8 +73,8 @@ public class SimpleFluentIterable implements FluentIterable { } /** - * Can be used to collect objects from the iteration. Is a terminating operation. - * @return an option of the last object of the iteration + * Can be used to collect objects from the Iterable. Is a terminating operation. + * @return an option of the last object of the Iterable */ @Override public final Optional last() { @@ -99,7 +86,7 @@ public class SimpleFluentIterable implements FluentIterable { } /** - * Can be used to collect objects from the iteration. Is a terminating operation. + * Can be used to collect objects from the Iterable. Is a terminating operation. * @param count defines the number of objects to return * @return the same FluentIterable with a collection decimated to a maximum of 'count' last objects */ @@ -136,8 +123,8 @@ public class SimpleFluentIterable implements FluentIterable { } /** - * Collects all remaining objects of this iteration into a list. - * @return a list with all remaining objects of this iteration + * Collects all remaining objects of this Iterable into a list. + * @return a list with all remaining objects of this Iterable */ @Override public List asList() { @@ -168,7 +155,7 @@ public class SimpleFluentIterable implements FluentIterable { } /** - * @return the count of remaining objects in the current iteration + * @return the count of remaining objects of the current Iterable */ public final int getRemainingElementsCount() { int counter = 0; @@ -181,7 +168,7 @@ public class SimpleFluentIterable implements FluentIterable { } /** - * Collects the remaining objects of the given iterators iteration into an List. + * Collects the remaining objects of the given iterator into a List. * @return a new List with the remaining objects. */ public static List toList(Iterator iterator) {