From 504ca816530efdff06bc202e0432ebd354aec304 Mon Sep 17 00:00:00 2001 From: EVE Date: Tue, 14 Mar 2017 12:07:14 -0400 Subject: Cleanup --- .../java/bjc/utils/funcdata/FunctionalList.java | 131 ++++++++------------- 1 file changed, 47 insertions(+), 84 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 f98f32c..1e52d05 100644 --- a/BJC-Utils2/src/main/java/bjc/utils/funcdata/FunctionalList.java +++ b/BJC-Utils2/src/main/java/bjc/utils/funcdata/FunctionalList.java @@ -1,5 +1,10 @@ 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; @@ -12,18 +17,13 @@ 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. * * Differs from a stream in every way except for the fact that they both provide * functional operations. - * + * * @author ben * * @param @@ -44,9 +44,9 @@ public class FunctionalList implements Cloneable, IList { /** * Create a new functional list containing the specified items. - * + * * Takes O(n) time, where n is the number of items specified - * + * * @param items * The items to put into this functional list. */ @@ -54,14 +54,14 @@ public class FunctionalList implements Cloneable, IList { public FunctionalList(E... items) { wrapped = new ArrayList<>(items.length); - for (E item : items) { + for(E item : items) { wrapped.add(item); } } /** * Create a new functional list with the specified size. - * + * * @param size * The size of the backing list . */ @@ -71,16 +71,14 @@ public class FunctionalList implements Cloneable, IList { /** * Create a new functional list as a wrapper of a existing list. - * + * * Takes O(1) time, since it doesn't copy the list. - * + * * @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"); - } + if(backing == null) throw new NullPointerException("Backing list must be non-null"); wrapped = backing; } @@ -92,15 +90,12 @@ public class FunctionalList implements Cloneable, IList { @Override public boolean allMatch(Predicate predicate) { - if (predicate == null) { - throw new NullPointerException("Predicate must be non-null"); - } + if(predicate == null) throw new NullPointerException("Predicate must be non-null"); - for (E item : wrapped) { - if (!predicate.test(item)) { - // We've found a non-matching item + for(E item : wrapped) { + if(!predicate.test(item)) // We've found a non-matching + // item return false; - } } // All of the items matched @@ -109,15 +104,11 @@ public class FunctionalList implements Cloneable, IList { @Override public boolean anyMatch(Predicate predicate) { - if (predicate == null) { - throw new NullPointerException("Predicate must be not null"); - } + if(predicate == null) throw new NullPointerException("Predicate must be not null"); - for (E item : wrapped) { - if (predicate.test(item)) { - // We've found a matching item + for(E item : wrapped) { + if(predicate.test(item)) // We've found a matching item return true; - } } // We didn't find a matching item @@ -126,16 +117,16 @@ public class FunctionalList implements Cloneable, IList { /** * Clone this list into a new one, and clone the backing list as well - * + * * Takes O(n) time, where n is the number of elements in the list - * + * * @return A list */ @Override public IList clone() { IList cloned = new FunctionalList<>(); - for (E element : wrapped) { + for(E element : wrapped) { cloned.add(element); } @@ -144,19 +135,16 @@ public class FunctionalList implements Cloneable, IList { @Override public IList combineWith(IList rightList, BiFunction itemCombiner) { - if (rightList == null) { + 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<>(); // Get the iterator for the other list Iterator rightIterator = rightList.toIterable().iterator(); - for (Iterator leftIterator = wrapped.iterator(); leftIterator.hasNext() - && rightIterator.hasNext();) { + for(Iterator leftIterator = wrapped.iterator(); leftIterator.hasNext() && rightIterator.hasNext();) { // Add the transformed items to the result list E leftVal = leftIterator.next(); T rightVal = rightIterator.next(); @@ -175,27 +163,21 @@ public class FunctionalList implements Cloneable, IList { @Override public E first() { - if (wrapped.size() < 1) { - throw new NoSuchElementException("Attempted to get first element of empty list"); - } + if(wrapped.size() < 1) throw new NoSuchElementException("Attempted to get first element of empty list"); return wrapped.get(0); } @Override public IList flatMap(Function> expander) { - if (expander == null) { - throw new NullPointerException("Expander must not be null"); - } + if(expander == null) throw new NullPointerException("Expander must not be null"); IList returned = new FunctionalList<>(this.wrapped.size()); forEach(element -> { 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); @@ -204,19 +186,16 @@ public class FunctionalList implements Cloneable, IList { return returned; } + @Override public void forEach(Consumer action) { - if (action == null) { - throw new NullPointerException("Action is null"); - } + 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"); - } + if(indexedAction == null) throw new NullPointerException("Action must not be null"); // This is held b/c ref'd variables must be final/effectively // final @@ -238,7 +217,7 @@ public class FunctionalList implements Cloneable, IList { /** * Get the internal backing list. - * + * * @return The backing list this list is based off of. */ public List getInternal() { @@ -247,14 +226,12 @@ public class FunctionalList implements Cloneable, IList { @Override public IList getMatching(Predicate predicate) { - if (predicate == null) { - throw new NullPointerException("Predicate must not be null"); - } + if(predicate == null) throw new NullPointerException("Predicate must not be null"); IList returned = new FunctionalList<>(); wrapped.forEach((element) -> { - if (predicate.test(element)) { + if(predicate.test(element)) { // The item matches, so add it to the returned // list returned.add(element); @@ -283,9 +260,7 @@ public class FunctionalList implements Cloneable, IList { @Override public IList map(Function elementTransformer) { - if (elementTransformer == null) { - throw new NullPointerException("Transformer must be not null"); - } + if(elementTransformer == null) throw new NullPointerException("Transformer must be not null"); IList returned = new FunctionalList<>(this.wrapped.size()); @@ -304,10 +279,9 @@ public class FunctionalList implements Cloneable, IList { @Override public IList> partition(int numberPerPartition) { - if (numberPerPartition < 1 || numberPerPartition > wrapped.size()) { + if(numberPerPartition < 1 || numberPerPartition > wrapped.size()) throw new IllegalArgumentException("" + numberPerPartition + " is an invalid partition size. Must be between 1 and " + wrapped.size()); - } IList> returned = new FunctionalList<>(); @@ -315,7 +289,7 @@ public class FunctionalList implements Cloneable, IList { IHolder> currentPartition = new Identity<>(new FunctionalList<>()); this.forEach((element) -> { - if (isPartitionFull(numberPerPartition, currentPartition)) { + if(isPartitionFull(numberPerPartition, currentPartition)) { // Add the partition to the list returned.add(currentPartition.unwrap((partition) -> partition)); @@ -337,9 +311,7 @@ public class FunctionalList implements Cloneable, IList { @Override public E randItem(Function rnd) { - if (rnd == null) { - throw new NullPointerException("Random source must not be null"); - } + if(rnd == null) throw new NullPointerException("Random source must not be null"); int randomIndex = rnd.apply(wrapped.size()); @@ -349,11 +321,9 @@ public class FunctionalList implements Cloneable, IList { @Override public F reduceAux(T initialValue, BiFunction stateAccumulator, Function resultTransformer) { - if (stateAccumulator == null) { + 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); @@ -369,9 +339,7 @@ public class FunctionalList implements Cloneable, IList { @Override public boolean removeIf(Predicate removePredicate) { - if (removePredicate == null) { - throw new NullPointerException("Predicate must be non-null"); - } + if(removePredicate == null) throw new NullPointerException("Predicate must be non-null"); return wrapped.removeIf(removePredicate); } @@ -391,10 +359,8 @@ public class FunctionalList implements Cloneable, IList { // Search our internal list int foundIndex = Collections.binarySearch(wrapped, searchKey, comparator); - if (foundIndex >= 0) { - // We found a matching element + if(foundIndex >= 0) // We found a matching element return wrapped.get(foundIndex); - } // We didn't find an element return null; @@ -425,22 +391,19 @@ public class FunctionalList implements Cloneable, IList { public String toString() { int lSize = getSize(); - if (lSize == 0) - return "()"; + if(lSize == 0) return "()"; StringBuilder sb = new StringBuilder("("); Iterator itr = toIterable().iterator(); E itm = itr.next(); int i = 0; - if (lSize == 1) { - return "(" + itm + ")"; - } + if(lSize == 1) return "(" + itm + ")"; - for (E item : toIterable()) { + for(E item : toIterable()) { sb.append(item.toString()); - if (i < lSize - 1) { + if(i < lSize - 1) { sb.append(", "); } -- cgit v1.2.3