From 889fac2bdf993dc86f64a8893c0260fdcf848acb Mon Sep 17 00:00:00 2001 From: bculkin2442 Date: Mon, 10 Apr 2017 16:40:33 -0400 Subject: Cleanup --- .../java/bjc/utils/funcdata/FunctionalList.java | 163 ++++++++++----------- 1 file changed, 74 insertions(+), 89 deletions(-) (limited to 'BJC-Utils2/src/main/java/bjc/utils/funcdata/FunctionalList.java') diff --git a/BJC-Utils2/src/main/java/bjc/utils/funcdata/FunctionalList.java b/BJC-Utils2/src/main/java/bjc/utils/funcdata/FunctionalList.java index 7627bdf..55ea7ff 100644 --- a/BJC-Utils2/src/main/java/bjc/utils/funcdata/FunctionalList.java +++ b/BJC-Utils2/src/main/java/bjc/utils/funcdata/FunctionalList.java @@ -1,10 +1,5 @@ package bjc.utils.funcdata; -import bjc.utils.data.IHolder; -import bjc.utils.data.IPair; -import bjc.utils.data.Identity; -import bjc.utils.data.Pair; - import java.util.ArrayList; import java.util.Collections; import java.util.Comparator; @@ -17,6 +12,11 @@ import java.util.function.Consumer; import java.util.function.Function; import java.util.function.Predicate; +import bjc.utils.data.IHolder; +import bjc.utils.data.IPair; +import bjc.utils.data.Identity; +import bjc.utils.data.Pair; + /** * A wrapper over another list that provides eager functional operations over * it. @@ -33,7 +33,7 @@ public class FunctionalList implements Cloneable, IList { /* * The list used as a backing store */ - private List wrapped; + private final List wrapped; /** * Create a new empty functional list. @@ -51,10 +51,10 @@ public class FunctionalList implements Cloneable, IList { * The items to put into this functional list. */ @SafeVarargs - public FunctionalList(E... items) { + public FunctionalList(final E... items) { wrapped = new ArrayList<>(items.length); - for (E item : items) { + for (final E item : items) { wrapped.add(item); } } @@ -65,7 +65,7 @@ public class FunctionalList implements Cloneable, IList { * @param size * The size of the backing list . */ - private FunctionalList(int size) { + private FunctionalList(final int size) { wrapped = new ArrayList<>(size); } @@ -77,24 +77,22 @@ public class FunctionalList implements Cloneable, IList { * @param backing * The list to use as a backing list. */ - public FunctionalList(List backing) { - if (backing == null) - throw new NullPointerException("Backing list must be non-null"); + public FunctionalList(final List backing) { + if (backing == null) throw new NullPointerException("Backing list must be non-null"); wrapped = backing; } @Override - public boolean add(E item) { + public boolean add(final E item) { return wrapped.add(item); } @Override - public boolean allMatch(Predicate predicate) { - if (predicate == null) - throw new NullPointerException("Predicate must be non-null"); + public boolean allMatch(final Predicate predicate) { + if (predicate == null) throw new NullPointerException("Predicate must be non-null"); - for (E item : wrapped) { + for (final E item : wrapped) { if (!predicate.test(item)) // We've found a non-matching item return false; @@ -105,11 +103,10 @@ public class FunctionalList implements Cloneable, IList { } @Override - public boolean anyMatch(Predicate predicate) { - if (predicate == null) - throw new NullPointerException("Predicate must be not null"); + public boolean anyMatch(final Predicate predicate) { + if (predicate == null) throw new NullPointerException("Predicate must be not null"); - for (E item : wrapped) { + for (final E item : wrapped) { if (predicate.test(item)) // We've found a matching item return true; @@ -128,9 +125,9 @@ public class FunctionalList implements Cloneable, IList { */ @Override public IList clone() { - IList cloned = new FunctionalList<>(); + final IList cloned = new FunctionalList<>(); - for (E element : wrapped) { + for (final E element : wrapped) { cloned.add(element); } @@ -138,22 +135,21 @@ public class FunctionalList implements Cloneable, IList { } @Override - public IList combineWith(IList rightList, BiFunction itemCombiner) { + public IList combineWith(final IList rightList, final BiFunction itemCombiner) { if (rightList == null) throw new NullPointerException("Target combine list must not be null"); - else if (itemCombiner == null) - throw new NullPointerException("Combiner must not be null"); + else if (itemCombiner == null) throw new NullPointerException("Combiner must not be null"); - IList returned = new FunctionalList<>(); + final IList returned = new FunctionalList<>(); // Get the iterator for the other list - Iterator rightIterator = rightList.toIterable().iterator(); + final Iterator rightIterator = rightList.toIterable().iterator(); - for (Iterator leftIterator = wrapped.iterator(); leftIterator.hasNext() + for (final Iterator leftIterator = wrapped.iterator(); leftIterator.hasNext() && rightIterator.hasNext();) { // Add the transformed items to the result list - E leftVal = leftIterator.next(); - T rightVal = rightIterator.next(); + final E leftVal = leftIterator.next(); + final T rightVal = rightIterator.next(); returned.add(itemCombiner.apply(leftVal, rightVal)); } @@ -162,7 +158,7 @@ public class FunctionalList implements Cloneable, IList { } @Override - public boolean contains(E item) { + public boolean contains(final E item) { // Check if any items in the list match the provided item return this.anyMatch(item::equals); } @@ -176,17 +172,15 @@ public class FunctionalList implements Cloneable, IList { } @Override - public IList flatMap(Function> expander) { - if (expander == null) - throw new NullPointerException("Expander must not be null"); + public IList flatMap(final Function> expander) { + if (expander == null) throw new NullPointerException("Expander must not be null"); - IList returned = new FunctionalList<>(this.wrapped.size()); + final IList returned = new FunctionalList<>(this.wrapped.size()); forEach(element -> { - IList expandedElement = expander.apply(element); + final IList expandedElement = expander.apply(element); - if (expandedElement == null) - throw new NullPointerException("Expander returned null list"); + if (expandedElement == null) throw new NullPointerException("Expander returned null list"); // Add each element to the returned list expandedElement.forEach(returned::add); @@ -196,21 +190,19 @@ public class FunctionalList implements Cloneable, IList { } @Override - public void forEach(Consumer action) { - if (action == null) - throw new NullPointerException("Action is null"); + public void forEach(final Consumer action) { + if (action == null) throw new NullPointerException("Action is null"); wrapped.forEach(action); } @Override - public void forEachIndexed(BiConsumer indexedAction) { - if (indexedAction == null) - throw new NullPointerException("Action must not be null"); + public void forEachIndexed(final BiConsumer indexedAction) { + if (indexedAction == null) throw new NullPointerException("Action must not be null"); // This is held b/c ref'd variables must be final/effectively // final - IHolder currentIndex = new Identity<>(0); + final IHolder currentIndex = new Identity<>(0); wrapped.forEach((element) -> { // Call the action with the index and the value @@ -222,7 +214,7 @@ public class FunctionalList implements Cloneable, IList { } @Override - public E getByIndex(int index) { + public E getByIndex(final int index) { return wrapped.get(index); } @@ -236,11 +228,10 @@ public class FunctionalList implements Cloneable, IList { } @Override - public IList getMatching(Predicate predicate) { - if (predicate == null) - throw new NullPointerException("Predicate must not be null"); + public IList getMatching(final Predicate predicate) { + if (predicate == null) throw new NullPointerException("Predicate must not be null"); - IList returned = new FunctionalList<>(); + final IList returned = new FunctionalList<>(); wrapped.forEach((element) -> { if (predicate.test(element)) { @@ -266,16 +257,15 @@ public class FunctionalList implements Cloneable, IList { /* * Check if a partition has room for another item */ - private Boolean isPartitionFull(int numberPerPartition, IHolder> currentPartition) { + private Boolean isPartitionFull(final int numberPerPartition, final IHolder> currentPartition) { return currentPartition.unwrap((partition) -> partition.getSize() >= numberPerPartition); } @Override - public IList map(Function elementTransformer) { - if (elementTransformer == null) - throw new NullPointerException("Transformer must be not null"); + public IList map(final Function elementTransformer) { + if (elementTransformer == null) throw new NullPointerException("Transformer must be not null"); - IList returned = new FunctionalList<>(this.wrapped.size()); + final IList returned = new FunctionalList<>(this.wrapped.size()); forEach(element -> { // Add the transformed item to the result @@ -286,23 +276,23 @@ public class FunctionalList implements Cloneable, IList { } @Override - public IList> pairWith(IList rightList) { + public IList> pairWith(final IList rightList) { return combineWith(rightList, Pair::new); } @Override - public IList> partition(int numberPerPartition) { + public IList> partition(final int numberPerPartition) { if (numberPerPartition < 1 || numberPerPartition > wrapped.size()) { - String fmt = "%s is an invalid partition size. Must be between 1 and %d"; - String msg = String.format(fmt, numberPerPartition, wrapped.size()); + final String fmt = "%s is an invalid partition size. Must be between 1 and %d"; + final String msg = String.format(fmt, numberPerPartition, wrapped.size()); throw new IllegalArgumentException(msg); } - IList> returned = new FunctionalList<>(); + final IList> returned = new FunctionalList<>(); // The current partition being filled - IHolder> currentPartition = new Identity<>(new FunctionalList<>()); + final IHolder> currentPartition = new Identity<>(new FunctionalList<>()); this.forEach(element -> { if (isPartitionFull(numberPerPartition, currentPartition)) { @@ -321,30 +311,28 @@ public class FunctionalList implements Cloneable, IList { } @Override - public void prepend(E item) { + public void prepend(final E item) { wrapped.add(0, item); } @Override - public E randItem(Function rnd) { - if (rnd == null) - throw new NullPointerException("Random source must not be null"); + public E randItem(final Function rnd) { + if (rnd == null) throw new NullPointerException("Random source must not be null"); - int randomIndex = rnd.apply(wrapped.size()); + final int randomIndex = rnd.apply(wrapped.size()); return wrapped.get(randomIndex); } @Override - public F reduceAux(T initialValue, BiFunction stateAccumulator, - Function resultTransformer) { + public F reduceAux(final T initialValue, final BiFunction stateAccumulator, + final Function resultTransformer) { if (stateAccumulator == null) throw new NullPointerException("Accumulator must not be null"); - else if (resultTransformer == null) - throw new NullPointerException("Transformer must not be null"); + else if (resultTransformer == null) throw new NullPointerException("Transformer must not be null"); // The current collapsed list - IHolder currentState = new Identity<>(initialValue); + final IHolder currentState = new Identity<>(initialValue); wrapped.forEach(element -> { // Accumulate a new value into the state @@ -356,15 +344,14 @@ public class FunctionalList implements Cloneable, IList { } @Override - public boolean removeIf(Predicate removePredicate) { - if (removePredicate == null) - throw new NullPointerException("Predicate must be non-null"); + public boolean removeIf(final Predicate removePredicate) { + if (removePredicate == null) throw new NullPointerException("Predicate must be non-null"); return wrapped.removeIf(removePredicate); } @Override - public void removeMatching(E desiredElement) { + public void removeMatching(final E desiredElement) { removeIf(element -> element.equals(desiredElement)); } @@ -374,9 +361,9 @@ public class FunctionalList implements Cloneable, IList { } @Override - public E search(E searchKey, Comparator comparator) { + public E search(final E searchKey, final Comparator comparator) { // Search our internal list - int foundIndex = Collections.binarySearch(wrapped, searchKey, comparator); + final int foundIndex = Collections.binarySearch(wrapped, searchKey, comparator); if (foundIndex >= 0) // We found a matching element return wrapped.get(foundIndex); @@ -386,7 +373,7 @@ public class FunctionalList implements Cloneable, IList { } @Override - public void sort(Comparator comparator) { + public void sort(final Comparator comparator) { // sb.deleteCharAt(sb.length() - 2); Collections.sort(wrapped, comparator); } @@ -397,7 +384,7 @@ public class FunctionalList implements Cloneable, IList { } @Override - public E[] toArray(E[] arrType) { + public E[] toArray(final E[] arrType) { return wrapped.toArray(arrType); } @@ -408,20 +395,18 @@ public class FunctionalList implements Cloneable, IList { @Override public String toString() { - int lSize = getSize(); + final int lSize = getSize(); - if (lSize == 0) - return "()"; + if (lSize == 0) return "()"; - StringBuilder sb = new StringBuilder("("); - Iterator itr = toIterable().iterator(); - E itm = itr.next(); + final StringBuilder sb = new StringBuilder("("); + final Iterator itr = toIterable().iterator(); + final E itm = itr.next(); int i = 0; - if (lSize == 1) - return "(" + itm + ")"; + if (lSize == 1) return "(" + itm + ")"; - for (E item : toIterable()) { + for (final E item : toIterable()) { sb.append(item.toString()); if (i < lSize - 1) { -- cgit v1.2.3