diff options
| author | EVE <EVE@EVE-PC> | 2017-03-14 12:07:14 -0400 |
|---|---|---|
| committer | EVE <EVE@EVE-PC> | 2017-03-14 12:07:14 -0400 |
| commit | 504ca816530efdff06bc202e0432ebd354aec304 (patch) | |
| tree | 4836932fb81d1d625470502c78c94d202c9a7420 /BJC-Utils2/src/main/java/bjc/utils/funcdata/FunctionalList.java | |
| parent | 5c1163df17c46f7d3e15b6c7949c38843ec56146 (diff) | |
Cleanup
Diffstat (limited to 'BJC-Utils2/src/main/java/bjc/utils/funcdata/FunctionalList.java')
| -rw-r--r-- | BJC-Utils2/src/main/java/bjc/utils/funcdata/FunctionalList.java | 131 |
1 files changed, 47 insertions, 84 deletions
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 <E> @@ -44,9 +44,9 @@ public class FunctionalList<E> implements Cloneable, IList<E> { /** * 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<E> implements Cloneable, IList<E> { 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<E> implements Cloneable, IList<E> { /** * 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<E> 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<E> implements Cloneable, IList<E> { @Override public boolean allMatch(Predicate<E> 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<E> implements Cloneable, IList<E> { @Override public boolean anyMatch(Predicate<E> 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<E> implements Cloneable, IList<E> { /** * 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<E> clone() { IList<E> cloned = new FunctionalList<>(); - for (E element : wrapped) { + for(E element : wrapped) { cloned.add(element); } @@ -144,19 +135,16 @@ public class FunctionalList<E> implements Cloneable, IList<E> { @Override public <T, F> IList<F> combineWith(IList<T> rightList, BiFunction<E, T, F> 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<F> returned = new FunctionalList<>(); // Get the iterator for the other list Iterator<T> rightIterator = rightList.toIterable().iterator(); - for (Iterator<E> leftIterator = wrapped.iterator(); leftIterator.hasNext() - && rightIterator.hasNext();) { + for(Iterator<E> 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<E> implements Cloneable, IList<E> { @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 <T> IList<T> flatMap(Function<E, IList<T>> expander) { - if (expander == null) { - throw new NullPointerException("Expander must not be null"); - } + if(expander == null) throw new NullPointerException("Expander must not be null"); IList<T> returned = new FunctionalList<>(this.wrapped.size()); forEach(element -> { IList<T> 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<E> implements Cloneable, IList<E> { return returned; } + @Override public void forEach(Consumer<? super E> 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<Integer, E> 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<E> implements Cloneable, IList<E> { /** * Get the internal backing list. - * + * * @return The backing list this list is based off of. */ public List<E> getInternal() { @@ -247,14 +226,12 @@ public class FunctionalList<E> implements Cloneable, IList<E> { @Override public IList<E> getMatching(Predicate<E> predicate) { - if (predicate == null) { - throw new NullPointerException("Predicate must not be null"); - } + if(predicate == null) throw new NullPointerException("Predicate must not be null"); IList<E> 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<E> implements Cloneable, IList<E> { @Override public <T> IList<T> map(Function<E, T> elementTransformer) { - if (elementTransformer == null) { - throw new NullPointerException("Transformer must be not null"); - } + if(elementTransformer == null) throw new NullPointerException("Transformer must be not null"); IList<T> returned = new FunctionalList<>(this.wrapped.size()); @@ -304,10 +279,9 @@ public class FunctionalList<E> implements Cloneable, IList<E> { @Override public IList<IList<E>> 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<IList<E>> returned = new FunctionalList<>(); @@ -315,7 +289,7 @@ public class FunctionalList<E> implements Cloneable, IList<E> { IHolder<IList<E>> 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<E> implements Cloneable, IList<E> { @Override public E randItem(Function<Integer, Integer> 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<E> implements Cloneable, IList<E> { @Override public <T, F> F reduceAux(T initialValue, BiFunction<E, T, T> stateAccumulator, Function<T, F> 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<T> currentState = new Identity<>(initialValue); @@ -369,9 +339,7 @@ public class FunctionalList<E> implements Cloneable, IList<E> { @Override public boolean removeIf(Predicate<E> 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<E> implements Cloneable, IList<E> { // 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<E> implements Cloneable, IList<E> { public String toString() { int lSize = getSize(); - if (lSize == 0) - return "()"; + if(lSize == 0) return "()"; StringBuilder sb = new StringBuilder("("); Iterator<E> 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(", "); } |
