From 63d88eb8db1f7a6d5924ec2a8b7f462373d5ac9a Mon Sep 17 00:00:00 2001 From: bjculkin Date: Fri, 7 Apr 2017 10:51:31 -0400 Subject: Cleanup --- .../java/bjc/utils/funcdata/FunctionalList.java | 102 +++++++++++++-------- 1 file changed, 62 insertions(+), 40 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 1e52d05..7627bdf 100644 --- a/BJC-Utils2/src/main/java/bjc/utils/funcdata/FunctionalList.java +++ b/BJC-Utils2/src/main/java/bjc/utils/funcdata/FunctionalList.java @@ -54,7 +54,7 @@ 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); } } @@ -78,7 +78,8 @@ public class FunctionalList implements Cloneable, IList { * 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; } @@ -90,11 +91,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; } @@ -104,10 +106,12 @@ 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; } @@ -126,7 +130,7 @@ public class FunctionalList implements Cloneable, IList { public IList clone() { IList cloned = new FunctionalList<>(); - for(E element : wrapped) { + for (E element : wrapped) { cloned.add(element); } @@ -135,16 +139,18 @@ 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(); @@ -163,21 +169,24 @@ 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); @@ -188,14 +197,16 @@ public class FunctionalList implements Cloneable, IList { @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 @@ -226,12 +237,13 @@ 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); @@ -260,7 +272,8 @@ 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()); @@ -279,25 +292,28 @@ public class FunctionalList implements Cloneable, IList { @Override public IList> partition(int numberPerPartition) { - if(numberPerPartition < 1 || numberPerPartition > wrapped.size()) - throw new IllegalArgumentException("" + numberPerPartition - + " is an invalid partition size. Must be between 1 and " + wrapped.size()); + 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()); + + throw new IllegalArgumentException(msg); + } IList> returned = new FunctionalList<>(); // The current partition being filled IHolder> currentPartition = new Identity<>(new FunctionalList<>()); - this.forEach((element) -> { - if(isPartitionFull(numberPerPartition, currentPartition)) { + this.forEach(element -> { + if (isPartitionFull(numberPerPartition, currentPartition)) { // Add the partition to the list - returned.add(currentPartition.unwrap((partition) -> partition)); + returned.add(currentPartition.unwrap(partition -> partition)); // Start a new partition - currentPartition.transform((partition) -> new FunctionalList<>()); + currentPartition.transform(partition -> new FunctionalList<>()); } else { // Add the element to the current partition - currentPartition.unwrap((partition) -> partition.add(element)); + currentPartition.unwrap(partition -> partition.add(element)); } }); @@ -311,7 +327,8 @@ 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()); @@ -321,16 +338,17 @@ 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); wrapped.forEach(element -> { // Accumulate a new value into the state - currentState.transform((state) -> stateAccumulator.apply(element, state)); + currentState.transform(state -> stateAccumulator.apply(element, state)); }); // Convert the state to its final value @@ -339,14 +357,15 @@ 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); } @Override public void removeMatching(E desiredElement) { - removeIf((element) -> element.equals(desiredElement)); + removeIf(element -> element.equals(desiredElement)); } @Override @@ -359,7 +378,7 @@ 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 @@ -391,19 +410,21 @@ 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(", "); } @@ -411,6 +432,7 @@ public class FunctionalList implements Cloneable, IList { } sb.append(")"); + return sb.toString(); } } -- cgit v1.2.3