diff options
| author | Ben Culkin <scorpress@gmail.com> | 2020-04-13 18:43:13 -0400 |
|---|---|---|
| committer | Ben Culkin <scorpress@gmail.com> | 2020-04-13 18:43:13 -0400 |
| commit | f51f6da7319787348c38b875652b5c0e9f88c8aa (patch) | |
| tree | 943888fc724da2d2dedd89abec99dcbfcc089fd0 /src/main/java/bjc/funcdata | |
| parent | 9052ed6da37af23ea82588d248f409e60a33c6cb (diff) | |
Cleanup pass
Pass to do some cleanups
Diffstat (limited to 'src/main/java/bjc/funcdata')
| -rw-r--r-- | src/main/java/bjc/funcdata/ExtendedMap.java | 50 | ||||
| -rw-r--r-- | src/main/java/bjc/funcdata/FunctionalList.java | 129 | ||||
| -rw-r--r-- | src/main/java/bjc/funcdata/FunctionalMap.java | 44 | ||||
| -rw-r--r-- | src/main/java/bjc/funcdata/FunctionalStringTokenizer.java | 47 | ||||
| -rw-r--r-- | src/main/java/bjc/funcdata/IList.java | 162 | ||||
| -rw-r--r-- | src/main/java/bjc/funcdata/IMap.java | 76 | ||||
| -rw-r--r-- | src/main/java/bjc/funcdata/SentryList.java | 6 | ||||
| -rw-r--r-- | src/main/java/bjc/funcdata/TransformedValueMap.java | 16 | ||||
| -rw-r--r-- | src/main/java/bjc/funcdata/bst/BinarySearchTree.java | 79 | ||||
| -rw-r--r-- | src/main/java/bjc/funcdata/bst/BinarySearchTreeLeaf.java | 43 | ||||
| -rw-r--r-- | src/main/java/bjc/funcdata/bst/BinarySearchTreeNode.java | 126 | ||||
| -rw-r--r-- | src/main/java/bjc/funcdata/bst/DirectedWalkFunction.java | 10 | ||||
| -rw-r--r-- | src/main/java/bjc/funcdata/bst/ITreePart.java | 47 | ||||
| -rw-r--r-- | src/main/java/bjc/funcdata/bst/TreeLinearizationMethod.java | 12 | ||||
| -rw-r--r-- | src/main/java/bjc/funcdata/theory/Bifunctor.java | 92 | ||||
| -rw-r--r-- | src/main/java/bjc/funcdata/theory/Functor.java | 22 |
16 files changed, 530 insertions, 431 deletions
diff --git a/src/main/java/bjc/funcdata/ExtendedMap.java b/src/main/java/bjc/funcdata/ExtendedMap.java index 76fac01..bd500f4 100644 --- a/src/main/java/bjc/funcdata/ExtendedMap.java +++ b/src/main/java/bjc/funcdata/ExtendedMap.java @@ -11,10 +11,10 @@ import java.util.function.Function; * @author Ben Culkin * * @param <KeyType> - * The type of the keys of the map. + * The type of the keys of the map. * * @param <ValueType> - * The type of the values of the map. + * The type of the values of the map. */ class ExtendedMap<KeyType, ValueType> implements IMap<KeyType, ValueType> { /* The map we delegate lookups to. */ @@ -26,12 +26,13 @@ class ExtendedMap<KeyType, ValueType> implements IMap<KeyType, ValueType> { * Create a new extended map. * * @param delegate - * The map to lookup things in. - * + * The map to lookup things in. + * * @param store - * The map to store things in. + * The map to store things in. */ - public ExtendedMap(final IMap<KeyType, ValueType> delegate, final IMap<KeyType, ValueType> store) { + public ExtendedMap(final IMap<KeyType, ValueType> delegate, + final IMap<KeyType, ValueType> store) { this.delegate = delegate; this.store = store; } @@ -43,7 +44,8 @@ class ExtendedMap<KeyType, ValueType> implements IMap<KeyType, ValueType> { @Override public boolean containsKey(final KeyType key) { - if(store.containsKey(key)) return true; + if (store.containsKey(key)) + return true; return delegate.containsKey(key); } @@ -76,7 +78,8 @@ class ExtendedMap<KeyType, ValueType> implements IMap<KeyType, ValueType> { @Override public ValueType get(final KeyType key) { - if(store.containsKey(key)) return store.get(key); + if (store.containsKey(key)) + return store.get(key); return delegate.get(key); } @@ -97,7 +100,8 @@ class ExtendedMap<KeyType, ValueType> implements IMap<KeyType, ValueType> { } @Override - public <MappedValue> IMap<KeyType, MappedValue> transform(final Function<ValueType, MappedValue> transformer) { + public <MappedValue> IMap<KeyType, MappedValue> + transform(final Function<ValueType, MappedValue> transformer) { return new TransformedValueMap<>(this, transformer); } @@ -108,7 +112,8 @@ class ExtendedMap<KeyType, ValueType> implements IMap<KeyType, ValueType> { @Override public ValueType remove(final KeyType key) { - if(!store.containsKey(key)) return delegate.remove(key); + if (!store.containsKey(key)) + return delegate.remove(key); return store.remove(key); } @@ -134,18 +139,25 @@ class ExtendedMap<KeyType, ValueType> implements IMap<KeyType, ValueType> { @Override public boolean equals(final Object obj) { - if(this == obj) return true; - if(obj == null) return false; - if(!(obj instanceof ExtendedMap)) return false; + if (this == obj) + return true; + if (obj == null) + return false; + if (!(obj instanceof ExtendedMap)) + return false; final ExtendedMap<?, ?> other = (ExtendedMap<?, ?>) obj; - if(delegate == null) { - 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 (delegate == null) { + 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; return true; } diff --git a/src/main/java/bjc/funcdata/FunctionalList.java b/src/main/java/bjc/funcdata/FunctionalList.java index 99b36fe..2cdfa27 100644 --- a/src/main/java/bjc/funcdata/FunctionalList.java +++ b/src/main/java/bjc/funcdata/FunctionalList.java @@ -27,7 +27,7 @@ import bjc.data.Pair; * @author ben * * @param <E> - * The type in this list + * The type in this list */ public class FunctionalList<E> implements Cloneable, IList<E> { /* The list used as a backing store */ @@ -44,35 +44,36 @@ public class FunctionalList<E> implements Cloneable, IList<E> { * Takes O(n) time, where n is the number of items specified * * @param items - * The items to put into this functional list. + * The items to put into this functional list. */ @SafeVarargs public FunctionalList(final E... items) { wrapped = new ArrayList<>(items.length); - for(final E item : items) { + for (final E item : items) { wrapped.add(item); } } - + /** * 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. + * The items to put into this functional list. * @return The returned list. */ @SafeVarargs public static <T> IList<T> listOf(final T... items) { return new FunctionalList<>(items); } + /** * Create a new functional list with the specified size. * * @param size - * The size of the backing list . + * The size of the backing list . */ private FunctionalList(final int size) { wrapped = new ArrayList<>(size); @@ -84,10 +85,11 @@ public class FunctionalList<E> implements Cloneable, IList<E> { * Takes O(1) time, since it doesn't copy the list. * * @param backing - * The list to use as a backing list. + * The list to use as a backing list. */ public FunctionalList(final 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; } @@ -99,10 +101,11 @@ public class FunctionalList<E> implements Cloneable, IList<E> { @Override public boolean allMatch(final 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(final E item : wrapped) { - if(!predicate.test(item)) + for (final E item : wrapped) { + if (!predicate.test(item)) /* We've found a non-matching item. */ return false; } @@ -113,10 +116,11 @@ public class FunctionalList<E> implements Cloneable, IList<E> { @Override public boolean anyMatch(final 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(final E item : wrapped) { - if(predicate.test(item)) + for (final E item : wrapped) { + if (predicate.test(item)) /* We've found a matching item. */ return true; } @@ -136,7 +140,7 @@ public class FunctionalList<E> implements Cloneable, IList<E> { public IList<E> clone() { final IList<E> cloned = new FunctionalList<>(); - for(final E element : wrapped) { + for (final E element : wrapped) { cloned.add(element); } @@ -144,10 +148,11 @@ public class FunctionalList<E> implements Cloneable, IList<E> { } @Override - public <T, F> IList<F> combineWith(final IList<T> rightList, final BiFunction<E, T, F> itemCombiner) { - if(rightList == null) { + 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) { + } else if (itemCombiner == null) { throw new NullPointerException("Combiner must not be null"); } @@ -156,8 +161,8 @@ public class FunctionalList<E> implements Cloneable, IList<E> { /* Get the iterator for the other list. */ final Iterator<T> rightIterator = rightList.toIterable().iterator(); - for(final Iterator<E> leftIterator = wrapped.iterator(); leftIterator.hasNext() - && rightIterator.hasNext();) { + for (final Iterator<E> leftIterator = wrapped.iterator(); + leftIterator.hasNext() && rightIterator.hasNext();) { /* Add the transformed items to the result list. */ final E leftVal = leftIterator.next(); final T rightVal = rightIterator.next(); @@ -176,21 +181,27 @@ 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 E last() { - if(wrapped.size() < 1) throw new NoSuchElementException("Attempted to get last element of empty list"); + if (wrapped.size() < 1) + throw new NoSuchElementException( + "Attempted to get last element of empty list"); return wrapped.get(wrapped.size() - 1); } @Override public E popFirst() { - if(wrapped.size() < 1) throw new NoSuchElementException("Attempted to pop first element of empty list"); + if (wrapped.size() < 1) + throw new NoSuchElementException( + "Attempted to pop first element of empty list"); E head = first(); wrapped.remove(0); @@ -200,7 +211,9 @@ public class FunctionalList<E> implements Cloneable, IList<E> { @Override public E popLast() { - if(wrapped.size() < 1) throw new NoSuchElementException("Attempted to pop last element of empty list"); + if (wrapped.size() < 1) + throw new NoSuchElementException( + "Attempted to pop last element of empty list"); E tail = last(); wrapped.remove(wrapped.size() - 1); @@ -210,14 +223,16 @@ public class FunctionalList<E> implements Cloneable, IList<E> { @Override public <T> IList<T> flatMap(final 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"); final IList<T> returned = new FunctionalList<>(this.wrapped.size()); forEach(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); @@ -228,22 +243,23 @@ public class FunctionalList<E> implements Cloneable, IList<E> { @Override public void forEach(final 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(final 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. + * This is held b/c ref'd variables must be final/effectively final. */ final IHolder<Integer> currentIndex = new Identity<>(0); - wrapped.forEach((element) -> { + wrapped.forEach(element -> { /* Call the action with the index and the value. */ indexedAction.accept(currentIndex.unwrap(index -> index), element); @@ -268,15 +284,15 @@ public class FunctionalList<E> implements Cloneable, IList<E> { @Override public IList<E> getMatching(final 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"); final IList<E> returned = new FunctionalList<>(); - wrapped.forEach((element) -> { - if(predicate.test(element)) { + wrapped.forEach(element -> { + if (predicate.test(element)) { /* - * The item matches, so add it to the returned - * list. + * The item matches, so add it to the returned list. */ returned.add(element); } @@ -296,13 +312,16 @@ public class FunctionalList<E> implements Cloneable, IList<E> { } /* Check if a partition has room for another item. */ - private Boolean isPartitionFull(final int numberPerPartition, final IHolder<IList<E>> currentPartition) { - return currentPartition.unwrap((partition) -> partition.getSize() >= numberPerPartition); + private Boolean isPartitionFull(final int numberPerPartition, + final IHolder<IList<E>> currentPartition) { + return currentPartition + .unwrap(partition -> partition.getSize() >= numberPerPartition); } @Override public <T> IList<T> map(final 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"); final IList<T> returned = new FunctionalList<>(this.wrapped.size()); @@ -321,8 +340,9 @@ public class FunctionalList<E> implements Cloneable, IList<E> { @Override public IList<IList<E>> partition(final int numberPerPartition) { - if(numberPerPartition < 1 || numberPerPartition > wrapped.size()) { - final String fmt = "%s is an invalid partition size. Must be between 1 and %d"; + if (numberPerPartition < 1 || 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); @@ -334,7 +354,7 @@ public class FunctionalList<E> implements Cloneable, IList<E> { final 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)); @@ -356,7 +376,8 @@ public class FunctionalList<E> implements Cloneable, IList<E> { @Override public E randItem(final 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"); final int randomIndex = rnd.apply(wrapped.size()); @@ -364,11 +385,12 @@ public class FunctionalList<E> implements Cloneable, IList<E> { } @Override - public <T, F> F reduceAux(final T initialValue, final BiFunction<E, T, T> stateAccumulator, + public <T, F> F reduceAux(final T initialValue, + final BiFunction<E, T, T> stateAccumulator, final Function<T, F> resultTransformer) { - if(stateAccumulator == null) { + if (stateAccumulator == null) { throw new NullPointerException("Accumulator must not be null"); - } else if(resultTransformer == null) { + } else if (resultTransformer == null) { throw new NullPointerException("Transformer must not be null"); } @@ -386,7 +408,8 @@ public class FunctionalList<E> implements Cloneable, IList<E> { @Override public boolean removeIf(final 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); } @@ -406,7 +429,7 @@ public class FunctionalList<E> implements Cloneable, IList<E> { /* Search our internal list. */ final int foundIndex = Collections.binarySearch(wrapped, searchKey, comparator); - if(foundIndex >= 0) { + if (foundIndex >= 0) { /* We found a matching element. */ return wrapped.get(foundIndex); } @@ -439,19 +462,21 @@ public class FunctionalList<E> implements Cloneable, IList<E> { public String toString() { final int lSize = getSize(); - if(lSize == 0) return "()"; + if (lSize == 0) + return "()"; 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(final E item : toIterable()) { + for (final E item : toIterable()) { sb.append(item.toString()); - if(i < lSize - 1) { + if (i < lSize - 1) { sb.append(", "); } diff --git a/src/main/java/bjc/funcdata/FunctionalMap.java b/src/main/java/bjc/funcdata/FunctionalMap.java index fc53829..aba3dd1 100644 --- a/src/main/java/bjc/funcdata/FunctionalMap.java +++ b/src/main/java/bjc/funcdata/FunctionalMap.java @@ -14,10 +14,10 @@ import bjc.data.IPair; * @author ben * * @param <KeyType> - * The type of the map's keys. + * The type of the map's keys. * * @param <ValueType> - * The type of the map's values. + * The type of the map's values. */ public class FunctionalMap<KeyType, ValueType> implements IMap<KeyType, ValueType> { /* Our backing store. */ @@ -32,13 +32,13 @@ public class FunctionalMap<KeyType, ValueType> implements IMap<KeyType, ValueTyp * Create a new functional map with the specified entries. * * @param entries - * The entries to put into the map. + * The entries to put into the map. */ @SafeVarargs public FunctionalMap(final IPair<KeyType, ValueType>... entries) { this(); - for(final IPair<KeyType, ValueType> entry : entries) { + for (final IPair<KeyType, ValueType> entry : entries) { entry.doWith((key, val) -> { wrappedMap.put(key, val); }); @@ -49,10 +49,11 @@ public class FunctionalMap<KeyType, ValueType> implements IMap<KeyType, ValueTyp * Create a new functional map wrapping the specified map. * * @param wrap - * The map to wrap. + * The map to wrap. */ public FunctionalMap(final Map<KeyType, ValueType> wrap) { - if(wrap == null) throw new NullPointerException("Map to wrap must not be null"); + if (wrap == null) + throw new NullPointerException("Map to wrap must not be null"); wrappedMap = wrap; } @@ -89,9 +90,10 @@ public class FunctionalMap<KeyType, ValueType> implements IMap<KeyType, ValueTyp @Override public ValueType get(final KeyType key) { - if(key == null) throw new NullPointerException("Key must not be null"); + if (key == null) + throw new NullPointerException("Key must not be null"); - if(!wrappedMap.containsKey(key)) { + if (!wrappedMap.containsKey(key)) { final String msg = String.format("Key %s is not present in the map", key); throw new IllegalArgumentException(msg); @@ -117,15 +119,18 @@ public class FunctionalMap<KeyType, ValueType> implements IMap<KeyType, ValueTyp } @Override - public <MappedValue> IMap<KeyType, MappedValue> transform(final 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(final KeyType key, final ValueType val) { - if(key == null) throw new NullPointerException("Key must not be null"); + if (key == null) + throw new NullPointerException("Key must not be null"); return wrappedMap.put(key, val); } @@ -161,15 +166,20 @@ public class FunctionalMap<KeyType, ValueType> implements IMap<KeyType, ValueTyp @Override public boolean equals(final Object obj) { - if(this == obj) return true; - if(obj == null) return false; - if(!(obj instanceof FunctionalMap)) return false; + if (this == obj) + return true; + if (obj == null) + return false; + if (!(obj instanceof FunctionalMap)) + return false; final FunctionalMap<?, ?> other = (FunctionalMap<?, ?>) obj; - if(wrappedMap == null) { - if(other.wrappedMap != null) return false; - } else if(!wrappedMap.equals(other.wrappedMap)) return false; + if (wrappedMap == null) { + if (other.wrappedMap != null) + return false; + } else if (!wrappedMap.equals(other.wrappedMap)) + return false; return true; } } diff --git a/src/main/java/bjc/funcdata/FunctionalStringTokenizer.java b/src/main/java/bjc/funcdata/FunctionalStringTokenizer.java index 7b7c2f2..856c153 100644 --- a/src/main/java/bjc/funcdata/FunctionalStringTokenizer.java +++ b/src/main/java/bjc/funcdata/FunctionalStringTokenizer.java @@ -14,12 +14,13 @@ public class FunctionalStringTokenizer { * Create a new tokenizer from the specified string. * * @param strang - * The string to create a tokenizer from. + * The string to create a tokenizer from. * * @return A new tokenizer that splits the provided string on spaces. */ public static FunctionalStringTokenizer fromString(final String strang) { - if(strang == null) throw new NullPointerException("String to tokenize must be non-null"); + if (strang == null) + throw new NullPointerException("String to tokenize must be non-null"); return new FunctionalStringTokenizer(new StringTokenizer(strang, " ")); } @@ -31,10 +32,11 @@ public class FunctionalStringTokenizer { * Create a functional string tokenizer from a given string. * * @param inp - * The string to tokenize. + * The string to tokenize. */ public FunctionalStringTokenizer(final String inp) { - if(inp == null) throw new NullPointerException("String to tokenize must be non-null"); + if (inp == null) + throw new NullPointerException("String to tokenize must be non-null"); this.input = new StringTokenizer(inp); } @@ -44,15 +46,15 @@ public class FunctionalStringTokenizer { * separators. * * @param input - * The string to tokenize. + * The string to tokenize. * * @param seperators - * The set of separating tokens to use for splitting. + * The set of separating tokens to use for splitting. */ public FunctionalStringTokenizer(final String input, final String seperators) { - if(input == null) { + if (input == null) { throw new NullPointerException("String to tokenize must not be null"); - } else if(seperators == null) { + } else if (seperators == null) { throw new NullPointerException("Tokens to split on must not be null"); } @@ -63,10 +65,11 @@ public class FunctionalStringTokenizer { * Create a functional string tokenizer from a non-functional one. * * @param toWrap - * The non-functional string tokenizer to wrap. + * The non-functional string tokenizer to wrap. */ public FunctionalStringTokenizer(final StringTokenizer toWrap) { - if(toWrap == null) throw new NullPointerException("Wrapped tokenizer must not be null"); + if (toWrap == null) + throw new NullPointerException("Wrapped tokenizer must not be null"); this.input = toWrap; } @@ -75,12 +78,13 @@ public class FunctionalStringTokenizer { * Execute a provided action for each of the remaining tokens. * * @param action - * The action to execute for each token. + * The action to execute for each token. */ public void forEachToken(final Consumer<String> action) { - if(action == null) throw new NullPointerException("Action must not be null"); + if (action == null) + throw new NullPointerException("Action must not be null"); - while(input.hasMoreTokens()) { + while (input.hasMoreTokens()) { action.accept(input.nextToken()); } } @@ -106,11 +110,11 @@ public class FunctionalStringTokenizer { /** * Return the next token from the tokenizer. * - * @return The next token from the tokenizer, or null if no more tokens - * are available. + * @return The next token from the tokenizer, or null if no more tokens are + * available. */ public String nextToken() { - if(input.hasMoreTokens()) { + if (input.hasMoreTokens()) { /* Return the next available token. */ return input.nextToken(); } @@ -129,19 +133,20 @@ public class FunctionalStringTokenizer { } /** - * Convert the contents of this tokenizer into a list. Consumes all of - * the input from this tokenizer. + * Convert the contents of this tokenizer into a list. Consumes all of the input + * from this tokenizer. * * @param <E> - * The type of the converted tokens. + * The type of the converted tokens. * * @param transformer - * The function to use to convert tokens. + * The function to use to convert tokens. * * @return A list containing all of the converted tokens. */ public <E> IList<E> toList(final Function<String, E> transformer) { - if(transformer == null) throw new NullPointerException("Transformer must not be null"); + if (transformer == null) + throw new NullPointerException("Transformer must not be null"); final IList<E> returned = new FunctionalList<>(); diff --git a/src/main/java/bjc/funcdata/IList.java b/src/main/java/bjc/funcdata/IList.java index 38356e2..0ff8e30 100644 --- a/src/main/java/bjc/funcdata/IList.java +++ b/src/main/java/bjc/funcdata/IList.java @@ -18,14 +18,14 @@ import bjc.functypes.ID; * @author ben * * @param <ContainedType> - * The type in this list + * The type in this list */ public interface IList<ContainedType> extends Iterable<ContainedType> { /** * Add an item to this list. * * @param item - * The item to add to this list. + * The item to add to this list. * * @return Whether the item was added to the list successfully.. */ @@ -35,7 +35,7 @@ public interface IList<ContainedType> extends Iterable<ContainedType> { * Add all of the elements in the provided list to this list. * * @param items - * The list of items to add. + * The list of items to add. * * @return True if every item was successfully added to the list, false * otherwise. @@ -48,7 +48,7 @@ public interface IList<ContainedType> extends Iterable<ContainedType> { * Add all of the elements in the provided array to this list. * * @param items - * The array of items to add. + * The array of items to add. * * @return True if every item was successfully added to the list, false * otherwise. @@ -67,8 +67,7 @@ public interface IList<ContainedType> extends Iterable<ContainedType> { } /** - * Check if all of the elements of this list match the specified - * predicate. + * Check if all of the elements of this list match the specified predicate. * * @param matcher * The predicate to use for checking. @@ -84,8 +83,7 @@ public interface IList<ContainedType> extends Iterable<ContainedType> { * @param matcher * The predicate to use for checking. * - * @return Whether any element in the list matches the provided - * predicate. + * @return Whether any element in the list matches the provided predicate. */ boolean anyMatch(Predicate<ContainedType> matcher); @@ -93,18 +91,18 @@ public interface IList<ContainedType> extends Iterable<ContainedType> { * Reduce the contents of this list using a collector. * * @param <StateType> - * The intermediate accumulation type. + * The intermediate accumulation type. * * @param <ReducedType> - * The final, reduced type. + * The final, reduced type. * * @param collector - * The collector to use for reduction. + * The collector to use for reduction. * * @return The reduced list. */ - default <StateType, ReducedType> ReducedType collect( - final Collector<ContainedType, StateType, ReducedType> collector) { + default <StateType, ReducedType> ReducedType + collect(final Collector<ContainedType, StateType, ReducedType> collector) { final BiConsumer<StateType, ContainedType> accumulator = collector.accumulator(); final StateType initial = collector.supplier().get(); @@ -116,26 +114,25 @@ public interface IList<ContainedType> extends Iterable<ContainedType> { } /** - * Combine this list with another one into a new list and merge the - * results. + * Combine this list with another one into a new list and merge the results. * - * Works sort of like a combined zip/map over resulting pairs. Does not - * change the underlying list. + * Works sort of like a combined zip/map over resulting pairs. Does not change + * the underlying list. * - * NOTE: The returned list will have the length of the shorter of this - * list and the combined one. + * NOTE: The returned list will have the length of the shorter of this list and + * the combined one. * * @param <OtherType> - * The type of the second list. + * The type of the second list. * * @param <CombinedType> - * The type of the combined list. + * The type of the combined list. * * @param list - * The list to combine with. + * The list to combine with. * * @param combiner - * The function to use for combining element pairs. + * The function to use for combining element pairs. * * @return A new list containing the merged pairs of lists. */ @@ -146,7 +143,7 @@ public interface IList<ContainedType> extends Iterable<ContainedType> { * Check if the list contains the specified item. * * @param item - * The item to see if it is contained. + * The item to see if it is contained. * * @return Whether or not the specified item is in the list. */ @@ -168,40 +165,40 @@ public interface IList<ContainedType> extends Iterable<ContainedType> { /** * Remove and return the first element from the list. - * + * * @return The first element from the list. */ ContainedType popFirst(); /** * Remove and return the last element from the list. - * + * * @return The last element from the list. */ ContainedType popLast(); /** - * Apply a function to each member of the list, then flatten the - * results. + * Apply a function to each member of the list, then flatten the results. * * Does not change the underlying list. * * @param <MappedType> - * The type of the flattened list. + * The type of the flattened list. * * @param expander - * The function to apply to each member of the list. + * The function to apply to each member of the list. * - * @return A new list containing the flattened results of applying the - * provided function. + * @return A new list containing the flattened results of applying the provided + * function. */ - <MappedType> IList<MappedType> flatMap(Function<ContainedType, IList<MappedType>> expander); + <MappedType> IList<MappedType> + flatMap(Function<ContainedType, IList<MappedType>> expander); /** * Apply a given action for each member of the list. * * @param action - * The action to apply to each member of the list. + * The action to apply to each member of the list. */ @Override void forEach(Consumer<? super ContainedType> action); @@ -210,8 +207,8 @@ public interface IList<ContainedType> extends Iterable<ContainedType> { * Apply a given function to each element in the list and its index. * * @param action - * The function to apply to each element in the list and - * its index. + * The function to apply to each element in the list and its + * index. */ void forEachIndexed(BiConsumer<Integer, ContainedType> action); @@ -219,7 +216,7 @@ public interface IList<ContainedType> extends Iterable<ContainedType> { * Retrieve a value in the list by its index. * * @param index - * The index to retrieve a value from. + * The index to retrieve a value from. * * @return The value at the specified index in the list. */ @@ -229,7 +226,7 @@ public interface IList<ContainedType> extends Iterable<ContainedType> { * Retrieve a list containing all elements matching a predicate. * * @param predicate - * The predicate to match by. + * The predicate to match by. * * @return A list containing all elements that match the predicate. */ @@ -250,16 +247,15 @@ public interface IList<ContainedType> extends Iterable<ContainedType> { boolean isEmpty(); /** - * Create a new list by applying the given function to each element in - * the list. + * Create a new list by applying the given function to each element in the list. * * Does not change the underlying list. * * @param <MappedType> - * The type of the transformed list. + * The type of the transformed list. * * @param transformer - * The function to apply to each element in the list. + * The function to apply to each element in the list. * * @return A new list containing the mapped elements of this list. */ @@ -269,13 +265,12 @@ public interface IList<ContainedType> extends Iterable<ContainedType> { * Zip two lists into a list of pairs. * * @param <OtherType> - * The type of the second list. + * The type of the second list. * * @param list - * The list to use as the left side of the pair. + * The list to use as the left side of the pair. * - * @return A list containing pairs of this element and the specified - * list. + * @return A list containing pairs of this element and the specified list. */ <OtherType> IList<IPair<ContainedType, OtherType>> pairWith(IList<OtherType> list); @@ -283,12 +278,12 @@ public interface IList<ContainedType> extends Iterable<ContainedType> { * Partition this list into a list of sublists. * * @param partitionSize - * The size of elements to put into each one of the - * sublists. - * - * @return A list partitioned into partitions of size partitionSize. The - * last partition may not be completely full if the size of the - * list is not a multiple of partitionSize. + * The size of elements to put into each one of the + * sublists. + * + * @return A list partitioned into partitions of size partitionSize. The last + * partition may not be completely full if the size of the list is not a + * multiple of partitionSize. */ IList<IList<ContainedType>> partition(int partitionSize); @@ -296,7 +291,7 @@ public interface IList<ContainedType> extends Iterable<ContainedType> { * Prepend an item to the list. * * @param item - * The item to prepend to the list. + * The item to prepend to the list. */ void prepend(ContainedType item); @@ -304,7 +299,7 @@ public interface IList<ContainedType> extends Iterable<ContainedType> { * Prepend an array of items to the list. * * @param items - * The items to prepend to the list. + * The items to prepend to the list. */ @SuppressWarnings("unchecked") default void prependAll(final ContainedType... items) { @@ -314,8 +309,7 @@ public interface IList<ContainedType> extends Iterable<ContainedType> { } /** - * Select a random item from the list, using a default random number - * generator. + * Select a random item from the list, using a default random number generator. * * @return A random item from the list */ @@ -328,7 +322,7 @@ public interface IList<ContainedType> extends Iterable<ContainedType> { * generator. * * @param rnd - * The random number generator to use. + * The random number generator to use. * * @return A random element from this list. */ @@ -338,24 +332,24 @@ public interface IList<ContainedType> extends Iterable<ContainedType> { * Reduce this list to a single value, using a accumulative approach. * * @param <StateType> - * The in-between type of the values + * The in-between type of the values * * @param <ReducedType> - * The final value type + * The final value type * * @param initial - * The initial value of the accumulative state. + * The initial value of the accumulative state. * * @param accumulator - * The function to use to combine a list element with the - * accumulative state. + * The function to use to combine a list element with the + * accumulative state. * * @param transformer - * The function to use to convert the accumulative state - * into a final result. + * The function to use to convert the accumulative state + * into a final result. * - * @return A single value condensed from this list and transformed into - * its final state. + * @return A single value condensed from this list and transformed into its + * final state. */ <StateType, ReducedType> ReducedType reduceAux(StateType initial, BiFunction<ContainedType, StateType, StateType> accumulator, @@ -365,15 +359,15 @@ public interface IList<ContainedType> extends Iterable<ContainedType> { * Reduce this list to a single value, using a accumulative approach. * * @param <StateType> - * The in-between type of the values. + * The in-between type of the values. * * @param initial - * The initial value of the accumulative state. - * + * The initial value of the accumulative state. + * * @param accumulator - * The function to use to combine a list element with the - * accumulative state. - * + * The function to use to combine a list element with the + * accumulative state. + * * @return A single value condensed from this list. */ default <StateType> StateType reduceAux(StateType initial, @@ -385,7 +379,7 @@ public interface IList<ContainedType> extends Iterable<ContainedType> { * Remove all elements that match a given predicate. * * @param predicate - * The predicate to use to determine elements to delete. + * The predicate to use to determine elements to delete. * * @return Whether there was anything that satisfied the predicate. */ @@ -403,32 +397,30 @@ public interface IList<ContainedType> extends Iterable<ContainedType> { void reverse(); /** - * Perform a binary search for the specified key using the provided - * means of comparing elements. + * Perform a binary search for the specified key using the provided means of + * comparing elements. * - * Since this IS a binary search, the list must have been sorted before - * hand. + * Since this IS a binary search, the list must have been sorted before hand. * * @param key - * The key to search for. + * The key to search for. * * @param comparator - * The way to compare elements for searching. Pass null - * to use the natural ordering for E. + * The way to compare elements for searching. Pass null to use + * the natural ordering for E. * * @return The element if it is in this list, or null if it is not. */ ContainedType search(ContainedType key, Comparator<ContainedType> comparator); /** - * Sort the elements of this list using the provided way of comparing - * elements. + * Sort the elements of this list using the provided way of comparing elements. * * Does change the underlying list. * * @param comparator - * The way to compare elements for sorting. Pass null to - * use E's natural ordering + * The way to compare elements for sorting. Pass null to use + * E's natural ordering */ void sort(Comparator<ContainedType> comparator); @@ -443,7 +435,7 @@ public interface IList<ContainedType> extends Iterable<ContainedType> { * Convert this list into an array. * * @param type - * The type of array to return. + * The type of array to return. * * @return The list, as an array. */ diff --git a/src/main/java/bjc/funcdata/IMap.java b/src/main/java/bjc/funcdata/IMap.java index ba56578..dc5ee00 100644 --- a/src/main/java/bjc/funcdata/IMap.java +++ b/src/main/java/bjc/funcdata/IMap.java @@ -10,17 +10,17 @@ import java.util.function.Function; * @author ben * * @param <KeyType> - * The type of this map's keys. + * The type of this map's keys. * * @param <ValueType> - * The type of this map's values. + * The type of this map's values. */ public interface IMap<KeyType, ValueType> { /** * Execute an action for each entry in the map. * * @param action - * The action to execute for each entry in the map. + * The action to execute for each entry in the map. */ void forEach(BiConsumer<KeyType, ValueType> action); @@ -28,7 +28,7 @@ public interface IMap<KeyType, ValueType> { * Perform an action for each key in the map. * * @param action - * The action to perform on each key in the map. + * The action to perform on each key in the map. */ default void forEachKey(final Consumer<KeyType> action) { forEach((key, val) -> action.accept(key)); @@ -38,7 +38,7 @@ public interface IMap<KeyType, ValueType> { * Perform an action for each value in the map. * * @param action - * The action to perform on each value in the map. + * The action to perform on each value in the map. */ default void forEachValue(final Consumer<ValueType> action) { forEach((key, val) -> action.accept(val)); @@ -48,7 +48,7 @@ public interface IMap<KeyType, ValueType> { * Check if this map contains the specified key. * * @param key - * The key to check. + * The key to check. * * @return Whether or not the map contains the key. */ @@ -58,32 +58,31 @@ public interface IMap<KeyType, ValueType> { * Get the value assigned to the given key. * * @param key - * The key to look for a value under. + * The key to look for a value under. * * @return The value of the key. */ ValueType get(KeyType key); /** - * Get a value from the map, and return a default value if the key - * doesn't exist. + * Get a value from the map, and return a default value if the key doesn't + * exist. * * @param key - * The key to attempt to retrieve. + * The key to attempt to retrieve. * * @param defaultValue - * The value to return if the key doesn't exist. + * 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. + * @return The value associated with the key, or the default value if the key + * doesn't exist. */ default ValueType getOrDefault(final KeyType key, final ValueType defaultValue) { try { return get(key); - } catch(final IllegalArgumentException iaex) { + } catch (final IllegalArgumentException iaex) { /* - * We don't care about this, because it indicates a key - * is missing. + * We don't care about this, because it indicates a key is missing. */ return defaultValue; } @@ -93,17 +92,18 @@ public interface IMap<KeyType, ValueType> { * Add an entry to the map. * * @param key - * The key to put the value under. + * The key to put the value under. * * @param val - * The value to add. + * 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. + * @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. * * @throws UnsupportedOperationException - * If the map implementation doesn't support modifying the map. + * If the map implementation doesn't + * support modifying the map. */ ValueType put(KeyType key, ValueType val); @@ -122,22 +122,22 @@ public interface IMap<KeyType, ValueType> { } /* - * @NOTE Do we want this to be the semantics for transform, or do we - * want to go to semantics using something like Isomorphism, or doing a - * one-time bulk conversion of the values? + * @NOTE Do we want this to be the semantics for transform, or do we want to go + * to semantics using something like Isomorphism, or doing a one-time bulk + * conversion of the values? */ /** * Transform the values returned by this map. * - * NOTE: This transform is applied once for each lookup of a value, so - * the transform passed should be a proper function, or things will - * likely not work as expected. + * NOTE: This transform is applied once for each lookup of a value, so the + * transform passed should be a proper function, or things will likely not work + * as expected. * * @param <V2> - * The new type of returned values. + * The new type of returned values. * * @param transformer - * The function to use to transform values. + * The function to use to transform values. * * @return The map where each value will be transformed after lookup. */ @@ -146,8 +146,8 @@ public interface IMap<KeyType, ValueType> { } /** - * Extends this map, creating a new map that will delegate queries to - * the map, but store any added values itself. + * Extends this map, creating a new map that will delegate queries to the map, + * but store any added values itself. * * @return An extended map. */ @@ -157,12 +157,12 @@ public interface IMap<KeyType, ValueType> { * Remove the value bound to the key. * * @param key - * The key to remove from the map. + * 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 - * put a null value for that key into 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 put a null value for + * that key into the map. */ ValueType remove(KeyType key); @@ -181,7 +181,7 @@ public interface IMap<KeyType, ValueType> { default IList<ValueType> valueList() { final IList<ValueType> returns = new FunctionalList<>(); - for(final KeyType key : keyList()) { + for (final KeyType key : keyList()) { returns.add(get(key)); } diff --git a/src/main/java/bjc/funcdata/SentryList.java b/src/main/java/bjc/funcdata/SentryList.java index 8a56675..b0554f5 100644 --- a/src/main/java/bjc/funcdata/SentryList.java +++ b/src/main/java/bjc/funcdata/SentryList.java @@ -8,7 +8,7 @@ import java.util.List; * @author bjculkin * * @param <T> - * The type of item in the list. + * The type of item in the list. */ public class SentryList<T> extends FunctionalList<T> { /** Create a new sentry list. */ @@ -20,7 +20,7 @@ public class SentryList<T> extends FunctionalList<T> { * Create a new sentry list backed by an existing list. * * @param backing - * The backing list. + * The backing list. */ public SentryList(final List<T> backing) { super(backing); @@ -30,7 +30,7 @@ public class SentryList<T> extends FunctionalList<T> { 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/src/main/java/bjc/funcdata/TransformedValueMap.java b/src/main/java/bjc/funcdata/TransformedValueMap.java index 0f0b3b5..5de6fc3 100644 --- a/src/main/java/bjc/funcdata/TransformedValueMap.java +++ b/src/main/java/bjc/funcdata/TransformedValueMap.java @@ -10,16 +10,17 @@ import java.util.function.Function; * @author ben * * @param <OldKey> - * The type of the map's keys + * The type of the map's keys * * @param <OldValue> - * The type of the map's values + * The type of the map's values * * @param <NewValue> - * The type of the transformed values + * The type of the transformed values * */ -final class TransformedValueMap<OldKey, OldValue, NewValue> implements IMap<OldKey, NewValue> { +final class TransformedValueMap<OldKey, OldValue, NewValue> + implements IMap<OldKey, NewValue> { /* Our backing map. */ private final IMap<OldKey, OldValue> backing; /* Our transforming function. */ @@ -29,10 +30,10 @@ final class TransformedValueMap<OldKey, OldValue, NewValue> implements IMap<OldK * Create a new transformed-value loop. * * @param backingMap - * The map to use as backing. + * The map to use as backing. * * @param transform - * The function to use for the transform. + * The function to use for the transform. */ public TransformedValueMap(final IMap<OldKey, OldValue> backingMap, final Function<OldValue, NewValue> transform) { @@ -90,7 +91,8 @@ final class TransformedValueMap<OldKey, OldValue, NewValue> implements IMap<OldK } @Override - public <MappedValue> IMap<OldKey, MappedValue> transform(final Function<NewValue, MappedValue> transform) { + public <MappedValue> IMap<OldKey, MappedValue> + transform(final Function<NewValue, MappedValue> transform) { return new TransformedValueMap<>(this, transform); } diff --git a/src/main/java/bjc/funcdata/bst/BinarySearchTree.java b/src/main/java/bjc/funcdata/bst/BinarySearchTree.java index d0319e4..e22a8da 100644 --- a/src/main/java/bjc/funcdata/bst/BinarySearchTree.java +++ b/src/main/java/bjc/funcdata/bst/BinarySearchTree.java @@ -14,7 +14,7 @@ import bjc.funcdata.IList; * @author ben * * @param <T> - * The data type stored in the node. + * The data type stored in the node. */ public class BinarySearchTree<T> { /* The comparator for use in ordering items */ @@ -30,10 +30,11 @@ public class BinarySearchTree<T> { * Create a new tree using the specified way to compare elements. * * @param cmp - * The thing to use for comparing elements + * The thing to use for comparing elements */ public BinarySearchTree(final Comparator<T> cmp) { - if(cmp == null) throw new NullPointerException("Comparator must not be null"); + if (cmp == null) + throw new NullPointerException("Comparator must not be null"); elementCount = 0; comparator = cmp; @@ -43,12 +44,12 @@ public class BinarySearchTree<T> { * Add a node to the binary search tree. * * @param element - * The data to add to the binary search tree. + * The data to add to the binary search tree. */ public void addNode(final T element) { elementCount++; - if(root == null) { + if (root == null) { root = new BinarySearchTreeNode<>(element, null, null); } else { root.add(element, comparator); @@ -59,18 +60,20 @@ public class BinarySearchTree<T> { * Check if an adjusted pivot falls with the bounds of a list. * * @param elements - * The list to get bounds from. + * The list to get bounds from. * * @param pivot - * The pivot. + * The pivot. * * @param pivotAdjustment - * The distance from the pivot. + * The distance from the pivot. * * @return Whether the adjusted pivot is with the list. */ - private boolean adjustedPivotInBounds(final IList<T> elements, final int pivot, final int pivotAdjustment) { - return ((pivot - pivotAdjustment) >= 0) && ((pivot + pivotAdjustment) < elements.getSize()); + private boolean adjustedPivotInBounds(final IList<T> elements, final int pivot, + final int pivotAdjustment) { + return ((pivot - pivotAdjustment) >= 0) + && ((pivot + pivotAdjustment) < elements.getSize()); } /** @@ -92,14 +95,13 @@ public class BinarySearchTree<T> { int pivotAdjustment = 0; /* Add elements until there aren't any left. */ - while(adjustedPivotInBounds(elements, pivot, pivotAdjustment)) { - if(root == null) { + while (adjustedPivotInBounds(elements, pivot, pivotAdjustment)) { + if (root == null) { /* Create a new root element. */ root = new BinarySearchTreeNode<>(elements.getByIndex(pivot), null, null); } else { /* - * Add the left and right elements in a balanced - * manner. + * Add the left and right elements in a balanced manner. */ root.add(elements.getByIndex(pivot + pivotAdjustment), comparator); root.add(elements.getByIndex(pivot - pivotAdjustment), comparator); @@ -110,9 +112,9 @@ public class BinarySearchTree<T> { } /* Add any trailing unbalanced elements. */ - if(pivot - pivotAdjustment >= 0) { + if (pivot - pivotAdjustment >= 0) { root.add(elements.getByIndex(pivot - pivotAdjustment), comparator); - } else if(pivot + pivotAdjustment < elements.getSize()) { + } else if (pivot + pivotAdjustment < elements.getSize()) { root.add(elements.getByIndex(pivot + pivotAdjustment), comparator); } } @@ -120,11 +122,11 @@ public class BinarySearchTree<T> { /** * Soft-delete a node from the tree. * - * Soft-deleted nodes stay in the tree until trim()/balance() is - * invoked, and are not included in traversals/finds. + * Soft-deleted nodes stay in the tree until trim()/balance() is invoked, and + * are not included in traversals/finds. * * @param element - * The node to delete + * The node to delete */ public void deleteNode(final T element) { elementCount--; @@ -145,7 +147,7 @@ public class BinarySearchTree<T> { * Check if a node is in the tree. * * @param element - * The node to check the presence of for the tree.. + * The node to check the presence of for the tree.. * * @return Whether or not the node is in the tree. */ @@ -157,15 +159,16 @@ public class BinarySearchTree<T> { * Traverse the tree in a specified way until the function fails. * * @param linearizationMethod - * The way to linearize the tree for traversal. + * The way to linearize the tree for traversal. * * @param traversalPredicate - * The function to use until it fails. + * The function to use until it fails. */ - public void traverse(final TreeLinearizationMethod linearizationMethod, final Predicate<T> traversalPredicate) { - if(linearizationMethod == null) { + 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) { + } else if (traversalPredicate == null) { throw new NullPointerException("Predicate must not be nulls"); } @@ -177,8 +180,7 @@ public class BinarySearchTree<T> { final List<T> nodes = new ArrayList<>(elementCount); /* - * Add all non-soft deleted nodes to the tree in insertion - * order. + * Add all non-soft deleted nodes to the tree in insertion order. */ traverse(TreeLinearizationMethod.PREORDER, node -> { nodes.add(node); @@ -194,7 +196,8 @@ public class BinarySearchTree<T> { @Override public String toString() { - return String.format("BinarySearchTree [elementCount=%s, root='%s']", elementCount, root); + return String.format("BinarySearchTree [elementCount=%s, root='%s']", + elementCount, root); } @Override @@ -208,16 +211,22 @@ public class BinarySearchTree<T> { @Override public boolean equals(final Object obj) { - if(this == obj) return true; - if(obj == null) return false; - if(!(obj instanceof BinarySearchTree<?>)) return false; + if (this == obj) + return true; + if (obj == null) + return false; + if (!(obj instanceof BinarySearchTree<?>)) + return false; final BinarySearchTree<?> other = (BinarySearchTree<?>) obj; - if(elementCount != other.elementCount) return false; - if(root == null) { - if(other.root != null) return false; - } else if(!root.equals(other.root)) 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; return true; } diff --git a/src/main/java/bjc/funcdata/bst/BinarySearchTreeLeaf.java b/src/main/java/bjc/funcdata/bst/BinarySearchTreeLeaf.java index dfad3d9..0b99cad 100644 --- a/src/main/java/bjc/funcdata/bst/BinarySearchTreeLeaf.java +++ b/src/main/java/bjc/funcdata/bst/BinarySearchTreeLeaf.java @@ -11,7 +11,7 @@ import java.util.function.Predicate; * @author ben * * @param <T> - * The data stored in the tree. + * The data stored in the tree. */ public class BinarySearchTreeLeaf<T> implements ITreePart<T> { /** The data held in this tree leaf */ @@ -24,7 +24,7 @@ public class BinarySearchTreeLeaf<T> implements ITreePart<T> { * Create a new leaf holding the specified data. * * @param element - * The data for the leaf to hold. + * The data for the leaf to hold. */ public BinarySearchTreeLeaf(final T element) { data = element; @@ -36,8 +36,10 @@ public class BinarySearchTreeLeaf<T> implements ITreePart<T> { } @Override - 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"); + 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); } @@ -54,16 +56,17 @@ public class BinarySearchTreeLeaf<T> implements ITreePart<T> { @Override public void delete(final T element, final Comparator<T> comparator) { - if(data.equals(element)) { + if (data.equals(element)) { isDeleted = true; } } @Override public boolean directedWalk(final DirectedWalkFunction<T> treeWalker) { - if(treeWalker == null) throw new NullPointerException("Tree walker must not be null"); + if (treeWalker == null) + throw new NullPointerException("Tree walker must not be null"); - switch(treeWalker.walk(data)) { + switch (treeWalker.walk(data)) { case SUCCESS: return true; /* We don't have any children to care about. */ @@ -78,14 +81,16 @@ public class BinarySearchTreeLeaf<T> implements ITreePart<T> { @Override public boolean forEach(final TreeLinearizationMethod linearizationMethod, final Predicate<T> traversalPredicate) { - if(traversalPredicate == null) throw new NullPointerException("Predicate must not be null"); + if (traversalPredicate == null) + throw new NullPointerException("Predicate must not be null"); return traversalPredicate.test(data); } @Override public String toString() { - return String.format("BinarySearchTreeLeaf [data='%s', isDeleted=%s]", data, isDeleted); + return String.format("BinarySearchTreeLeaf [data='%s', isDeleted=%s]", data, + isDeleted); } @Override @@ -99,16 +104,22 @@ public class BinarySearchTreeLeaf<T> implements ITreePart<T> { @Override public boolean equals(final Object obj) { - if(this == obj) return true; - if(obj == null) return false; - if(!(obj instanceof BinarySearchTreeLeaf<?>)) return false; + if (this == obj) + return true; + if (obj == null) + return false; + if (!(obj instanceof BinarySearchTreeLeaf<?>)) + return false; 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 (data == null) { + 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/src/main/java/bjc/funcdata/bst/BinarySearchTreeNode.java b/src/main/java/bjc/funcdata/bst/BinarySearchTreeNode.java index 0453f80..a73f81a 100644 --- a/src/main/java/bjc/funcdata/bst/BinarySearchTreeNode.java +++ b/src/main/java/bjc/funcdata/bst/BinarySearchTreeNode.java @@ -16,7 +16,7 @@ import java.util.function.Predicate; * @author ben * * @param <T> - * The data type stored in the tree. + * The data type stored in the tree. */ public class BinarySearchTreeNode<T> extends BinarySearchTreeLeaf<T> { /* The left child of this node */ @@ -29,15 +29,16 @@ public class BinarySearchTreeNode<T> extends BinarySearchTreeLeaf<T> { * Create a new node with the specified data and children. * * @param element - * The data to store in this node. + * The data to store in this node. * * @param lft - * The left child of this node. + * The left child of this node. * * @param rght - * The right child of this node. + * The right child of this node. */ - public BinarySearchTreeNode(final T element, final ITreePart<T> lft, final ITreePart<T> rght) { + public BinarySearchTreeNode(final T element, final ITreePart<T> lft, + final ITreePart<T> rght) { super(element); this.left = lft; this.right = rght; @@ -45,24 +46,25 @@ public class BinarySearchTreeNode<T> extends BinarySearchTreeLeaf<T> { @Override public void add(final T element, final Comparator<T> comparator) { - if(comparator == null) throw new NullPointerException("Comparator must not be null"); + if (comparator == null) + throw new NullPointerException("Comparator must not be null"); - switch(comparator.compare(data, element)) { + switch (comparator.compare(data, element)) { case -1: - if(left == null) { + if (left == null) { left = new BinarySearchTreeNode<>(element, null, null); } else { left.add(element, comparator); } break; case 0: - if(isDeleted) { + if (isDeleted) { isDeleted = false; } else throw new IllegalArgumentException("Can't add duplicate values"); break; case 1: - if(right == null) { + if (right == null) { right = new BinarySearchTreeNode<>(element, null, null); } else { right.add(element, comparator); @@ -74,17 +76,19 @@ public class BinarySearchTreeNode<T> extends BinarySearchTreeLeaf<T> { } @Override - public <E> E collapse(final Function<T, E> nodeCollapser, final BiFunction<E, E, E> branchCollapser) { - if(nodeCollapser == null || branchCollapser == null) + 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"); final E collapsedNode = nodeCollapser.apply(data); - if(left != null) { + if (left != null) { final E collapsedLeftBranch = left.collapse(nodeCollapser, branchCollapser); - if(right != null) { - final E collapsedRightBranch = right.collapse(nodeCollapser, branchCollapser); + if (right != null) { + final E collapsedRightBranch + = right.collapse(nodeCollapser, branchCollapser); final E collapsedBranches = branchCollapser.apply(collapsedLeftBranch, collapsedRightBranch); @@ -95,7 +99,7 @@ public class BinarySearchTreeNode<T> extends BinarySearchTreeLeaf<T> { return branchCollapser.apply(collapsedNode, collapsedLeftBranch); } - if(right != null) { + if (right != null) { final E collapsedRightBranch = right.collapse(nodeCollapser, branchCollapser); return branchCollapser.apply(collapsedNode, collapsedRightBranch); @@ -106,10 +110,11 @@ public class BinarySearchTreeNode<T> extends BinarySearchTreeLeaf<T> { @Override public boolean contains(final T element, final Comparator<T> comparator) { - if(comparator == null) throw new NullPointerException("Comparator must not be null"); + if (comparator == null) + throw new NullPointerException("Comparator must not be null"); return directedWalk(currentElement -> { - switch(comparator.compare(element, currentElement)) { + switch (comparator.compare(element, currentElement)) { case -1: return LEFT; case 0: @@ -124,10 +129,11 @@ public class BinarySearchTreeNode<T> extends BinarySearchTreeLeaf<T> { @Override public void delete(final T element, final Comparator<T> comparator) { - if(comparator == null) throw new NullPointerException("Comparator must not be null"); + if (comparator == null) + throw new NullPointerException("Comparator must not be null"); directedWalk(currentElement -> { - switch(comparator.compare(data, element)) { + switch (comparator.compare(data, element)) { case -1: return left == null ? FAILURE : LEFT; case 0: @@ -143,9 +149,10 @@ public class BinarySearchTreeNode<T> extends BinarySearchTreeLeaf<T> { @Override public boolean directedWalk(final DirectedWalkFunction<T> treeWalker) { - if(treeWalker == null) throw new NullPointerException("Walker must not be null"); + if (treeWalker == null) + throw new NullPointerException("Walker must not be null"); - switch(treeWalker.walk(data)) { + switch (treeWalker.walk(data)) { case SUCCESS: return true; case LEFT: @@ -161,13 +168,13 @@ public class BinarySearchTreeNode<T> extends BinarySearchTreeLeaf<T> { @Override public boolean forEach(final TreeLinearizationMethod linearizationMethod, final Predicate<T> traversalPredicate) { - if(linearizationMethod == null) { + if (linearizationMethod == null) { throw new NullPointerException("Linearization method must not be null"); - } else if(traversalPredicate == null) { + } else if (traversalPredicate == null) { throw new NullPointerException("Predicate must not be null"); } - switch(linearizationMethod) { + switch (linearizationMethod) { case PREORDER: return preorderTraverse(linearizationMethod, traversalPredicate); case INORDER: @@ -175,8 +182,9 @@ public class BinarySearchTreeNode<T> extends BinarySearchTreeLeaf<T> { case POSTORDER: return postorderTraverse(linearizationMethod, traversalPredicate); default: - String msg = String.format("Passed an incorrect TreeLinearizationMethod %s. WAT", - linearizationMethod); + String msg + = String.format("Passed an incorrect TreeLinearizationMethod %s. WAT", + linearizationMethod); throw new IllegalArgumentException(msg); } @@ -185,11 +193,14 @@ public class BinarySearchTreeNode<T> extends BinarySearchTreeLeaf<T> { /* Do an in-order traversal. */ private boolean inorderTraverse(final TreeLinearizationMethod linearizationMethod, final Predicate<T> traversalPredicate) { - if(!traverseLeftBranch(linearizationMethod, traversalPredicate)) return false; + 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; } @@ -197,11 +208,14 @@ public class BinarySearchTreeNode<T> extends BinarySearchTreeLeaf<T> { /* Do a post-order traversal. */ private boolean postorderTraverse(final TreeLinearizationMethod linearizationMethod, final Predicate<T> traversalPredicate) { - if(!traverseLeftBranch(linearizationMethod, traversalPredicate)) return false; + 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; @@ -210,11 +224,14 @@ public class BinarySearchTreeNode<T> extends BinarySearchTreeLeaf<T> { /* Do a pre-order traversal. */ private boolean preorderTraverse(final TreeLinearizationMethod linearizationMethod, final Predicate<T> traversalPredicate) { - if(!traverseElement(traversalPredicate)) return false; + 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; } @@ -223,7 +240,7 @@ public class BinarySearchTreeNode<T> extends BinarySearchTreeLeaf<T> { private boolean traverseElement(final Predicate<T> traversalPredicate) { boolean nodeSuccesfullyTraversed; - if(isDeleted) { + if (isDeleted) { nodeSuccesfullyTraversed = true; } else { nodeSuccesfullyTraversed = traversalPredicate.test(data); @@ -237,10 +254,11 @@ public class BinarySearchTreeNode<T> extends BinarySearchTreeLeaf<T> { final Predicate<T> traversalPredicate) { boolean leftSuccesfullyTraversed; - if(left == null) { + if (left == null) { leftSuccesfullyTraversed = true; } else { - leftSuccesfullyTraversed = left.forEach(linearizationMethod, traversalPredicate); + leftSuccesfullyTraversed + = left.forEach(linearizationMethod, traversalPredicate); } return leftSuccesfullyTraversed; @@ -251,10 +269,11 @@ public class BinarySearchTreeNode<T> extends BinarySearchTreeLeaf<T> { final Predicate<T> traversalPredicate) { boolean rightSuccesfullyTraversed; - if(right == null) { + if (right == null) { rightSuccesfullyTraversed = true; } else { - rightSuccesfullyTraversed = right.forEach(linearizationMethod, traversalPredicate); + rightSuccesfullyTraversed + = right.forEach(linearizationMethod, traversalPredicate); } return rightSuccesfullyTraversed; @@ -276,19 +295,26 @@ public class BinarySearchTreeNode<T> extends BinarySearchTreeLeaf<T> { @Override public boolean equals(final Object obj) { - if(this == obj) return true; - if(!super.equals(obj)) return false; - if(!(obj instanceof BinarySearchTreeNode<?>)) return false; + if (this == obj) + return true; + if (!super.equals(obj)) + return false; + if (!(obj instanceof BinarySearchTreeNode<?>)) + return false; final BinarySearchTreeNode<?> other = (BinarySearchTreeNode<?>) obj; - if(left == null) { - if(other.left != null) return false; - } else if(!left.equals(other.left)) return false; + if (left == null) { + 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 (right == null) { + if (other.right != null) + return false; + } else if (!right.equals(other.right)) + return false; return true; } diff --git a/src/main/java/bjc/funcdata/bst/DirectedWalkFunction.java b/src/main/java/bjc/funcdata/bst/DirectedWalkFunction.java index ac2b918..4f64339 100644 --- a/src/main/java/bjc/funcdata/bst/DirectedWalkFunction.java +++ b/src/main/java/bjc/funcdata/bst/DirectedWalkFunction.java @@ -6,7 +6,7 @@ package bjc.funcdata.bst; * @author ben * * @param <T> - * The type of element stored in the walked tree + * The type of element stored in the walked tree */ @FunctionalInterface public interface DirectedWalkFunction<T> { @@ -19,13 +19,11 @@ public interface DirectedWalkFunction<T> { /** Specifies that the function has failed. */ FAILURE, /** - * Specifies that the function wants to move left in the tree - * next. + * Specifies that the function wants to move left in the tree next. */ LEFT, /** - * Specifies that the function wants to move right in the tree - * next. + * Specifies that the function wants to move right in the tree next. */ RIGHT, /** Specifies that the function has succesfully completed */ @@ -36,7 +34,7 @@ public interface DirectedWalkFunction<T> { * Perform a directed walk on a node of a tree. * * @param element - * The data stored in the node currently being visited. + * The data stored in the node currently being visited. * * @return The way the function wants the walk to go next. */ diff --git a/src/main/java/bjc/funcdata/bst/ITreePart.java b/src/main/java/bjc/funcdata/bst/ITreePart.java index 903965f..bac640d 100644 --- a/src/main/java/bjc/funcdata/bst/ITreePart.java +++ b/src/main/java/bjc/funcdata/bst/ITreePart.java @@ -11,18 +11,18 @@ import java.util.function.Predicate; * @author ben * * @param <T> - * The data contained in this part of the tree. + * The data contained in this part of the tree. */ public interface ITreePart<T> { /** * Add a element below this tree part somewhere. * * @param element - * The element to add below this tree part + * The element to add below this tree part * * @param comparator - * The thing to use for comparing values to find where to insert - * the tree part. + * The thing to use for comparing values to find where to + * insert the tree part. */ public void add(T element, Comparator<T> comparator); @@ -32,30 +32,32 @@ public interface ITreePart<T> { * Does not change the underlying tree. * * @param <E> - * The type of the final collapsed value + * The type of the final collapsed value * * @param nodeCollapser - * The function to use to transform data into mapped form. + * The function to use to transform data into mapped + * form. * * @param branchCollapser - * The function to use to collapse data in mapped form into a - * single value. + * The function to use to collapse data in mapped form + * into a single value. * * @return A single value from collapsing the tree. */ - public <E> E collapse(Function<T, E> nodeCollapser, BiFunction<E, E, E> branchCollapser); + public <E> E collapse(Function<T, E> nodeCollapser, + BiFunction<E, E, E> branchCollapser); /** * Check if this tre part or below it contains the specified data item. * * @param element - * The data item to look for. + * The data item to look for. * * @param comparator - * The comparator to use to search for the data item. + * The comparator to use to search for the data item. * - * @return Whether or not the given item is contained in this tree part - * or its children. + * @return Whether or not the given item is contained in this tree part or its + * children. */ public boolean contains(T element, Comparator<T> comparator); @@ -70,10 +72,10 @@ public interface ITreePart<T> { * Remove the given node from this tree part and any of its children. * * @param element - * The data item to remove. + * The data item to remove. * * @param comparator - * The comparator to use to search for the data item. + * The comparator to use to search for the data item. */ public void delete(T element, Comparator<T> comparator); @@ -81,24 +83,25 @@ public interface ITreePart<T> { * Execute a directed walk through the tree. * * @param walker - * The function to use to direct the walk through the tree. + * The function to use to direct the walk through the tree. * * @return Whether the directed walk finished successfully. */ public boolean directedWalk(DirectedWalkFunction<T> walker); /** - * Execute a provided function for each element of tree it succesfully - * completes for. + * Execute a provided function for each element of tree it succesfully completes + * for. * * @param linearizationMethod - * The way to linearize the tree for executing. + * The way to linearize the tree for executing. * * @param predicate - * The predicate to apply to each element, where it returning - * false terminates traversal early. + * The predicate to apply to each element, where it + * returning false terminates traversal early. * * @return Whether the traversal finished succesfully. */ - public boolean forEach(TreeLinearizationMethod linearizationMethod, Predicate<T> predicate); + public boolean forEach(TreeLinearizationMethod linearizationMethod, + Predicate<T> predicate); } diff --git a/src/main/java/bjc/funcdata/bst/TreeLinearizationMethod.java b/src/main/java/bjc/funcdata/bst/TreeLinearizationMethod.java index 35b116b..65c013b 100644 --- a/src/main/java/bjc/funcdata/bst/TreeLinearizationMethod.java +++ b/src/main/java/bjc/funcdata/bst/TreeLinearizationMethod.java @@ -7,18 +7,18 @@ package bjc.funcdata.bst; */ public enum TreeLinearizationMethod { /** - * Visit the left side of this tree part, the tree part itself, and then - * the right part. + * Visit the left side of this tree part, the tree part itself, and then the + * right part. */ INORDER, /** - * Visit the left side of this tree part, the right side, and then the - * tree part itself. + * Visit the left side of this tree part, the right side, and then the tree part + * itself. */ POSTORDER, /** - * Visit the tree part itself, then the left side of tthis tree part and - * then the right part. + * Visit the tree part itself, then the left side of tthis tree part and then + * the right part. */ PREORDER } diff --git a/src/main/java/bjc/funcdata/theory/Bifunctor.java b/src/main/java/bjc/funcdata/theory/Bifunctor.java index b576a2b..72fccff 100644 --- a/src/main/java/bjc/funcdata/theory/Bifunctor.java +++ b/src/main/java/bjc/funcdata/theory/Bifunctor.java @@ -8,10 +8,10 @@ import java.util.function.Function; * @author ben * * @param <LeftType> - * The type stored on the 'left' of the pair. + * The type stored on the 'left' of the pair. * * @param <RightType> - * The type stored on the 'right' of the pair. + * The type stored on the 'right' of the pair. */ public interface Bifunctor<LeftType, RightType> { /** @@ -20,16 +20,16 @@ public interface Bifunctor<LeftType, RightType> { * @author EVE * * @param <OldLeft> - * The old left type. + * The old left type. * * @param <OldRight> - * The old right type. + * The old right type. * * @param <NewLeft> - * The new left type. + * The new left type. * * @param <NewRight> - * The new right type. + * The new right type. */ public interface BifunctorMap<OldLeft, OldRight, NewLeft, NewRight> extends Function<Bifunctor<OldLeft, OldRight>, Bifunctor<NewLeft, NewRight>> { @@ -44,13 +44,13 @@ public interface Bifunctor<LeftType, RightType> { * @author EVE * * @param <OldLeft> - * The old left type. + * The old left type. * * @param <OldRight> - * The old right type. + * The old right type. * * @param <NewLeft> - * The new left type. + * The new left type. */ public interface LeftBifunctorMap<OldLeft, OldRight, NewLeft> extends BifunctorMap<OldLeft, OldRight, NewLeft, OldRight> { @@ -65,13 +65,13 @@ public interface Bifunctor<LeftType, RightType> { * @author EVE * * @param <OldLeft> - * The old left type. + * The old left type. * * @param <OldRight> - * The old right type. + * The old right type. * * @param <NewRight> - * The new right type. + * The new right type. */ public interface RightBifunctorMap<OldLeft, OldRight, NewRight> extends BifunctorMap<OldLeft, OldRight, OldLeft, NewRight> { @@ -81,40 +81,45 @@ public interface Bifunctor<LeftType, RightType> { } /** - * Lift a pair of functions to a single function that maps over both - * parts of a pair. + * Lift a pair of functions to a single function that maps over both parts of a + * pair. * * @param <OldLeft> - * The old left type of the pair. + * The old left type of the pair. * * @param <OldRight> - * The old right type of the pair. + * The old right type of the pair. * * @param <NewLeft> - * The new left type of the pair. + * The new left type of the pair. * * @param <NewRight> - * The new right type of the pair. + * The new right type of the pair. * * @param leftFunc - * The function that maps over the left of the pair. + * The function that maps over the left of the pair. * * @param rightFunc - * The function that maps over the right of the pair. + * The function that maps over the right of the pair. * * @return A function that maps over both parts of the pair. */ - public default <OldLeft, OldRight, NewLeft, NewRight> BifunctorMap<OldLeft, OldRight, NewLeft, NewRight> bimap( - 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); + public default <OldLeft, OldRight, NewLeft, NewRight> + BifunctorMap<OldLeft, OldRight, NewLeft, NewRight> + bimap(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); - final Bifunctor<NewLeft, OldRight> leftMappedFunctor = leftMapper.apply(argPair); - final RightBifunctorMap<NewLeft, OldRight, NewRight> rightMapper = leftMappedFunctor - .fmapRight(rightFunc); + final Bifunctor<NewLeft, OldRight> leftMappedFunctor + = leftMapper.apply(argPair); + final RightBifunctorMap<NewLeft, OldRight, NewRight> rightMapper + = leftMappedFunctor.fmapRight(rightFunc); - return rightMapper.apply(leftMappedFunctor); - }; + return rightMapper.apply(leftMappedFunctor); + }; return bimappedFunc; } @@ -123,42 +128,43 @@ public interface Bifunctor<LeftType, RightType> { * Lift a function to operate over the left part of this pair. * * @param <OldLeft> - * The old left type of the pair. + * The old left type of the pair. * * @param <OldRight> - * The old right type of the pair. + * The old right type of the pair. * * @param <NewLeft> - * The new left type of the pair. + * The new left type of the pair. * * @param func - * The function to lift to work over the left side of the pair. + * The function to lift to work over the left side of the + * pair. * * @return The function lifted to work over the left side of bifunctors. */ - public <OldLeft, OldRight, NewLeft> LeftBifunctorMap<OldLeft, OldRight, NewLeft> fmapLeft( - Function<OldLeft, NewLeft> func); + public <OldLeft, OldRight, NewLeft> LeftBifunctorMap<OldLeft, OldRight, NewLeft> + fmapLeft(Function<OldLeft, NewLeft> func); /** * Lift a function to operate over the right part of this pair. * * @param <OldLeft> - * The old left type of the pair. + * The old left type of the pair. * * @param <OldRight> - * The old right type of the pair. + * The old right type of the pair. * * @param <NewRight> - * The new right type of the pair. + * The new right type of the pair. * * @param func - * The function to lift to work over the right side of the pair. + * The function to lift to work over the right side of the + * pair. * - * @return The function lifted to work over the right side of - * bifunctors. + * @return The function lifted to work over the right side of bifunctors. */ - public <OldLeft, OldRight, NewRight> RightBifunctorMap<OldLeft, OldRight, NewRight> fmapRight( - Function<OldRight, NewRight> func); + public <OldLeft, OldRight, NewRight> RightBifunctorMap<OldLeft, OldRight, NewRight> + fmapRight(Function<OldRight, NewRight> func); /** * Get the value contained on the left of this bifunctor. diff --git a/src/main/java/bjc/funcdata/theory/Functor.java b/src/main/java/bjc/funcdata/theory/Functor.java index 4192bf4..85a1ec7 100644 --- a/src/main/java/bjc/funcdata/theory/Functor.java +++ b/src/main/java/bjc/funcdata/theory/Functor.java @@ -9,30 +9,30 @@ import java.util.function.Function; * @author ben * * @param <ContainedType> - * The value inside the functor. + * The value inside the functor. */ public interface Functor<ContainedType> { /** * Converts a normal function to operate over values in a functor.. * - * N.B: Even though the type signature implies that you can apply the - * resulting function to any type of functor, it is only safe to call it - * on instances of the type of functor you called fmap on.. + * N.B: Even though the type signature implies that you can apply the resulting + * function to any type of functor, it is only safe to call it on instances of + * the type of functor you called fmap on.. * * @param <ArgType> - * The argument of the function. + * The argument of the function. * * @param <ReturnType> - * The return type of the function. + * The return type of the function. * * @param func - * The function to convert. + * The function to convert. * - * @return The passed in function converted to work over a particular - * type of functors. + * @return The passed in function converted to work over a particular type of + * functors. */ - public <ArgType, ReturnType> Function<Functor<ArgType>, Functor<ReturnType>> fmap( - Function<ArgType, ReturnType> func); + public <ArgType, ReturnType> Function<Functor<ArgType>, Functor<ReturnType>> + fmap(Function<ArgType, ReturnType> func); /** * Retrieve the thing inside this functor. |
