diff options
| author | bculkin2442 <bjculkin@mix.wvu.edu> | 2017-04-10 16:40:33 -0400 |
|---|---|---|
| committer | bculkin2442 <bjculkin@mix.wvu.edu> | 2017-04-10 16:40:33 -0400 |
| commit | 889fac2bdf993dc86f64a8893c0260fdcf848acb (patch) | |
| tree | 99ed08552efa86fdc5fdf4ddb8720d10e599fafe /BJC-Utils2/src/main/java/bjc/utils/funcdata | |
| parent | 1656b02144446aeedebb3d1179e07ed99c01861c (diff) | |
Cleanup
Diffstat (limited to 'BJC-Utils2/src/main/java/bjc/utils/funcdata')
12 files changed, 303 insertions, 375 deletions
diff --git a/BJC-Utils2/src/main/java/bjc/utils/funcdata/ExtendedMap.java b/BJC-Utils2/src/main/java/bjc/utils/funcdata/ExtendedMap.java index caa487c..909c5e9 100644 --- a/BJC-Utils2/src/main/java/bjc/utils/funcdata/ExtendedMap.java +++ b/BJC-Utils2/src/main/java/bjc/utils/funcdata/ExtendedMap.java @@ -1,17 +1,17 @@ package bjc.utils.funcdata; -import bjc.utils.funcutils.ListUtils; - import java.util.function.BiConsumer; import java.util.function.Consumer; import java.util.function.Function; +import bjc.utils.funcutils.ListUtils; + class ExtendedMap<KeyType, ValueType> implements IMap<KeyType, ValueType> { - private IMap<KeyType, ValueType> delegate; + private final IMap<KeyType, ValueType> delegate; - private IMap<KeyType, ValueType> store; + private final IMap<KeyType, ValueType> store; - public ExtendedMap(IMap<KeyType, ValueType> delegate, IMap<KeyType, ValueType> store) { + public ExtendedMap(final IMap<KeyType, ValueType> delegate, final IMap<KeyType, ValueType> store) { this.delegate = delegate; this.store = store; } @@ -22,9 +22,8 @@ class ExtendedMap<KeyType, ValueType> implements IMap<KeyType, ValueType> { } @Override - public boolean containsKey(KeyType key) { - if (store.containsKey(key)) - return true; + public boolean containsKey(final KeyType key) { + if (store.containsKey(key)) return true; return delegate.containsKey(key); } @@ -35,30 +34,29 @@ class ExtendedMap<KeyType, ValueType> implements IMap<KeyType, ValueType> { } @Override - public void forEach(BiConsumer<KeyType, ValueType> action) { + public void forEach(final BiConsumer<KeyType, ValueType> action) { store.forEach(action); delegate.forEach(action); } @Override - public void forEachKey(Consumer<KeyType> action) { + public void forEachKey(final Consumer<KeyType> action) { store.forEachKey(action); delegate.forEachKey(action); } @Override - public void forEachValue(Consumer<ValueType> action) { + public void forEachValue(final Consumer<ValueType> action) { store.forEachValue(action); delegate.forEachValue(action); } @Override - public ValueType get(KeyType key) { - if (store.containsKey(key)) - return store.get(key); + public ValueType get(final KeyType key) { + if (store.containsKey(key)) return store.get(key); return delegate.get(key); } @@ -74,19 +72,18 @@ class ExtendedMap<KeyType, ValueType> implements IMap<KeyType, ValueType> { } @Override - public <MappedValue> IMap<KeyType, MappedValue> transform(Function<ValueType, MappedValue> transformer) { + public <MappedValue> IMap<KeyType, MappedValue> transform(final Function<ValueType, MappedValue> transformer) { return new TransformedValueMap<>(this, transformer); } @Override - public ValueType put(KeyType key, ValueType val) { + public ValueType put(final KeyType key, final ValueType val) { return store.put(key, val); } @Override - public ValueType remove(KeyType key) { - if (!store.containsKey(key)) - return delegate.remove(key); + public ValueType remove(final KeyType key) { + if (!store.containsKey(key)) return delegate.remove(key); return store.remove(key); } @@ -100,32 +97,25 @@ class ExtendedMap<KeyType, ValueType> implements IMap<KeyType, ValueType> { public int hashCode() { final int prime = 31; int result = 1; - result = prime * result + ((delegate == null) ? 0 : delegate.hashCode()); - result = prime * result + ((store == null) ? 0 : store.hashCode()); + result = prime * result + (delegate == null ? 0 : delegate.hashCode()); + result = prime * result + (store == null ? 0 : store.hashCode()); return result; } @Override - public boolean equals(Object obj) { - if (this == obj) - return true; - if (obj == null) - return false; - if (!(obj instanceof ExtendedMap)) - return false; + public boolean equals(final Object obj) { + if (this == obj) return true; + if (obj == null) return false; + if (!(obj instanceof ExtendedMap)) return false; - ExtendedMap<?, ?> other = (ExtendedMap<?, ?>) obj; + final ExtendedMap<?, ?> other = (ExtendedMap<?, ?>) obj; if (delegate == null) { - if (other.delegate != null) - return false; - } else if (!delegate.equals(other.delegate)) - return false; + if (other.delegate != null) return false; + } else if (!delegate.equals(other.delegate)) return false; if (store == null) { - if (other.store != null) - return false; - } else if (!store.equals(other.store)) - return false; + if (other.store != null) return false; + } else if (!store.equals(other.store)) return false; return true; } 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<E> implements Cloneable, IList<E> { /* * The list used as a backing store */ - private List<E> wrapped; + private final List<E> wrapped; /** * Create a new empty functional list. @@ -51,10 +51,10 @@ public class FunctionalList<E> implements Cloneable, IList<E> { * 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<E> implements Cloneable, IList<E> { * @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<E> implements Cloneable, IList<E> { * @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"); + public FunctionalList(final List<E> 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<E> predicate) { - if (predicate == null) - throw new NullPointerException("Predicate must be non-null"); + public boolean allMatch(final Predicate<E> 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<E> implements Cloneable, IList<E> { } @Override - public boolean anyMatch(Predicate<E> predicate) { - if (predicate == null) - throw new NullPointerException("Predicate must be not null"); + public boolean anyMatch(final Predicate<E> 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<E> implements Cloneable, IList<E> { */ @Override public IList<E> clone() { - IList<E> cloned = new FunctionalList<>(); + final IList<E> cloned = new FunctionalList<>(); - for (E element : wrapped) { + for (final E element : wrapped) { cloned.add(element); } @@ -138,22 +135,21 @@ public class FunctionalList<E> implements Cloneable, IList<E> { } @Override - public <T, F> IList<F> combineWith(IList<T> rightList, BiFunction<E, T, F> itemCombiner) { + public <T, F> IList<F> combineWith(final IList<T> rightList, final BiFunction<E, T, F> 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<F> returned = new FunctionalList<>(); + final IList<F> returned = new FunctionalList<>(); // Get the iterator for the other list - Iterator<T> rightIterator = rightList.toIterable().iterator(); + final Iterator<T> rightIterator = rightList.toIterable().iterator(); - for (Iterator<E> leftIterator = wrapped.iterator(); leftIterator.hasNext() + for (final 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(); + final E leftVal = leftIterator.next(); + final T rightVal = rightIterator.next(); returned.add(itemCombiner.apply(leftVal, rightVal)); } @@ -162,7 +158,7 @@ public class FunctionalList<E> implements Cloneable, IList<E> { } @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<E> implements Cloneable, IList<E> { } @Override - public <T> IList<T> flatMap(Function<E, IList<T>> expander) { - if (expander == null) - throw new NullPointerException("Expander must not be null"); + public <T> IList<T> flatMap(final Function<E, IList<T>> expander) { + if (expander == null) throw new NullPointerException("Expander must not be null"); - IList<T> returned = new FunctionalList<>(this.wrapped.size()); + final IList<T> returned = new FunctionalList<>(this.wrapped.size()); forEach(element -> { - IList<T> expandedElement = expander.apply(element); + final 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); @@ -196,21 +190,19 @@ public class FunctionalList<E> implements Cloneable, IList<E> { } @Override - public void forEach(Consumer<? super E> action) { - if (action == null) - throw new NullPointerException("Action is null"); + public void forEach(final Consumer<? super E> action) { + 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"); + public void forEachIndexed(final BiConsumer<Integer, E> 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<Integer> currentIndex = new Identity<>(0); + final IHolder<Integer> currentIndex = new Identity<>(0); wrapped.forEach((element) -> { // Call the action with the index and the value @@ -222,7 +214,7 @@ public class FunctionalList<E> implements Cloneable, IList<E> { } @Override - public E getByIndex(int index) { + public E getByIndex(final int index) { return wrapped.get(index); } @@ -236,11 +228,10 @@ 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"); + public IList<E> getMatching(final Predicate<E> predicate) { + if (predicate == null) throw new NullPointerException("Predicate must not be null"); - IList<E> returned = new FunctionalList<>(); + final IList<E> returned = new FunctionalList<>(); wrapped.forEach((element) -> { if (predicate.test(element)) { @@ -266,16 +257,15 @@ public class FunctionalList<E> implements Cloneable, IList<E> { /* * Check if a partition has room for another item */ - private Boolean isPartitionFull(int numberPerPartition, IHolder<IList<E>> currentPartition) { + private Boolean isPartitionFull(final int numberPerPartition, final IHolder<IList<E>> currentPartition) { return currentPartition.unwrap((partition) -> partition.getSize() >= numberPerPartition); } @Override - public <T> IList<T> map(Function<E, T> elementTransformer) { - if (elementTransformer == null) - throw new NullPointerException("Transformer must be not null"); + public <T> IList<T> map(final Function<E, T> elementTransformer) { + if (elementTransformer == null) throw new NullPointerException("Transformer must be not null"); - IList<T> returned = new FunctionalList<>(this.wrapped.size()); + final IList<T> returned = new FunctionalList<>(this.wrapped.size()); forEach(element -> { // Add the transformed item to the result @@ -286,23 +276,23 @@ public class FunctionalList<E> implements Cloneable, IList<E> { } @Override - public <T> IList<IPair<E, T>> pairWith(IList<T> rightList) { + public <T> IList<IPair<E, T>> pairWith(final IList<T> rightList) { return combineWith(rightList, Pair<E, T>::new); } @Override - public IList<IList<E>> partition(int numberPerPartition) { + public IList<IList<E>> 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<IList<E>> returned = new FunctionalList<>(); + final IList<IList<E>> returned = new FunctionalList<>(); // The current partition being filled - IHolder<IList<E>> currentPartition = new Identity<>(new FunctionalList<>()); + final IHolder<IList<E>> currentPartition = new Identity<>(new FunctionalList<>()); this.forEach(element -> { if (isPartitionFull(numberPerPartition, currentPartition)) { @@ -321,30 +311,28 @@ public class FunctionalList<E> implements Cloneable, IList<E> { } @Override - public void prepend(E item) { + public void prepend(final E item) { wrapped.add(0, item); } @Override - public E randItem(Function<Integer, Integer> rnd) { - if (rnd == null) - throw new NullPointerException("Random source must not be null"); + public E randItem(final Function<Integer, Integer> 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 <T, F> F reduceAux(T initialValue, BiFunction<E, T, T> stateAccumulator, - Function<T, F> resultTransformer) { + public <T, F> F reduceAux(final T initialValue, final BiFunction<E, T, T> stateAccumulator, + final Function<T, F> 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<T> currentState = new Identity<>(initialValue); + final IHolder<T> currentState = new Identity<>(initialValue); wrapped.forEach(element -> { // Accumulate a new value into the state @@ -356,15 +344,14 @@ 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"); + public boolean removeIf(final Predicate<E> 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<E> implements Cloneable, IList<E> { } @Override - public E search(E searchKey, Comparator<E> comparator) { + public E search(final E searchKey, final Comparator<E> 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<E> implements Cloneable, IList<E> { } @Override - public void sort(Comparator<E> comparator) { + public void sort(final Comparator<E> comparator) { // sb.deleteCharAt(sb.length() - 2); Collections.sort(wrapped, comparator); } @@ -397,7 +384,7 @@ public class FunctionalList<E> implements Cloneable, IList<E> { } @Override - public E[] toArray(E[] arrType) { + public E[] toArray(final E[] arrType) { return wrapped.toArray(arrType); } @@ -408,20 +395,18 @@ public class FunctionalList<E> implements Cloneable, IList<E> { @Override public String toString() { - int lSize = getSize(); + final int lSize = getSize(); - if (lSize == 0) - return "()"; + if (lSize == 0) return "()"; - StringBuilder sb = new StringBuilder("("); - Iterator<E> itr = toIterable().iterator(); - E itm = itr.next(); + final StringBuilder sb = new StringBuilder("("); + final Iterator<E> 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) { diff --git a/BJC-Utils2/src/main/java/bjc/utils/funcdata/FunctionalMap.java b/BJC-Utils2/src/main/java/bjc/utils/funcdata/FunctionalMap.java index 62c39af..c4f0ff1 100644 --- a/BJC-Utils2/src/main/java/bjc/utils/funcdata/FunctionalMap.java +++ b/BJC-Utils2/src/main/java/bjc/utils/funcdata/FunctionalMap.java @@ -1,13 +1,13 @@ package bjc.utils.funcdata; -import bjc.utils.data.IPair; - import java.util.HashMap; import java.util.Map; import java.util.function.BiConsumer; import java.util.function.Consumer; import java.util.function.Function; +import bjc.utils.data.IPair; + /** * Basic implementation of {@link IMap} * @@ -35,10 +35,10 @@ public class FunctionalMap<KeyType, ValueType> implements IMap<KeyType, ValueTyp * The entries to put into the map */ @SafeVarargs - public FunctionalMap(IPair<KeyType, ValueType>... entries) { + public FunctionalMap(final IPair<KeyType, ValueType>... entries) { this(); - for (IPair<KeyType, ValueType> entry : entries) { + for (final IPair<KeyType, ValueType> entry : entries) { entry.doWith((key, val) -> { wrappedMap.put(key, val); }); @@ -51,9 +51,8 @@ public class FunctionalMap<KeyType, ValueType> implements IMap<KeyType, ValueTyp * @param wrap * The map to wrap */ - public FunctionalMap(Map<KeyType, ValueType> wrap) { - if (wrap == null) - throw new NullPointerException("Map to wrap must not be null"); + public FunctionalMap(final Map<KeyType, ValueType> wrap) { + if (wrap == null) throw new NullPointerException("Map to wrap must not be null"); wrappedMap = wrap; } @@ -64,7 +63,7 @@ public class FunctionalMap<KeyType, ValueType> implements IMap<KeyType, ValueTyp } @Override - public boolean containsKey(KeyType key) { + public boolean containsKey(final KeyType key) { return wrappedMap.containsKey(key); } @@ -74,27 +73,26 @@ public class FunctionalMap<KeyType, ValueType> implements IMap<KeyType, ValueTyp } @Override - public void forEach(BiConsumer<KeyType, ValueType> action) { + public void forEach(final BiConsumer<KeyType, ValueType> action) { wrappedMap.forEach(action); } @Override - public void forEachKey(Consumer<KeyType> action) { + public void forEachKey(final Consumer<KeyType> action) { wrappedMap.keySet().forEach(action); } @Override - public void forEachValue(Consumer<ValueType> action) { + public void forEachValue(final Consumer<ValueType> action) { wrappedMap.values().forEach(action); } @Override - public ValueType get(KeyType key) { - if (key == null) - throw new NullPointerException("Key must not be null"); + public ValueType get(final KeyType key) { + if (key == null) throw new NullPointerException("Key must not be null"); if (!wrappedMap.containsKey(key)) { - String msg = String.format("Key %s is not present in the map", key); + final String msg = String.format("Key %s is not present in the map", key); throw new IllegalArgumentException(msg); } @@ -109,7 +107,7 @@ public class FunctionalMap<KeyType, ValueType> implements IMap<KeyType, ValueTyp @Override public IList<KeyType> keyList() { - FunctionalList<KeyType> keys = new FunctionalList<>(); + final FunctionalList<KeyType> keys = new FunctionalList<>(); wrappedMap.keySet().forEach(key -> { keys.add(key); @@ -119,23 +117,21 @@ public class FunctionalMap<KeyType, ValueType> implements IMap<KeyType, ValueTyp } @Override - public <MappedValue> IMap<KeyType, MappedValue> transform(Function<ValueType, MappedValue> transformer) { - if (transformer == null) - throw new NullPointerException("Transformer must not be null"); + public <MappedValue> IMap<KeyType, MappedValue> transform(final Function<ValueType, MappedValue> transformer) { + if (transformer == null) throw new NullPointerException("Transformer must not be null"); return new TransformedValueMap<>(this, transformer); } @Override - public ValueType put(KeyType key, ValueType val) { - if (key == null) - throw new NullPointerException("Key must not be null"); + public ValueType put(final KeyType key, final ValueType val) { + if (key == null) throw new NullPointerException("Key must not be null"); return wrappedMap.put(key, val); } @Override - public ValueType remove(KeyType key) { + public ValueType remove(final KeyType key) { return wrappedMap.remove(key); } @@ -146,7 +142,7 @@ public class FunctionalMap<KeyType, ValueType> implements IMap<KeyType, ValueTyp @Override public IList<ValueType> valueList() { - FunctionalList<ValueType> values = new FunctionalList<>(); + final FunctionalList<ValueType> values = new FunctionalList<>(); wrappedMap.values().forEach(value -> { values.add(value); @@ -159,26 +155,21 @@ public class FunctionalMap<KeyType, ValueType> implements IMap<KeyType, ValueTyp public int hashCode() { final int prime = 31; int result = 1; - result = prime * result + ((wrappedMap == null) ? 0 : wrappedMap.hashCode()); + result = prime * result + (wrappedMap == null ? 0 : wrappedMap.hashCode()); return result; } @Override - public boolean equals(Object obj) { - if (this == obj) - return true; - if (obj == null) - return false; - if (!(obj instanceof FunctionalMap)) - return false; + public boolean equals(final Object obj) { + if (this == obj) return true; + if (obj == null) return false; + if (!(obj instanceof FunctionalMap)) return false; - FunctionalMap<?, ?> other = (FunctionalMap<?, ?>) obj; + final FunctionalMap<?, ?> other = (FunctionalMap<?, ?>) obj; if (wrappedMap == null) { - if (other.wrappedMap != null) - return false; - } else if (!wrappedMap.equals(other.wrappedMap)) - return false; + if (other.wrappedMap != null) return false; + } else if (!wrappedMap.equals(other.wrappedMap)) return false; return true; } } diff --git a/BJC-Utils2/src/main/java/bjc/utils/funcdata/FunctionalStringTokenizer.java b/BJC-Utils2/src/main/java/bjc/utils/funcdata/FunctionalStringTokenizer.java index 4723bcd..e068b46 100644 --- a/BJC-Utils2/src/main/java/bjc/utils/funcdata/FunctionalStringTokenizer.java +++ b/BJC-Utils2/src/main/java/bjc/utils/funcdata/FunctionalStringTokenizer.java @@ -18,9 +18,8 @@ public class FunctionalStringTokenizer { * The string to create a tokenizer from. * @return A new tokenizer that splits the provided string on spaces. */ - public static FunctionalStringTokenizer fromString(String strang) { - if (strang == null) - throw new NullPointerException("String to tokenize must be non-null"); + public static FunctionalStringTokenizer fromString(final String strang) { + if (strang == null) throw new NullPointerException("String to tokenize must be non-null"); return new FunctionalStringTokenizer(new StringTokenizer(strang, " ")); } @@ -28,7 +27,7 @@ public class FunctionalStringTokenizer { /* * The string tokenizer being driven */ - private StringTokenizer input; + private final StringTokenizer input; /** * Create a functional string tokenizer from a given string @@ -36,9 +35,8 @@ public class FunctionalStringTokenizer { * @param inp * The string to tokenize */ - public FunctionalStringTokenizer(String inp) { - if (inp == null) - throw new NullPointerException("String to tokenize must be non-null"); + public FunctionalStringTokenizer(final String inp) { + if (inp == null) throw new NullPointerException("String to tokenize must be non-null"); this.input = new StringTokenizer(inp); } @@ -52,11 +50,10 @@ public class FunctionalStringTokenizer { * @param seperators * The set of separating tokens to use for splitting */ - public FunctionalStringTokenizer(String input, String seperators) { + public FunctionalStringTokenizer(final String input, final String seperators) { if (input == null) throw new NullPointerException("String to tokenize must not be null"); - else if (seperators == null) - throw new NullPointerException("Tokens to split on must not be null"); + else if (seperators == null) throw new NullPointerException("Tokens to split on must not be null"); this.input = new StringTokenizer(input, seperators); } @@ -67,9 +64,8 @@ public class FunctionalStringTokenizer { * @param toWrap * The non-functional string tokenizer to wrap */ - public FunctionalStringTokenizer(StringTokenizer toWrap) { - if (toWrap == null) - throw new NullPointerException("Wrapped tokenizer must not be null"); + public FunctionalStringTokenizer(final StringTokenizer toWrap) { + if (toWrap == null) throw new NullPointerException("Wrapped tokenizer must not be null"); this.input = toWrap; } @@ -80,9 +76,8 @@ public class FunctionalStringTokenizer { * @param action * The action to execute for each token */ - public void forEachToken(Consumer<String> action) { - if (action == null) - throw new NullPointerException("Action must not be null"); + public void forEachToken(final Consumer<String> action) { + if (action == null) throw new NullPointerException("Action must not be null"); while (input.hasMoreTokens()) { action.accept(input.nextToken()); @@ -128,7 +123,7 @@ public class FunctionalStringTokenizer { * @return This tokenizer, converted into a list of strings */ public IList<String> toList() { - return toList((String element) -> element); + return toList((final String element) -> element); } /** @@ -142,15 +137,14 @@ public class FunctionalStringTokenizer { * The function to use to convert tokens. * @return A list containing all of the converted tokens. */ - public <E> IList<E> toList(Function<String, E> transformer) { - if (transformer == null) - throw new NullPointerException("Transformer must not be null"); + public <E> IList<E> toList(final Function<String, E> transformer) { + if (transformer == null) throw new NullPointerException("Transformer must not be null"); - IList<E> returned = new FunctionalList<>(); + final IList<E> returned = new FunctionalList<>(); // Add each token to the list after transforming it forEachToken(token -> { - E transformedToken = transformer.apply(token); + final E transformedToken = transformer.apply(token); returned.add(transformedToken); }); diff --git a/BJC-Utils2/src/main/java/bjc/utils/funcdata/IList.java b/BJC-Utils2/src/main/java/bjc/utils/funcdata/IList.java index d92d564..de48d6f 100644 --- a/BJC-Utils2/src/main/java/bjc/utils/funcdata/IList.java +++ b/BJC-Utils2/src/main/java/bjc/utils/funcdata/IList.java @@ -1,7 +1,5 @@ package bjc.utils.funcdata; -import bjc.utils.data.IPair; - import java.util.Comparator; import java.util.Iterator; import java.util.function.BiConsumer; @@ -11,6 +9,8 @@ import java.util.function.Function; import java.util.function.Predicate; import java.util.stream.Collector; +import bjc.utils.data.IPair; + /** * A wrapper over another list that provides functional operations over it. * @@ -37,7 +37,7 @@ public interface IList<ContainedType> extends Iterable<ContainedType> { * @return True if every item was successfully added to the list, false * otherwise */ - default boolean addAll(IList<ContainedType> items) { + default boolean addAll(final IList<ContainedType> items) { return items.map(this::add).anyMatch(bl -> bl == false); } @@ -46,16 +46,16 @@ public interface IList<ContainedType> extends Iterable<ContainedType> { * * @param items * The array of items to add. - * + * * @return True if every item was successfully added to the list, false * otherwise. */ @SuppressWarnings("unchecked") - default boolean addAll(ContainedType... items) { + default boolean addAll(final ContainedType... items) { boolean succ = true; - for(ContainedType item : items) { - boolean addSucc = add(item); + for (final ContainedType item : items) { + final boolean addSucc = add(item); succ = succ ? addSucc : false; } @@ -96,10 +96,10 @@ public interface IList<ContainedType> extends Iterable<ContainedType> { * @return The reduced list */ default <StateType, ReducedType> ReducedType collect( - Collector<ContainedType, StateType, ReducedType> collector) { - BiConsumer<StateType, ContainedType> accumulator = collector.accumulator(); + final Collector<ContainedType, StateType, ReducedType> collector) { + final BiConsumer<StateType, ContainedType> accumulator = collector.accumulator(); - StateType initial = collector.supplier().get(); + final StateType initial = collector.supplier().get(); return reduceAux(initial, (value, state) -> { accumulator.accept(state, value); @@ -266,8 +266,8 @@ public interface IList<ContainedType> extends Iterable<ContainedType> { * The items to prepend to the list. */ @SuppressWarnings("unchecked") - default void prependAll(ContainedType... items) { - for(ContainedType item : items) { + default void prependAll(final ContainedType... items) { + for (final ContainedType item : items) { prepend(item); } } diff --git a/BJC-Utils2/src/main/java/bjc/utils/funcdata/IMap.java b/BJC-Utils2/src/main/java/bjc/utils/funcdata/IMap.java index e58409e..0ee7375 100644 --- a/BJC-Utils2/src/main/java/bjc/utils/funcdata/IMap.java +++ b/BJC-Utils2/src/main/java/bjc/utils/funcdata/IMap.java @@ -11,7 +11,7 @@ import java.util.function.Function; * * @param <KeyType> * The type of this map's keys. - * + * * @param <ValueType> * The type of this map's values. */ @@ -30,7 +30,7 @@ public interface IMap<KeyType, ValueType> { * @param action * The action to perform on each key in the map. */ - default void forEachKey(Consumer<KeyType> action) { + default void forEachKey(final Consumer<KeyType> action) { forEach((key, val) -> action.accept(key)); } @@ -40,7 +40,7 @@ public interface IMap<KeyType, ValueType> { * @param action * The action to perform on each value in the map. */ - default void forEachValue(Consumer<ValueType> action) { + default void forEachValue(final Consumer<ValueType> action) { forEach((key, val) -> action.accept(val)); } @@ -49,7 +49,7 @@ public interface IMap<KeyType, ValueType> { * * @param key * The key to check. - * + * * @return Whether or not the map contains the key. */ boolean containsKey(KeyType key); @@ -59,7 +59,7 @@ public interface IMap<KeyType, ValueType> { * * @param key * The key to look for a value under. - * + * * @return The value of the key. */ ValueType get(KeyType key); @@ -70,17 +70,17 @@ public interface IMap<KeyType, ValueType> { * * @param key * The key to attempt to retrieve. - * + * * @param defaultValue * The value to return if the key doesn't exist. - * + * * @return The value associated with the key, or the default value if * the key doesn't exist. */ - default ValueType getOrDefault(KeyType key, ValueType defaultValue) { + default ValueType getOrDefault(final KeyType key, final ValueType defaultValue) { try { return get(key); - } catch (IllegalArgumentException iaex) { + } catch (final IllegalArgumentException iaex) { /* * We don't care about this, because it indicates a key * is missing. @@ -94,10 +94,10 @@ public interface IMap<KeyType, ValueType> { * * @param key * The key to put the value under. - * + * * @param val * The value to add. - * + * * @return The previous value of the key in the map, or null if the key * wasn't in the map. However, note that it may also return null * if the key was set to null. @@ -133,13 +133,13 @@ public interface IMap<KeyType, ValueType> { * * @param <V2> * The new type of returned values. - * + * * @param transformer * The function to use to transform values. - * + * * @return The map where each value will be transformed after lookup. */ - default <V2> IMap<KeyType, V2> transform(Function<ValueType, V2> transformer) { + default <V2> IMap<KeyType, V2> transform(final Function<ValueType, V2> transformer) { return new TransformedValueMap<>(this, transformer); } @@ -156,7 +156,7 @@ public interface IMap<KeyType, ValueType> { * * @param key * The key to remove from the map. - * + * * @return The previous value for the key in the map, or null if the key * wasn't in the class. NOTE: Just because you received null, * doesn't mean the map wasn't changed. It may mean that someone @@ -177,9 +177,9 @@ public interface IMap<KeyType, ValueType> { * @return A list of values in this map. */ default IList<ValueType> valueList() { - IList<ValueType> returns = new FunctionalList<>(); + final IList<ValueType> returns = new FunctionalList<>(); - for (KeyType key : keyList()) { + for (final KeyType key : keyList()) { returns.add(get(key)); } diff --git a/BJC-Utils2/src/main/java/bjc/utils/funcdata/SentryList.java b/BJC-Utils2/src/main/java/bjc/utils/funcdata/SentryList.java index f52a286..c322743 100644 --- a/BJC-Utils2/src/main/java/bjc/utils/funcdata/SentryList.java +++ b/BJC-Utils2/src/main/java/bjc/utils/funcdata/SentryList.java @@ -4,10 +4,11 @@ import java.util.List; /** * A list that logs when items are inserted into it. - * + * * @author bjculkin * - * @param <T> The type of item in the list. + * @param <T> + * The type of item in the list. */ public class SentryList<T> extends FunctionalList<T> { /** @@ -19,19 +20,19 @@ public class SentryList<T> extends FunctionalList<T> { /** * Create a new sentry list backed by an existing list. - * + * * @param backing * The backing list. */ - public SentryList(List<T> backing) { + public SentryList(final List<T> backing) { super(backing); } @Override - public boolean add(T item) { - boolean val = super.add(item); + public boolean add(final T item) { + final boolean val = super.add(item); - if(val) { + if (val) { System.out.println("Added item (" + item + ") to list"); } diff --git a/BJC-Utils2/src/main/java/bjc/utils/funcdata/TransformedValueMap.java b/BJC-Utils2/src/main/java/bjc/utils/funcdata/TransformedValueMap.java index 43bd4d0..0ca1fdc 100644 --- a/BJC-Utils2/src/main/java/bjc/utils/funcdata/TransformedValueMap.java +++ b/BJC-Utils2/src/main/java/bjc/utils/funcdata/TransformedValueMap.java @@ -17,10 +17,11 @@ import java.util.function.Function; * The type of the transformed values */ final class TransformedValueMap<OldKey, OldValue, NewValue> implements IMap<OldKey, NewValue> { - private IMap<OldKey, OldValue> backing; - private Function<OldValue, NewValue> transformer; + private final IMap<OldKey, OldValue> backing; + private final Function<OldValue, NewValue> transformer; - public TransformedValueMap(IMap<OldKey, OldValue> backingMap, Function<OldValue, NewValue> transform) { + public TransformedValueMap(final IMap<OldKey, OldValue> backingMap, + final Function<OldValue, NewValue> transform) { backing = backingMap; transformer = transform; } @@ -31,7 +32,7 @@ final class TransformedValueMap<OldKey, OldValue, NewValue> implements IMap<OldK } @Override - public boolean containsKey(OldKey key) { + public boolean containsKey(final OldKey key) { return backing.containsKey(key); } @@ -41,26 +42,26 @@ final class TransformedValueMap<OldKey, OldValue, NewValue> implements IMap<OldK } @Override - public void forEach(BiConsumer<OldKey, NewValue> action) { + public void forEach(final BiConsumer<OldKey, NewValue> action) { backing.forEach((key, value) -> { action.accept(key, transformer.apply(value)); }); } @Override - public void forEachKey(Consumer<OldKey> action) { + public void forEachKey(final Consumer<OldKey> action) { backing.forEachKey(action); } @Override - public void forEachValue(Consumer<NewValue> action) { + public void forEachValue(final Consumer<NewValue> action) { backing.forEachValue(value -> { action.accept(transformer.apply(value)); }); } @Override - public NewValue get(OldKey key) { + public NewValue get(final OldKey key) { return transformer.apply(backing.get(key)); } @@ -75,17 +76,17 @@ final class TransformedValueMap<OldKey, OldValue, NewValue> implements IMap<OldK } @Override - public <MappedValue> IMap<OldKey, MappedValue> transform(Function<NewValue, MappedValue> transform) { + public <MappedValue> IMap<OldKey, MappedValue> transform(final Function<NewValue, MappedValue> transform) { return new TransformedValueMap<>(this, transform); } @Override - public NewValue put(OldKey key, NewValue value) { + public NewValue put(final OldKey key, final NewValue value) { throw new UnsupportedOperationException("Can't add items to transformed map"); } @Override - public NewValue remove(OldKey key) { + public NewValue remove(final OldKey key) { return transformer.apply(backing.remove(key)); } diff --git a/BJC-Utils2/src/main/java/bjc/utils/funcdata/bst/BinarySearchTree.java b/BJC-Utils2/src/main/java/bjc/utils/funcdata/bst/BinarySearchTree.java index e85ae34..8acd477 100644 --- a/BJC-Utils2/src/main/java/bjc/utils/funcdata/bst/BinarySearchTree.java +++ b/BJC-Utils2/src/main/java/bjc/utils/funcdata/bst/BinarySearchTree.java @@ -1,13 +1,13 @@ package bjc.utils.funcdata.bst; -import bjc.utils.funcdata.FunctionalList; -import bjc.utils.funcdata.IList; - import java.util.ArrayList; import java.util.Comparator; import java.util.List; import java.util.function.Predicate; +import bjc.utils.funcdata.FunctionalList; +import bjc.utils.funcdata.IList; + /** * A binary search tree, with some mild support for functional traversal. * @@ -20,7 +20,7 @@ public class BinarySearchTree<T> { /* * The comparator for use in ordering items */ - private Comparator<T> comparator; + private final Comparator<T> comparator; /* * The current count of elements in the tree @@ -38,9 +38,8 @@ public class BinarySearchTree<T> { * @param cmp * The thing to use for comparing elements */ - public BinarySearchTree(Comparator<T> cmp) { - if (cmp == null) - throw new NullPointerException("Comparator must not be null"); + public BinarySearchTree(final Comparator<T> cmp) { + if (cmp == null) throw new NullPointerException("Comparator must not be null"); elementCount = 0; comparator = cmp; @@ -52,7 +51,7 @@ public class BinarySearchTree<T> { * @param element * The data to add to the binary search tree. */ - public void addNode(T element) { + public void addNode(final T element) { elementCount++; if (root == null) { @@ -73,7 +72,7 @@ public class BinarySearchTree<T> { * The distance from the pivot * @return Whether the adjusted pivot is with the list */ - private boolean adjustedPivotInBounds(IList<T> elements, int pivot, int pivotAdjustment) { + private boolean adjustedPivotInBounds(final IList<T> elements, final int pivot, final int pivotAdjustment) { return pivot - pivotAdjustment >= 0 && pivot + pivotAdjustment < elements.getSize(); } @@ -83,7 +82,7 @@ public class BinarySearchTree<T> { * Takes O(N) time, but also O(N) space. */ public void balance() { - IList<T> elements = new FunctionalList<>(); + final IList<T> elements = new FunctionalList<>(); // Add each element to the list in sorted order root.forEach(TreeLinearizationMethod.INORDER, element -> elements.add(element)); @@ -92,7 +91,7 @@ public class BinarySearchTree<T> { root = null; // Set up the pivot and adjustment for readding elements - int pivot = elements.getSize() / 2; + final int pivot = elements.getSize() / 2; int pivotAdjustment = 0; // Add elements until there aren't any left @@ -129,7 +128,7 @@ public class BinarySearchTree<T> { * @param element * The node to delete */ - public void deleteNode(T element) { + public void deleteNode(final T element) { elementCount--; root.delete(element, comparator); @@ -151,7 +150,7 @@ public class BinarySearchTree<T> { * The node to check the presence of for the tree. * @return Whether or not the node is in the tree. */ - public boolean isInTree(T element) { + public boolean isInTree(final T element) { return root.contains(element, comparator); } @@ -163,11 +162,10 @@ public class BinarySearchTree<T> { * @param traversalPredicate * The function to use until it fails */ - public void traverse(TreeLinearizationMethod linearizationMethod, Predicate<T> traversalPredicate) { + public void traverse(final TreeLinearizationMethod linearizationMethod, final Predicate<T> traversalPredicate) { if (linearizationMethod == null) throw new NullPointerException("Linearization method must not be null"); - else if (traversalPredicate == null) - throw new NullPointerException("Predicate must not be nulls"); + else if (traversalPredicate == null) throw new NullPointerException("Predicate must not be nulls"); root.forEach(linearizationMethod, traversalPredicate); } @@ -176,7 +174,7 @@ public class BinarySearchTree<T> { * Remove all soft-deleted nodes from the tree. */ public void trim() { - List<T> nodes = new ArrayList<>(elementCount); + final List<T> nodes = new ArrayList<>(elementCount); // Add all non-soft deleted nodes to the tree in insertion order traverse(TreeLinearizationMethod.PREORDER, node -> { @@ -201,28 +199,22 @@ public class BinarySearchTree<T> { final int prime = 31; int result = 1; result = prime * result + elementCount; - result = prime * result + ((root == null) ? 0 : root.hashCode()); + result = prime * result + (root == null ? 0 : root.hashCode()); return result; } @Override - public boolean equals(Object obj) { - if (this == obj) - return true; - if (obj == null) - return false; - if (!(obj instanceof BinarySearchTree<?>)) - return false; + public boolean equals(final Object obj) { + if (this == obj) return true; + if (obj == null) return false; + if (!(obj instanceof BinarySearchTree<?>)) return false; - BinarySearchTree<?> other = (BinarySearchTree<?>) obj; + final BinarySearchTree<?> other = (BinarySearchTree<?>) obj; - if (elementCount != other.elementCount) - return false; + if (elementCount != other.elementCount) return false; if (root == null) { - if (other.root != null) - return false; - } else if (!root.equals(other.root)) - return false; + if (other.root != null) return false; + } else if (!root.equals(other.root)) return false; return true; } diff --git a/BJC-Utils2/src/main/java/bjc/utils/funcdata/bst/BinarySearchTreeLeaf.java b/BJC-Utils2/src/main/java/bjc/utils/funcdata/bst/BinarySearchTreeLeaf.java index fe30dad..8c4f3f0 100644 --- a/BJC-Utils2/src/main/java/bjc/utils/funcdata/bst/BinarySearchTreeLeaf.java +++ b/BJC-Utils2/src/main/java/bjc/utils/funcdata/bst/BinarySearchTreeLeaf.java @@ -30,25 +30,24 @@ public class BinarySearchTreeLeaf<T> implements ITreePart<T> { * @param element * The data for the leaf to hold. */ - public BinarySearchTreeLeaf(T element) { + public BinarySearchTreeLeaf(final T element) { data = element; } @Override - public void add(T element, Comparator<T> comparator) { + public void add(final T element, final Comparator<T> comparator) { throw new IllegalArgumentException("Can't add to a leaf."); } @Override - public <E> E collapse(Function<T, E> leafTransformer, BiFunction<E, E, E> branchCollapser) { - if (leafTransformer == null) - throw new NullPointerException("Transformer must not be null"); + public <E> E collapse(final Function<T, E> leafTransformer, final BiFunction<E, E, E> branchCollapser) { + if (leafTransformer == null) throw new NullPointerException("Transformer must not be null"); return leafTransformer.apply(data); } @Override - public boolean contains(T element, Comparator<T> comparator) { + public boolean contains(final T element, final Comparator<T> comparator) { return this.data.equals(element); } @@ -58,16 +57,15 @@ public class BinarySearchTreeLeaf<T> implements ITreePart<T> { } @Override - public void delete(T element, Comparator<T> comparator) { + public void delete(final T element, final Comparator<T> comparator) { if (data.equals(element)) { isDeleted = true; } } @Override - public boolean directedWalk(DirectedWalkFunction<T> treeWalker) { - if (treeWalker == null) - throw new NullPointerException("Tree walker must not be null"); + public boolean directedWalk(final DirectedWalkFunction<T> treeWalker) { + if (treeWalker == null) throw new NullPointerException("Tree walker must not be null"); switch (treeWalker.walk(data)) { case SUCCESS: @@ -82,9 +80,9 @@ public class BinarySearchTreeLeaf<T> implements ITreePart<T> { } @Override - public boolean forEach(TreeLinearizationMethod linearizationMethod, Predicate<T> traversalPredicate) { - if (traversalPredicate == null) - throw new NullPointerException("Predicate must not be null"); + public boolean forEach(final TreeLinearizationMethod linearizationMethod, + final Predicate<T> traversalPredicate) { + if (traversalPredicate == null) throw new NullPointerException("Predicate must not be null"); return traversalPredicate.test(data); } @@ -98,29 +96,23 @@ public class BinarySearchTreeLeaf<T> implements ITreePart<T> { public int hashCode() { final int prime = 31; int result = 1; - result = prime * result + ((data == null) ? 0 : data.hashCode()); + result = prime * result + (data == null ? 0 : data.hashCode()); result = prime * result + (isDeleted ? 1231 : 1237); return result; } @Override - public boolean equals(Object obj) { - if (this == obj) - return true; - if (obj == null) - return false; - if (!(obj instanceof BinarySearchTreeLeaf<?>)) - return false; + public boolean equals(final Object obj) { + if (this == obj) return true; + if (obj == null) return false; + if (!(obj instanceof BinarySearchTreeLeaf<?>)) return false; - BinarySearchTreeLeaf<?> other = (BinarySearchTreeLeaf<?>) obj; + final BinarySearchTreeLeaf<?> other = (BinarySearchTreeLeaf<?>) obj; if (data == null) { - if (other.data != null) - return false; - } else if (!data.equals(other.data)) - return false; - if (isDeleted != other.isDeleted) - return false; + if (other.data != null) return false; + } else if (!data.equals(other.data)) return false; + if (isDeleted != other.isDeleted) return false; return true; } diff --git a/BJC-Utils2/src/main/java/bjc/utils/funcdata/bst/BinarySearchTreeNode.java b/BJC-Utils2/src/main/java/bjc/utils/funcdata/bst/BinarySearchTreeNode.java index 527f221..9f45c17 100644 --- a/BJC-Utils2/src/main/java/bjc/utils/funcdata/bst/BinarySearchTreeNode.java +++ b/BJC-Utils2/src/main/java/bjc/utils/funcdata/bst/BinarySearchTreeNode.java @@ -1,15 +1,15 @@ package bjc.utils.funcdata.bst; -import java.util.Comparator; -import java.util.function.BiFunction; -import java.util.function.Function; -import java.util.function.Predicate; - import static bjc.utils.funcdata.bst.DirectedWalkFunction.DirectedWalkResult.FAILURE; import static bjc.utils.funcdata.bst.DirectedWalkFunction.DirectedWalkResult.LEFT; import static bjc.utils.funcdata.bst.DirectedWalkFunction.DirectedWalkResult.RIGHT; import static bjc.utils.funcdata.bst.DirectedWalkFunction.DirectedWalkResult.SUCCESS; +import java.util.Comparator; +import java.util.function.BiFunction; +import java.util.function.Function; +import java.util.function.Predicate; + /** * A binary node in a tree. * @@ -39,16 +39,15 @@ public class BinarySearchTreeNode<T> extends BinarySearchTreeLeaf<T> { * @param rght * The right child of this node. */ - public BinarySearchTreeNode(T element, ITreePart<T> lft, ITreePart<T> rght) { + public BinarySearchTreeNode(final T element, final ITreePart<T> lft, final ITreePart<T> rght) { super(element); this.left = lft; this.right = rght; } @Override - public void add(T element, Comparator<T> comparator) { - if (comparator == null) - throw new NullPointerException("Comparator must not be null"); + public void add(final T element, final Comparator<T> comparator) { + if (comparator == null) throw new NullPointerException("Comparator must not be null"); switch (comparator.compare(data, element)) { case -1: @@ -61,8 +60,7 @@ public class BinarySearchTreeNode<T> extends BinarySearchTreeLeaf<T> { case 0: if (isDeleted) { isDeleted = false; - } else - throw new IllegalArgumentException("Can't add duplicate values"); + } else throw new IllegalArgumentException("Can't add duplicate values"); break; case 1: if (right == null) { @@ -77,19 +75,20 @@ public class BinarySearchTreeNode<T> extends BinarySearchTreeLeaf<T> { } @Override - public <E> E collapse(Function<T, E> nodeCollapser, BiFunction<E, E, E> branchCollapser) { + public <E> E collapse(final Function<T, E> nodeCollapser, final BiFunction<E, E, E> branchCollapser) { if (nodeCollapser == null || branchCollapser == null) throw new NullPointerException("Collapser must not be null"); - E collapsedNode = nodeCollapser.apply(data); + final E collapsedNode = nodeCollapser.apply(data); if (left != null) { - E collapsedLeftBranch = left.collapse(nodeCollapser, branchCollapser); + final E collapsedLeftBranch = left.collapse(nodeCollapser, branchCollapser); if (right != null) { - E collapsedRightBranch = right.collapse(nodeCollapser, branchCollapser); + final E collapsedRightBranch = right.collapse(nodeCollapser, branchCollapser); - E collapsedBranches = branchCollapser.apply(collapsedLeftBranch, collapsedRightBranch); + final E collapsedBranches = branchCollapser.apply(collapsedLeftBranch, + collapsedRightBranch); return branchCollapser.apply(collapsedNode, collapsedBranches); } @@ -98,7 +97,7 @@ public class BinarySearchTreeNode<T> extends BinarySearchTreeLeaf<T> { } if (right != null) { - E collapsedRightBranch = right.collapse(nodeCollapser, branchCollapser); + final E collapsedRightBranch = right.collapse(nodeCollapser, branchCollapser); return branchCollapser.apply(collapsedNode, collapsedRightBranch); } @@ -107,9 +106,8 @@ public class BinarySearchTreeNode<T> extends BinarySearchTreeLeaf<T> { } @Override - public boolean contains(T element, Comparator<T> comparator) { - if (comparator == null) - throw new NullPointerException("Comparator must not be null"); + public boolean contains(final T element, final Comparator<T> comparator) { + if (comparator == null) throw new NullPointerException("Comparator must not be null"); return directedWalk(currentElement -> { switch (comparator.compare(element, currentElement)) { @@ -126,9 +124,8 @@ public class BinarySearchTreeNode<T> extends BinarySearchTreeLeaf<T> { } @Override - public void delete(T element, Comparator<T> comparator) { - if (comparator == null) - throw new NullPointerException("Comparator must not be null"); + public void delete(final T element, final Comparator<T> comparator) { + if (comparator == null) throw new NullPointerException("Comparator must not be null"); directedWalk(currentElement -> { switch (comparator.compare(data, element)) { @@ -146,9 +143,8 @@ public class BinarySearchTreeNode<T> extends BinarySearchTreeLeaf<T> { } @Override - public boolean directedWalk(DirectedWalkFunction<T> treeWalker) { - if (treeWalker == null) - throw new NullPointerException("Walker must not be null"); + public boolean directedWalk(final DirectedWalkFunction<T> treeWalker) { + if (treeWalker == null) throw new NullPointerException("Walker must not be null"); switch (treeWalker.walk(data)) { case SUCCESS: @@ -165,11 +161,11 @@ public class BinarySearchTreeNode<T> extends BinarySearchTreeLeaf<T> { } @Override - public boolean forEach(TreeLinearizationMethod linearizationMethod, Predicate<T> traversalPredicate) { + public boolean forEach(final TreeLinearizationMethod linearizationMethod, + final Predicate<T> traversalPredicate) { if (linearizationMethod == null) throw new NullPointerException("Linearization method must not be null"); - else if (traversalPredicate == null) - throw new NullPointerException("Predicate must not be null"); + else if (traversalPredicate == null) throw new NullPointerException("Predicate must not be null"); switch (linearizationMethod) { case PREORDER: @@ -184,48 +180,41 @@ public class BinarySearchTreeNode<T> extends BinarySearchTreeLeaf<T> { } } - private boolean inorderTraverse(TreeLinearizationMethod linearizationMethod, Predicate<T> traversalPredicate) { - if (!traverseLeftBranch(linearizationMethod, traversalPredicate)) - return false; + private boolean inorderTraverse(final TreeLinearizationMethod linearizationMethod, + final Predicate<T> traversalPredicate) { + if (!traverseLeftBranch(linearizationMethod, traversalPredicate)) return false; - if (!traverseElement(traversalPredicate)) - return false; + if (!traverseElement(traversalPredicate)) return false; - if (!traverseRightBranch(linearizationMethod, traversalPredicate)) - return false; + if (!traverseRightBranch(linearizationMethod, traversalPredicate)) return false; return true; } - private boolean postorderTraverse(TreeLinearizationMethod linearizationMethod, - Predicate<T> traversalPredicate) { - if (!traverseLeftBranch(linearizationMethod, traversalPredicate)) - return false; + private boolean postorderTraverse(final TreeLinearizationMethod linearizationMethod, + final Predicate<T> traversalPredicate) { + if (!traverseLeftBranch(linearizationMethod, traversalPredicate)) return false; - if (!traverseRightBranch(linearizationMethod, traversalPredicate)) - return false; + if (!traverseRightBranch(linearizationMethod, traversalPredicate)) return false; - if (!traverseElement(traversalPredicate)) - return false; + if (!traverseElement(traversalPredicate)) return false; return true; } - private boolean preorderTraverse(TreeLinearizationMethod linearizationMethod, Predicate<T> traversalPredicate) { - if (!traverseElement(traversalPredicate)) - return false; + private boolean preorderTraverse(final TreeLinearizationMethod linearizationMethod, + final Predicate<T> traversalPredicate) { + if (!traverseElement(traversalPredicate)) return false; - if (!traverseLeftBranch(linearizationMethod, traversalPredicate)) - return false; + if (!traverseLeftBranch(linearizationMethod, traversalPredicate)) return false; - if (!traverseRightBranch(linearizationMethod, traversalPredicate)) - return false; + if (!traverseRightBranch(linearizationMethod, traversalPredicate)) return false; return true; } - private boolean traverseElement(Predicate<T> traversalPredicate) { + private boolean traverseElement(final Predicate<T> traversalPredicate) { boolean nodeSuccesfullyTraversed; if (isDeleted) { @@ -237,8 +226,8 @@ public class BinarySearchTreeNode<T> extends BinarySearchTreeLeaf<T> { return nodeSuccesfullyTraversed; } - private boolean traverseLeftBranch(TreeLinearizationMethod linearizationMethod, - Predicate<T> traversalPredicate) { + private boolean traverseLeftBranch(final TreeLinearizationMethod linearizationMethod, + final Predicate<T> traversalPredicate) { boolean leftSuccesfullyTraversed; if (left == null) { @@ -250,8 +239,8 @@ public class BinarySearchTreeNode<T> extends BinarySearchTreeLeaf<T> { return leftSuccesfullyTraversed; } - private boolean traverseRightBranch(TreeLinearizationMethod linearizationMethod, - Predicate<T> traversalPredicate) { + private boolean traverseRightBranch(final TreeLinearizationMethod linearizationMethod, + final Predicate<T> traversalPredicate) { boolean rightSuccesfullyTraversed; if (right == null) { @@ -272,33 +261,26 @@ public class BinarySearchTreeNode<T> extends BinarySearchTreeLeaf<T> { public int hashCode() { final int prime = 31; int result = super.hashCode(); - result = prime * result + ((left == null) ? 0 : left.hashCode()); - result = prime * result + ((right == null) ? 0 : right.hashCode()); + result = prime * result + (left == null ? 0 : left.hashCode()); + result = prime * result + (right == null ? 0 : right.hashCode()); return result; } @Override - public boolean equals(Object obj) { - if (this == obj) - return true; - if (!super.equals(obj)) - return false; - if (!(obj instanceof BinarySearchTreeNode<?>)) - return false; + public boolean equals(final Object obj) { + if (this == obj) return true; + if (!super.equals(obj)) return false; + if (!(obj instanceof BinarySearchTreeNode<?>)) return false; - BinarySearchTreeNode<?> other = (BinarySearchTreeNode<?>) obj; + final BinarySearchTreeNode<?> other = (BinarySearchTreeNode<?>) obj; if (left == null) { - if (other.left != null) - return false; - } else if (!left.equals(other.left)) - return false; + if (other.left != null) return false; + } else if (!left.equals(other.left)) return false; if (right == null) { - if (other.right != null) - return false; - } else if (!right.equals(other.right)) - return false; + if (other.right != null) return false; + } else if (!right.equals(other.right)) return false; return true; } diff --git a/BJC-Utils2/src/main/java/bjc/utils/funcdata/theory/Bifunctor.java b/BJC-Utils2/src/main/java/bjc/utils/funcdata/theory/Bifunctor.java index 5380b24..13c1709 100644 --- a/BJC-Utils2/src/main/java/bjc/utils/funcdata/theory/Bifunctor.java +++ b/BJC-Utils2/src/main/java/bjc/utils/funcdata/theory/Bifunctor.java @@ -15,9 +15,9 @@ import java.util.function.Function; public interface Bifunctor<LeftType, RightType> { /** * Alias for functor mapping. - * + * * @author EVE - * + * * @param <OldLeft> * @param <OldRight> * @param <NewLeft> @@ -30,7 +30,7 @@ public interface Bifunctor<LeftType, RightType> { /** * Alias for left functor mapping. - * + * * @author EVE * * @param <OldLeft> @@ -44,7 +44,7 @@ public interface Bifunctor<LeftType, RightType> { /** * Alias for right functor mapping. - * + * * @author EVE * * @param <OldLeft> @@ -75,12 +75,12 @@ public interface Bifunctor<LeftType, RightType> { * @return A function that maps over both parts of the pair */ public default <OldLeft, OldRight, NewLeft, NewRight> BifunctorMap<OldLeft, OldRight, NewLeft, NewRight> bimap( - Function<OldLeft, NewLeft> leftFunc, Function<OldRight, NewRight> rightFunc) { - BifunctorMap<OldLeft, OldRight, NewLeft, NewRight> bimappedFunc = (argPair) -> { - LeftBifunctorMap<OldLeft, OldRight, NewLeft> leftMapper = argPair.fmapLeft(leftFunc); + final Function<OldLeft, NewLeft> leftFunc, final Function<OldRight, NewRight> rightFunc) { + final BifunctorMap<OldLeft, OldRight, NewLeft, NewRight> bimappedFunc = (argPair) -> { + final LeftBifunctorMap<OldLeft, OldRight, NewLeft> leftMapper = argPair.fmapLeft(leftFunc); - Bifunctor<NewLeft, OldRight> leftMappedFunctor = leftMapper.apply(argPair); - RightBifunctorMap<NewLeft, OldRight, NewRight> rightMapper = leftMappedFunctor + final Bifunctor<NewLeft, OldRight> leftMappedFunctor = leftMapper.apply(argPair); + final RightBifunctorMap<NewLeft, OldRight, NewRight> rightMapper = leftMappedFunctor .fmapRight(rightFunc); return rightMapper.apply(leftMappedFunctor); |
