diff --git a/fluentinterface/src/main/java/com/iluwatar/fluentinterface/app/App.java b/fluentinterface/src/main/java/com/iluwatar/fluentinterface/app/App.java index cd69a2cbb..1be2b1e70 100644 --- a/fluentinterface/src/main/java/com/iluwatar/fluentinterface/app/App.java +++ b/fluentinterface/src/main/java/com/iluwatar/fluentinterface/app/App.java @@ -114,14 +114,14 @@ public class App { return integer -> integer > 0; } - private static void prettyPrint(String prefix, Iterable iterable) { + private static void prettyPrint(String prefix, Iterable iterable) { prettyPrint(", ", prefix, iterable); } - private static void prettyPrint(String delimiter, String prefix, - Iterable iterable) { + private static void prettyPrint(String delimiter, String prefix, + Iterable iterable) { StringJoiner joiner = new StringJoiner(delimiter, prefix, "."); - Iterator iterator = iterable.iterator(); + Iterator iterator = iterable.iterator(); while (iterator.hasNext()) { joiner.add(iterator.next().toString()); } 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 d99a14315..a134814dd 100644 --- a/fluentinterface/src/main/java/com/iluwatar/fluentinterface/fluentiterable/FluentIterable.java +++ b/fluentinterface/src/main/java/com/iluwatar/fluentinterface/fluentiterable/FluentIterable.java @@ -34,9 +34,9 @@ import java.util.function.Predicate; * the fluent interface design pattern. This interface defines common operations, but doesn't aim to * be complete. It was inspired by Guava's com.google.common.collect.FluentIterable. * - * @param is the class of objects the iterable contains + * @param is the class of objects the iterable contains */ -public interface FluentIterable extends Iterable { +public interface FluentIterable extends Iterable { /** * Filters the contents of Iterable using the given predicate, leaving only the ones which satisfy @@ -46,7 +46,7 @@ public interface FluentIterable extends Iterable { * tested object is removed by the iterator. * @return a filtered FluentIterable */ - FluentIterable filter(Predicate predicate); + FluentIterable filter(Predicate predicate); /** * Returns an Optional containing the first element of this iterable if present, else returns @@ -54,55 +54,55 @@ public interface FluentIterable extends Iterable { * * @return the first element after the iteration is evaluated */ - Optional first(); + Optional first(); /** * Evaluates the iteration and leaves only the count first elements. * * @return the first count elements as an Iterable */ - FluentIterable first(int count); + FluentIterable first(int count); /** * Evaluates the iteration and returns the last element. This is a terminating operation. * * @return the last element after the iteration is evaluated */ - Optional last(); + Optional last(); /** * Evaluates the iteration and leaves only the count last elements. * * @return the last counts elements as an Iterable */ - FluentIterable last(int count); + FluentIterable last(int count); /** - * Transforms this FluentIterable into a new one containing objects of the type NEW_TYPE. + * Transforms this FluentIterable into a new one containing objects of the type T. * - * @param function a function that transforms an instance of TYPE into an instance of NEW_TYPE - * @param the target type of the transformation + * @param function a function that transforms an instance of E into an instance of T + * @param the target type of the transformation * @return a new FluentIterable of the new type */ - FluentIterable map(Function function); + FluentIterable map(Function function); /** * Returns the contents of this Iterable as a List. * * @return a List representation of this Iterable */ - List asList(); + List asList(); /** * Utility method that iterates over iterable and adds the contents to a list. * * @param iterable the iterable to collect - * @param the type of the objects to iterate + * @param the type of the objects to iterate * @return a list with all objects of the given iterator */ - static List copyToList(Iterable iterable) { - ArrayList copy = new ArrayList<>(); - Iterator iterator = iterable.iterator(); + static List copyToList(Iterable iterable) { + ArrayList copy = new ArrayList<>(); + Iterator iterator = iterable.iterator(); while (iterator.hasNext()) { copy.add(iterator.next()); } 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 d15cecea7..07d7f5739 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 @@ -28,16 +28,16 @@ import java.util.Iterator; * This class is used to realize LazyFluentIterables. It decorates a given iterator. Does not * support consecutive hasNext() calls. */ -public abstract class DecoratingIterator implements Iterator { +public abstract class DecoratingIterator implements Iterator { - protected final Iterator fromIterator; + protected final Iterator fromIterator; - private TYPE next; + private E next; /** * Creates an iterator that decorates the given iterator. */ - public DecoratingIterator(Iterator fromIterator) { + public DecoratingIterator(Iterator fromIterator) { this.fromIterator = fromIterator; } @@ -58,11 +58,11 @@ public abstract class DecoratingIterator implements Iterator { * @return the next element of the Iterable, or null if not present. */ @Override - public final TYPE next() { + public final E next() { if (next == null) { return fromIterator.next(); } else { - final TYPE result = next; + final E result = next; next = null; return result; } @@ -74,5 +74,5 @@ public abstract class DecoratingIterator implements Iterator { * * @return the next element of the Iterable. */ - public abstract TYPE computeNext(); + public abstract E 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 8b7f88101..63247875c 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 @@ -35,18 +35,18 @@ import com.iluwatar.fluentinterface.fluentiterable.FluentIterable; * This is a lazy implementation of the FluentIterable interface. It evaluates all chained * operations when a terminating operation is applied. * - * @param the type of the objects the iteration is about + * @param the type of the objects the iteration is about */ -public class LazyFluentIterable implements FluentIterable { +public class LazyFluentIterable implements FluentIterable { - private final Iterable iterable; + private final Iterable iterable; /** * This constructor creates a new LazyFluentIterable. It wraps the given iterable. * * @param iterable the iterable this FluentIterable works on. */ - protected LazyFluentIterable(Iterable iterable) { + protected LazyFluentIterable(Iterable iterable) { this.iterable = iterable; } @@ -66,15 +66,15 @@ public class LazyFluentIterable implements FluentIterable { * @return a new FluentIterable object that decorates the source iterable */ @Override - public FluentIterable filter(Predicate predicate) { - return new LazyFluentIterable() { + public FluentIterable filter(Predicate predicate) { + return new LazyFluentIterable() { @Override - public Iterator iterator() { - return new DecoratingIterator(iterable.iterator()) { + public Iterator iterator() { + return new DecoratingIterator(iterable.iterator()) { @Override - public TYPE computeNext() { + public E computeNext() { while (fromIterator.hasNext()) { - TYPE candidate = fromIterator.next(); + E candidate = fromIterator.next(); if (predicate.test(candidate)) { return candidate; } @@ -93,8 +93,8 @@ public class LazyFluentIterable implements FluentIterable { * @return an Optional containing the first object of this Iterable */ @Override - public Optional first() { - Iterator resultIterator = first(1).iterator(); + public Optional first() { + Iterator resultIterator = first(1).iterator(); return resultIterator.hasNext() ? Optional.of(resultIterator.next()) : Optional.empty(); } @@ -106,17 +106,17 @@ public class LazyFluentIterable implements FluentIterable { * objects. */ @Override - public FluentIterable first(int count) { - return new LazyFluentIterable() { + public FluentIterable first(int count) { + return new LazyFluentIterable() { @Override - public Iterator iterator() { - return new DecoratingIterator(iterable.iterator()) { + public Iterator iterator() { + return new DecoratingIterator(iterable.iterator()) { int currentIndex; @Override - public TYPE computeNext() { + public E computeNext() { if (currentIndex < count && fromIterator.hasNext()) { - TYPE candidate = fromIterator.next(); + E candidate = fromIterator.next(); currentIndex++; return candidate; } @@ -133,8 +133,8 @@ public class LazyFluentIterable implements FluentIterable { * @return an Optional containing the last object of this Iterable */ @Override - public Optional last() { - Iterator resultIterator = last(1).iterator(); + public Optional last() { + Iterator resultIterator = last(1).iterator(); return resultIterator.hasNext() ? Optional.of(resultIterator.next()) : Optional.empty(); } @@ -148,21 +148,21 @@ public class LazyFluentIterable implements FluentIterable { * objects */ @Override - public FluentIterable last(int count) { - return new LazyFluentIterable() { + public FluentIterable last(int count) { + return new LazyFluentIterable() { @Override - public Iterator iterator() { - return new DecoratingIterator(iterable.iterator()) { + public Iterator iterator() { + return new DecoratingIterator(iterable.iterator()) { private int stopIndex; private int totalElementsCount; - private List list; + private List list; private int currentIndex; @Override - public TYPE computeNext() { + public E computeNext() { initialize(); - TYPE candidate = null; + E candidate = null; while (currentIndex < stopIndex && fromIterator.hasNext()) { currentIndex++; fromIterator.next(); @@ -176,7 +176,7 @@ public class LazyFluentIterable implements FluentIterable { private void initialize() { if (list == null) { list = new ArrayList<>(); - Iterator newIterator = iterable.iterator(); + Iterator newIterator = iterable.iterator(); while (newIterator.hasNext()) { list.add(newIterator.next()); } @@ -191,24 +191,24 @@ public class LazyFluentIterable implements FluentIterable { } /** - * Transforms this FluentIterable into a new one containing objects of the type NEW_TYPE. + * Transforms this FluentIterable into a new one containing objects of the type T. * - * @param function a function that transforms an instance of TYPE into an instance of NEW_TYPE - * @param the target type of the transformation + * @param function a function that transforms an instance of E into an instance of T + * @param the target type of the transformation * @return a new FluentIterable of the new type */ @Override - public FluentIterable map(Function function) { - return new LazyFluentIterable() { + public FluentIterable map(Function function) { + return new LazyFluentIterable() { @Override - public Iterator iterator() { - return new DecoratingIterator(null) { - Iterator oldTypeIterator = iterable.iterator(); + public Iterator iterator() { + return new DecoratingIterator(null) { + Iterator oldTypeIterator = iterable.iterator(); @Override - public NEW_TYPE computeNext() { + public T computeNext() { if (oldTypeIterator.hasNext()) { - TYPE candidate = oldTypeIterator.next(); + E candidate = oldTypeIterator.next(); return function.apply(candidate); } else { return null; @@ -225,15 +225,15 @@ public class LazyFluentIterable implements FluentIterable { * @return a list with all remaining objects of this iteration */ @Override - public List asList() { + public List asList() { return FluentIterable.copyToList(iterable); } @Override - public Iterator iterator() { - return new DecoratingIterator(iterable.iterator()) { + public Iterator iterator() { + return new DecoratingIterator(iterable.iterator()) { @Override - public TYPE computeNext() { + public E computeNext() { return fromIterator.hasNext() ? fromIterator.next() : null; } }; @@ -242,7 +242,7 @@ public class LazyFluentIterable implements FluentIterable { /** * @return a FluentIterable from a given iterable. Calls the LazyFluentIterable constructor. */ - public static final FluentIterable from(Iterable iterable) { + public static final FluentIterable from(Iterable iterable) { return new LazyFluentIterable<>(iterable); } 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 29ef25b56..153f3faa5 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 @@ -37,18 +37,18 @@ import com.iluwatar.fluentinterface.fluentiterable.FluentIterable; * This is a simple implementation of the FluentIterable interface. It evaluates all chained * operations eagerly. This implementation would be costly to be utilized in real applications. * - * @param the type of the objects the iteration is about + * @param the type of the objects the iteration is about */ -public class SimpleFluentIterable implements FluentIterable { +public class SimpleFluentIterable implements FluentIterable { - private final Iterable iterable; + private final Iterable iterable; /** * This constructor creates a copy of a given iterable's contents. * * @param iterable the iterable this interface copies to work on. */ - protected SimpleFluentIterable(Iterable iterable) { + protected SimpleFluentIterable(Iterable iterable) { this.iterable = iterable; } @@ -61,10 +61,10 @@ public class SimpleFluentIterable implements FluentIterable { * @return the same FluentIterable with a filtered collection */ @Override - public final FluentIterable filter(Predicate predicate) { - Iterator iterator = iterator(); + public final FluentIterable filter(Predicate predicate) { + Iterator iterator = iterator(); while (iterator.hasNext()) { - TYPE nextElement = iterator.next(); + E nextElement = iterator.next(); if (!predicate.test(nextElement)) { iterator.remove(); } @@ -78,8 +78,8 @@ public class SimpleFluentIterable implements FluentIterable { * @return an option of the first object of the Iterable */ @Override - public final Optional first() { - Iterator resultIterator = first(1).iterator(); + public final Optional first() { + Iterator resultIterator = first(1).iterator(); return resultIterator.hasNext() ? Optional.of(resultIterator.next()) : Optional.empty(); } @@ -91,8 +91,8 @@ public class SimpleFluentIterable implements FluentIterable { * objects. */ @Override - public final FluentIterable first(int count) { - Iterator iterator = iterator(); + public final FluentIterable first(int count) { + Iterator iterator = iterator(); int currentCount = 0; while (iterator.hasNext()) { iterator.next(); @@ -110,8 +110,8 @@ public class SimpleFluentIterable implements FluentIterable { * @return an option of the last object of the Iterable */ @Override - public final Optional last() { - List list = last(1).asList(); + public final Optional last() { + List list = last(1).asList(); if (list.isEmpty()) { return Optional.empty(); } @@ -126,9 +126,9 @@ public class SimpleFluentIterable implements FluentIterable { * objects */ @Override - public final FluentIterable last(int count) { + public final FluentIterable last(int count) { int remainingElementsCount = getRemainingElementsCount(); - Iterator iterator = iterator(); + Iterator iterator = iterator(); int currentIndex = 0; while (iterator.hasNext()) { iterator.next(); @@ -142,16 +142,16 @@ public class SimpleFluentIterable implements FluentIterable { } /** - * Transforms this FluentIterable into a new one containing objects of the type NEW_TYPE. + * Transforms this FluentIterable into a new one containing objects of the type T. * - * @param function a function that transforms an instance of TYPE into an instance of NEW_TYPE - * @param the target type of the transformation + * @param function a function that transforms an instance of E into an instance of T + * @param the target type of the transformation * @return a new FluentIterable of the new type */ @Override - public final FluentIterable map(Function function) { - List temporaryList = new ArrayList<>(); - Iterator iterator = iterator(); + public final FluentIterable map(Function function) { + List temporaryList = new ArrayList<>(); + Iterator iterator = iterator(); while (iterator.hasNext()) { temporaryList.add(function.apply(iterator.next())); } @@ -164,35 +164,35 @@ public class SimpleFluentIterable implements FluentIterable { * @return a list with all remaining objects of this Iterable */ @Override - public List asList() { + public List asList() { return toList(iterable.iterator()); } /** * @return a FluentIterable from a given iterable. Calls the SimpleFluentIterable constructor. */ - public static final FluentIterable from(Iterable iterable) { + public static final FluentIterable from(Iterable iterable) { return new SimpleFluentIterable<>(iterable); } - public static final FluentIterable fromCopyOf(Iterable iterable) { - List copy = FluentIterable.copyToList(iterable); + public static final FluentIterable fromCopyOf(Iterable iterable) { + List copy = FluentIterable.copyToList(iterable); return new SimpleFluentIterable<>(copy); } @Override - public Iterator iterator() { + public Iterator iterator() { return iterable.iterator(); } @Override - public void forEach(Consumer action) { + public void forEach(Consumer action) { iterable.forEach(action); } @Override - public Spliterator spliterator() { + public Spliterator spliterator() { return iterable.spliterator(); } @@ -201,7 +201,7 @@ public class SimpleFluentIterable implements FluentIterable { */ public final int getRemainingElementsCount() { int counter = 0; - Iterator iterator = iterator(); + Iterator iterator = iterator(); while (iterator.hasNext()) { iterator.next(); counter++; @@ -214,8 +214,8 @@ public class SimpleFluentIterable implements FluentIterable { * * @return a new List with the remaining objects. */ - public static List toList(Iterator iterator) { - List copy = new ArrayList<>(); + public static List toList(Iterator iterator) { + List copy = new ArrayList<>(); while (iterator.hasNext()) { copy.add(iterator.next()); }