summaryrefslogtreecommitdiff
path: root/BJC-Utils2/src/main/java/bjc/utils/funcdata
diff options
context:
space:
mode:
Diffstat (limited to 'BJC-Utils2/src/main/java/bjc/utils/funcdata')
-rw-r--r--BJC-Utils2/src/main/java/bjc/utils/funcdata/FunctionalList.java313
-rw-r--r--BJC-Utils2/src/main/java/bjc/utils/funcdata/FunctionalMap.java29
-rw-r--r--BJC-Utils2/src/main/java/bjc/utils/funcdata/FunctionalStringTokenizer.java50
-rw-r--r--BJC-Utils2/src/main/java/bjc/utils/funcdata/IList.java102
-rw-r--r--BJC-Utils2/src/main/java/bjc/utils/funcdata/IMap.java4
-rw-r--r--BJC-Utils2/src/main/java/bjc/utils/funcdata/TransformedValueMap.java43
-rw-r--r--BJC-Utils2/src/main/java/bjc/utils/funcdata/bst/BinarySearchTree.java74
-rw-r--r--BJC-Utils2/src/main/java/bjc/utils/funcdata/bst/BinarySearchTreeLeaf.java50
-rw-r--r--BJC-Utils2/src/main/java/bjc/utils/funcdata/bst/BinarySearchTreeNode.java153
-rw-r--r--BJC-Utils2/src/main/java/bjc/utils/funcdata/bst/ITreePart.java10
-rw-r--r--BJC-Utils2/src/main/java/bjc/utils/funcdata/theory/Functor.java7
11 files changed, 287 insertions, 548 deletions
diff --git a/BJC-Utils2/src/main/java/bjc/utils/funcdata/FunctionalList.java b/BJC-Utils2/src/main/java/bjc/utils/funcdata/FunctionalList.java
index b902207..81c263f 100644
--- a/BJC-Utils2/src/main/java/bjc/utils/funcdata/FunctionalList.java
+++ b/BJC-Utils2/src/main/java/bjc/utils/funcdata/FunctionalList.java
@@ -19,7 +19,9 @@ import bjc.utils.data.Pair;
/**
* A wrapper over another list that provides eager functional operations
- * over it. Differs from a stream in every way except for the fact that
+ * over it.
+ *
+ * Differs from a stream in every way except for the fact that
* they both provide functional operations.
*
* @author ben
@@ -28,16 +30,16 @@ import bjc.utils.data.Pair;
* The type in this list
*/
public class FunctionalList<E> implements Cloneable, IList<E> {
- /**
+ /*
* The list used as a backing store
*/
- private List<E> wrappedList;
+ private List<E> wrapped;
/**
* Create a new empty functional list.
*/
public FunctionalList() {
- wrappedList = new ArrayList<>();
+ wrapped = new ArrayList<>();
}
/**
@@ -50,10 +52,10 @@ public class FunctionalList<E> implements Cloneable, IList<E> {
*/
@SafeVarargs
public FunctionalList(E... items) {
- wrappedList = new ArrayList<>(items.length);
+ wrapped = new ArrayList<>(items.length);
for (E item : items) {
- wrappedList.add(item);
+ wrapped.add(item);
}
}
@@ -64,7 +66,7 @@ public class FunctionalList<E> implements Cloneable, IList<E> {
* The size of the backing list .
*/
private FunctionalList(int size) {
- wrappedList = new ArrayList<>(size);
+ wrapped = new ArrayList<>(size);
}
/**
@@ -72,42 +74,31 @@ public class FunctionalList<E> implements Cloneable, IList<E> {
*
* Takes O(1) time, since it doesn't copy the list.
*
- * @param backingList
+ * @param backing
* The list to use as a backing list.
*/
- public FunctionalList(List<E> backingList) {
- if (backingList == null) {
+ public FunctionalList(List<E> backing) {
+ if (backing == null) {
throw new NullPointerException(
"Backing list must be non-null");
}
- wrappedList = backingList;
+ wrapped = backing;
}
- /*
- * (non-Javadoc)
- *
- * @see bjc.utils.funcdata.IFunctionalList#add(E)
- */
@Override
public boolean add(E item) {
- return wrappedList.add(item);
+ return wrapped.add(item);
}
- /*
- * (non-Javadoc)
- *
- * @see bjc.utils.funcdata.IFunctionalList#allMatch(java.util.function.
- * Predicate)
- */
@Override
- public boolean allMatch(Predicate<E> matchPredicate) {
- if (matchPredicate == null) {
+ public boolean allMatch(Predicate<E> predicate) {
+ if (predicate == null) {
throw new NullPointerException("Predicate must be non-null");
}
- for (E item : wrappedList) {
- if (!matchPredicate.test(item)) {
+ for (E item : wrapped) {
+ if (!predicate.test(item)) {
// We've found a non-matching item
return false;
}
@@ -117,20 +108,14 @@ public class FunctionalList<E> implements Cloneable, IList<E> {
return true;
}
- /*
- * (non-Javadoc)
- *
- * @see bjc.utils.funcdata.IFunctionalList#anyMatch(java.util.function.
- * Predicate)
- */
@Override
- public boolean anyMatch(Predicate<E> matchPredicate) {
- if (matchPredicate == null) {
+ public boolean anyMatch(Predicate<E> predicate) {
+ if (predicate == null) {
throw new NullPointerException("Predicate must be not null");
}
- for (E item : wrappedList) {
- if (matchPredicate.test(item)) {
+ for (E item : wrapped) {
+ if (predicate.test(item)) {
// We've found a matching item
return true;
}
@@ -149,127 +134,87 @@ public class FunctionalList<E> implements Cloneable, IList<E> {
*/
@Override
public IList<E> clone() {
- IList<E> clonedList = new FunctionalList<>();
+ IList<E> cloned = new FunctionalList<>();
- for (E element : wrappedList) {
- clonedList.add(element);
+ for (E element : wrapped) {
+ cloned.add(element);
}
- return clonedList;
+ return cloned;
}
- /*
- * (non-Javadoc)
- *
- * @see
- * bjc.utils.funcdata.IFunctionalList#combineWith(bjc.utils.funcdata.
- * IFunctionalList, java.util.function.BiFunction)
- */
@Override
public <T, F> IList<F> combineWith(IList<T> rightList,
BiFunction<E, T, F> itemCombiner) {
if (rightList == null) {
- throw new NullPointerException(
- "Target combine list must not be null");
+ throw new NullPointerException( "Target combine list must not be null");
} else if (itemCombiner == null) {
throw new NullPointerException("Combiner must not be null");
}
- IList<F> returnedList = new FunctionalList<>();
+ IList<F> returned = new FunctionalList<>();
// Get the iterator for the other list
Iterator<T> rightIterator = rightList.toIterable().iterator();
- for (Iterator<E> leftIterator = wrappedList.iterator();
+ for (Iterator<E> leftIterator = wrapped.iterator();
leftIterator.hasNext() && rightIterator.hasNext();) {
// Add the transformed items to the result list
E leftVal = leftIterator.next();
T rightVal = rightIterator.next();
- returnedList.add(itemCombiner.apply(leftVal, rightVal));
+ returned.add(itemCombiner.apply(leftVal, rightVal));
}
- return returnedList;
+ return returned;
}
- /*
- * (non-Javadoc)
- *
- * @see bjc.utils.funcdata.IFunctionalList#contains(E)
- */
@Override
public boolean contains(E item) {
// Check if any items in the list match the provided item
return this.anyMatch(item::equals);
}
- /*
- * (non-Javadoc)
- *
- * @see bjc.utils.funcdata.IFunctionalList#first()
- */
@Override
public E first() {
- if (wrappedList.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 wrappedList.get(0);
+ return wrapped.get(0);
}
- /*
- * (non-Javadoc)
- *
- * @see bjc.utils.funcdata.IFunctionalList#flatMap(java.util.function.
- * Function)
- */
@Override
- public <T> IList<T> flatMap(Function<E, IList<T>> elementExpander) {
- if (elementExpander == null) {
+ public <T> IList<T> flatMap(Function<E, IList<T>> expander) {
+ if (expander == null) {
throw new NullPointerException("Expander must not be null");
}
- IList<T> returnedList = new FunctionalList<>(
- this.wrappedList.size());
+ IList<T> returned = new FunctionalList<>(this.wrapped.size());
forEach(element -> {
- IList<T> expandedElement = elementExpander.apply(element);
+ IList<T> expandedElement = expander.apply(element);
if (expandedElement == null) {
- throw new NullPointerException(
- "Expander returned null list");
+ throw new NullPointerException("Expander returned null list");
}
// Add each element to the returned list
- expandedElement.forEach(returnedList::add);
+ expandedElement.forEach(returned::add);
});
- return returnedList;
+ return returned;
}
- /*
- * (non-Javadoc)
- *
- * @see bjc.utils.funcdata.IFunctionalList#forEach(java.util.function.
- * Consumer)
- */
@Override
public void forEach(Consumer<E> action) {
if (action == null) {
throw new NullPointerException("Action is null");
}
- wrappedList.forEach(action);
+ wrapped.forEach(action);
}
- /*
- * (non-Javadoc)
- *
- * @see
- * bjc.utils.funcdata.IFunctionalList#forEachIndexed(java.util.function
- * .BiConsumer)
- */
@Override
public void forEachIndexed(BiConsumer<Integer, E> indexedAction) {
if (indexedAction == null) {
@@ -279,24 +224,18 @@ public class FunctionalList<E> implements Cloneable, IList<E> {
// This is held b/c ref'd variables must be final/effectively final
IHolder<Integer> currentIndex = new Identity<>(0);
- wrappedList.forEach((element) -> {
+ wrapped.forEach((element) -> {
// Call the action with the index and the value
- indexedAction.accept(currentIndex.unwrap(index -> index),
- element);
+ indexedAction.accept(currentIndex.unwrap(index -> index), element);
// Increment the value
currentIndex.transform((index) -> index + 1);
});
}
- /*
- * (non-Javadoc)
- *
- * @see bjc.utils.funcdata.IFunctionalList#getByIndex(int)
- */
@Override
public E getByIndex(int index) {
- return wrappedList.get(index);
+ return wrapped.get(index);
}
/**
@@ -305,52 +244,35 @@ public class FunctionalList<E> implements Cloneable, IList<E> {
* @return The backing list this list is based off of.
*/
public List<E> getInternal() {
- return wrappedList;
+ return wrapped;
}
- /*
- * (non-Javadoc)
- *
- * @see
- * bjc.utils.funcdata.IFunctionalList#getMatching(java.util.function.
- * Predicate)
- */
@Override
- public IList<E> getMatching(Predicate<E> matchPredicate) {
- if (matchPredicate == null) {
+ public IList<E> getMatching(Predicate<E> predicate) {
+ if (predicate == null) {
throw new NullPointerException("Predicate must not be null");
}
- IList<E> returnedList = new FunctionalList<>();
+ IList<E> returned = new FunctionalList<>();
- wrappedList.forEach((element) -> {
- if (matchPredicate.test(element)) {
+ wrapped.forEach((element) -> {
+ if (predicate.test(element)) {
// The item matches, so add it to the returned list
- returnedList.add(element);
+ returned.add(element);
}
});
- return returnedList;
+ return returned;
}
- /*
- * (non-Javadoc)
- *
- * @see bjc.utils.funcdata.IFunctionalList#getSize()
- */
@Override
public int getSize() {
- return wrappedList.size();
+ return wrapped.size();
}
- /*
- * (non-Javadoc)
- *
- * @see bjc.utils.funcdata.IFunctionalList#isEmpty()
- */
@Override
public boolean isEmpty() {
- return wrappedList.isEmpty();
+ return wrapped.isEmpty();
}
/*
@@ -358,59 +280,40 @@ public class FunctionalList<E> implements Cloneable, IList<E> {
*/
private Boolean isPartitionFull(int numberPerPartition,
IHolder<IList<E>> currentPartition) {
- return currentPartition.unwrap(
- (partition) -> partition.getSize() >= numberPerPartition);
+ return currentPartition.unwrap((partition) -> partition.getSize() >= numberPerPartition);
}
- /*
- * (non-Javadoc)
- *
- * @see
- * bjc.utils.funcdata.IFunctionalList#map(java.util.function.Function)
- */
@Override
public <T> IList<T> map(Function<E, T> elementTransformer) {
if (elementTransformer == null) {
throw new NullPointerException("Transformer must be not null");
}
- IList<T> returnedList = new FunctionalList<>(
- this.wrappedList.size());
+ IList<T> returned = new FunctionalList<>(this.wrapped.size());
forEach(element -> {
// Add the transformed item to the result
- returnedList.add(elementTransformer.apply(element));
+ returned.add(elementTransformer.apply(element));
});
- return returnedList;
+ return returned;
}
- /*
- * (non-Javadoc)
- *
- * @see bjc.utils.funcdata.IFunctionalList#pairWith(bjc.utils.funcdata.
- * IFunctionalList)
- */
@Override
public <T> IList<IPair<E, T>> pairWith(IList<T> rightList) {
return combineWith(rightList, Pair<E, T>::new);
}
- /*
- * (non-Javadoc)
- *
- * @see bjc.utils.funcdata.IFunctionalList#partition(int)
- */
@Override
public IList<IList<E>> partition(int numberPerPartition) {
if (numberPerPartition < 1
- || numberPerPartition > wrappedList.size()) {
+ || numberPerPartition > wrapped.size()) {
throw new IllegalArgumentException("" + numberPerPartition
+ " is an invalid partition size. Must be between 1 and "
- + wrappedList.size());
+ + wrapped.size());
}
- IList<IList<E>> returnedList = new FunctionalList<>();
+ IList<IList<E>> returned = new FunctionalList<>();
// The current partition being filled
IHolder<IList<E>> currentPartition = new Identity<>(
@@ -419,55 +322,35 @@ public class FunctionalList<E> implements Cloneable, IList<E> {
this.forEach((element) -> {
if (isPartitionFull(numberPerPartition, currentPartition)) {
// Add the partition to the list
- returnedList.add(
- currentPartition.unwrap((partition) -> partition));
+ returned.add(currentPartition.unwrap((partition) -> partition));
// Start a new partition
- currentPartition
- .transform((partition) -> new FunctionalList<>());
+ currentPartition.transform((partition) -> new FunctionalList<>());
} else {
// Add the element to the current partition
- currentPartition
- .unwrap((partition) -> partition.add(element));
+ currentPartition.unwrap((partition) -> partition.add(element));
}
});
- return returnedList;
+ return returned;
}
- /*
- * (non-Javadoc)
- *
- * @see bjc.utils.funcdata.IFunctionalList#prepend(E)
- */
@Override
public void prepend(E item) {
- wrappedList.add(0, item);
+ wrapped.add(0, item);
}
- /*
- * (non-Javadoc)
- *
- * @see bjc.utils.funcdata.IFunctionalList#randItem(java.util.Random)
- */
@Override
public E randItem(Function<Integer, Integer> rnd) {
if (rnd == null) {
- throw new NullPointerException(
- "Random source must not be null");
+ throw new NullPointerException("Random source must not be null");
}
- int randomIndex = rnd.apply(wrappedList.size());
+ int randomIndex = rnd.apply(wrapped.size());
- return wrappedList.get(randomIndex);
+ return wrapped.get(randomIndex);
}
- /*
- * (non-Javadoc)
- *
- * @see bjc.utils.funcdata.IFunctionalList#reduceAux(T,
- * java.util.function.BiFunction, java.util.function.Function)
- */
@Override
public <T, F> F reduceAux(T initialValue,
BiFunction<E, T, T> stateAccumulator,
@@ -481,36 +364,24 @@ public class FunctionalList<E> implements Cloneable, IList<E> {
// The current collapsed list
IHolder<T> currentState = new Identity<>(initialValue);
- wrappedList.forEach(element -> {
+ wrapped.forEach(element -> {
// Accumulate a new value into the state
- currentState.transform(
- (state) -> stateAccumulator.apply(element, state));
+ currentState.transform((state) -> stateAccumulator.apply(element, state));
});
// Convert the state to its final value
return currentState.unwrap(resultTransformer);
}
- /*
- * (non-Javadoc)
- *
- * @see bjc.utils.funcdata.IFunctionalList#removeIf(java.util.function.
- * Predicate)
- */
@Override
public boolean removeIf(Predicate<E> removePredicate) {
if (removePredicate == null) {
throw new NullPointerException("Predicate must be non-null");
}
- return wrappedList.removeIf(removePredicate);
+ return wrapped.removeIf(removePredicate);
}
- /*
- * (non-Javadoc)
- *
- * @see bjc.utils.funcdata.IFunctionalList#removeMatching(E)
- */
@Override
public void removeMatching(E desiredElement) {
removeIf((element) -> element.equals(desiredElement));
@@ -518,66 +389,44 @@ public class FunctionalList<E> implements Cloneable, IList<E> {
@Override
public void reverse() {
- Collections.reverse(wrappedList);
+ Collections.reverse(wrapped);
}
- /*
- * (non-Javadoc)
- *
- * @see bjc.utils.funcdata.IFunctionalList#search(E,
- * java.util.Comparator)
- */
@Override
public E search(E searchKey, Comparator<E> comparator) {
// Search our internal list
- int foundIndex = Collections.binarySearch(wrappedList, searchKey,
+ int foundIndex = Collections.binarySearch(wrapped, searchKey,
comparator);
if (foundIndex >= 0) {
// We found a matching element
- return wrappedList.get(foundIndex);
+ return wrapped.get(foundIndex);
}
// We didn't find an element
return null;
}
- /*
- * (non-Javadoc)
- *
- * @see bjc.utils.funcdata.IFunctionalList#sort(java.util.Comparator)
- */
@Override
public void sort(Comparator<E> comparator) {
- Collections.sort(wrappedList, comparator);
+ Collections.sort(wrapped, comparator);
}
@Override
public IList<E> tail() {
- return new FunctionalList<>(wrappedList.subList(1, getSize()));
+ return new FunctionalList<>(wrapped.subList(1, getSize()));
}
@Override
public E[] toArray(E[] arrType) {
- return wrappedList.toArray(arrType);
+ return wrapped.toArray(arrType);
}
- /*
- * (non-Javadoc)
- *
- * @see bjc.utils.funcdata.IFunctionalList#toIterable()
- */
@Override
public Iterable<E> toIterable() {
- return wrappedList;
+ return wrapped;
}
- /*
- * Reduce this item to a form useful for looking at in the debugger.
- * (non-Javadoc)
- *
- * @see java.lang.Object#toString()
- */
@Override
public String toString() {
StringBuilder sb = new StringBuilder("(");
@@ -593,4 +442,4 @@ public class FunctionalList<E> implements Cloneable, IList<E> {
return sb.toString();
}
-} \ No newline at end of file
+}
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 dfe02dc..cc70ae1 100644
--- a/BJC-Utils2/src/main/java/bjc/utils/funcdata/FunctionalMap.java
+++ b/BJC-Utils2/src/main/java/bjc/utils/funcdata/FunctionalMap.java
@@ -18,8 +18,7 @@ import bjc.utils.data.IPair;
* @param <ValueType>
* The type of the map's values
*/
-public class FunctionalMap<KeyType, ValueType>
- implements IMap<KeyType, ValueType> {
+public class FunctionalMap<KeyType, ValueType> implements IMap<KeyType, ValueType> {
private Map<KeyType, ValueType> wrappedMap;
/**
@@ -65,11 +64,6 @@ public class FunctionalMap<KeyType, ValueType>
wrappedMap.clear();
}
- /*
- * (non-Javadoc)
- *
- * @see bjc.utils.funcdata.IFunctionalMap#containsKey(K)
- */
@Override
public boolean containsKey(KeyType key) {
return wrappedMap.containsKey(key);
@@ -95,11 +89,6 @@ public class FunctionalMap<KeyType, ValueType>
wrappedMap.values().forEach(action);
}
- /*
- * (non-Javadoc)
- *
- * @see bjc.utils.funcdata.IFunctionalMap#get(K)
- */
@Override
public ValueType get(KeyType key) {
if (key == null) {
@@ -107,8 +96,7 @@ public class FunctionalMap<KeyType, ValueType>
}
if (!wrappedMap.containsKey(key)) {
- throw new IllegalArgumentException(
- "Key " + key + " is not present in the map");
+ throw new IllegalArgumentException("Key " + key + " is not present in the map");
}
return wrappedMap.get(key);
@@ -130,12 +118,6 @@ public class FunctionalMap<KeyType, ValueType>
return keys;
}
- /*
- * (non-Javadoc)
- *
- * @see bjc.utils.funcdata.IFunctionalMap#mapValues(java.util.function.
- * Function)
- */
@Override
public <MappedValue> IMap<KeyType, MappedValue> mapValues(
Function<ValueType, MappedValue> transformer) {
@@ -146,11 +128,6 @@ public class FunctionalMap<KeyType, ValueType>
return new TransformedValueMap<>(this, transformer);
}
- /*
- * (non-Javadoc)
- *
- * @see bjc.utils.funcdata.IFunctionalMap#put(K, V)
- */
@Override
public ValueType put(KeyType key, ValueType val) {
if (key == null) {
@@ -180,4 +157,4 @@ public class FunctionalMap<KeyType, ValueType>
return values;
}
-} \ No newline at end of file
+}
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 3714bcd..078bba6 100644
--- a/BJC-Utils2/src/main/java/bjc/utils/funcdata/FunctionalStringTokenizer.java
+++ b/BJC-Utils2/src/main/java/bjc/utils/funcdata/FunctionalStringTokenizer.java
@@ -24,11 +24,10 @@ public class FunctionalStringTokenizer {
"String to tokenize must be non-null");
}
- return new FunctionalStringTokenizer(
- new StringTokenizer(strang, " "));
+ return new FunctionalStringTokenizer(new StringTokenizer(strang, " "));
}
- /**
+ /*
* The string tokenizer being driven
*/
private StringTokenizer input;
@@ -41,8 +40,7 @@ public class FunctionalStringTokenizer {
*/
public FunctionalStringTokenizer(String inp) {
if (inp == null) {
- throw new NullPointerException(
- "String to tokenize must be non-null");
+ throw new NullPointerException("String to tokenize must be non-null");
}
this.input = new StringTokenizer(inp);
@@ -50,24 +48,22 @@ public class FunctionalStringTokenizer {
/**
* Create a functional string tokenizer from a given string and set of
- * seperators
+ * separators
*
- * @param inputString
+ * @param input
* The string to tokenize
* @param seperators
* The set of separating tokens to use for splitting
*/
- public FunctionalStringTokenizer(String inputString,
+ public FunctionalStringTokenizer(String input,
String seperators) {
- if (inputString == null) {
- throw new NullPointerException(
- "String to tokenize must not be null");
+ 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");
+ throw new NullPointerException("Tokens to split on must not be null");
}
- this.input = new StringTokenizer(inputString, seperators);
+ this.input = new StringTokenizer(input, seperators);
}
/**
@@ -78,8 +74,7 @@ public class FunctionalStringTokenizer {
*/
public FunctionalStringTokenizer(StringTokenizer toWrap) {
if (toWrap == null) {
- throw new NullPointerException(
- "Wrapped tokenizer must not be null");
+ throw new NullPointerException("Wrapped tokenizer must not be null");
}
this.input = toWrap;
@@ -102,7 +97,7 @@ public class FunctionalStringTokenizer {
}
/**
- * Get the string tokenizer encapsuled by this tokenizer
+ * Get the string tokenizer encapsulated by this tokenizer
*
* @return The encapsulated tokenizer
*/
@@ -120,14 +115,15 @@ public class FunctionalStringTokenizer {
}
/**
- * Return the next token from the tokenizer. Returns null if no more
- * tokens are available
+ * Return the next token from the tokenizer.
+ *
+ * Returns null if no more tokens are available
*
* @return The next token from the tokenizer
*/
public String nextToken() {
if (input.hasMoreTokens()) {
- // Return the next availible token
+ // Return the next available token
return input.nextToken();
}
@@ -151,24 +147,24 @@ public class FunctionalStringTokenizer {
* @param <E>
* The type of the converted tokens
*
- * @param tokenTransformer
+ * @param transformer
* The function to use to convert tokens.
* @return A list containing all of the converted tokens.
*/
- public <E> IList<E> toList(Function<String, E> tokenTransformer) {
- if (tokenTransformer == null) {
+ public <E> IList<E> toList(Function<String, E> transformer) {
+ if (transformer == null) {
throw new NullPointerException("Transformer must not be null");
}
- IList<E> returnList = new FunctionalList<>();
+ IList<E> returned = new FunctionalList<>();
// Add each token to the list after transforming it
forEachToken(token -> {
- E transformedToken = tokenTransformer.apply(token);
+ E transformedToken = transformer.apply(token);
- returnList.add(transformedToken);
+ returned.add(transformedToken);
});
- return returnList;
+ return returned;
}
}
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 037b2b6..f920fce 100644
--- a/BJC-Utils2/src/main/java/bjc/utils/funcdata/IList.java
+++ b/BJC-Utils2/src/main/java/bjc/utils/funcdata/IList.java
@@ -24,7 +24,7 @@ public interface IList<ContainedType> {
*
* @param item
* The item to add to this list.
- * @return Whether the item was added to the list succesfully.
+ * @return Whether the item was added to the list successfully.
*/
boolean add(ContainedType item);
@@ -33,7 +33,7 @@ public interface IList<ContainedType> {
*
* @param items
* The list of items to add
- * @return True if every item was succesfully added to the list, false
+ * @return True if every item was successfully added to the list, false
* otherwise
*/
default boolean addAll(IList<ContainedType> items) {
@@ -44,22 +44,22 @@ public interface IList<ContainedType> {
* Check if all of the elements of this list match the specified
* predicate.
*
- * @param matchPredicate
+ * @param matcher
* The predicate to use for checking.
* @return Whether all of the elements of the list match the specified
* predicate.
*/
- boolean allMatch(Predicate<ContainedType> matchPredicate);
+ boolean allMatch(Predicate<ContainedType> matcher);
/**
* Check if any of the elements in this list match the specified list.
*
- * @param matchPredicate
+ * @param matcher
* The predicate to use for checking.
* @return Whether any element in the list matches the provided
* predicate.
*/
- boolean anyMatch(Predicate<ContainedType> matchPredicate);
+ boolean anyMatch(Predicate<ContainedType> matcher);
/**
* Reduce the contents of this list using a collector
@@ -74,8 +74,7 @@ public interface IList<ContainedType> {
*/
public default <StateType, ReducedType> ReducedType collect(
Collector<ContainedType, StateType, ReducedType> collector) {
- BiConsumer<StateType, ContainedType> accumulator = collector
- .accumulator();
+ BiConsumer<StateType, ContainedType> accumulator = collector.accumulator();
return reduceAux(collector.supplier().get(), (value, state) -> {
accumulator.accept(state, value);
@@ -86,7 +85,9 @@ public interface IList<ContainedType> {
/**
* 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.
+ * results.
+ *
+ * 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
@@ -97,15 +98,15 @@ public interface IList<ContainedType> {
* @param <CombinedType>
* The type of the combined list
*
- * @param rightList
+ * @param list
* The list to combine with
- * @param itemCombiner
+ * @param combiner
* The function to use for combining element pairs.
* @return A new list containing the merged pairs of lists.
*/
<OtherType, CombinedType> IList<CombinedType> combineWith(
- IList<OtherType> rightList,
- BiFunction<ContainedType, OtherType, CombinedType> itemCombiner);
+ IList<OtherType> list,
+ BiFunction<ContainedType, OtherType, CombinedType> combiner);
/**
* Check if the list contains the specified item
@@ -125,18 +126,20 @@ public interface IList<ContainedType> {
/**
* Apply a function to each member of the list, then flatten the
- * results. Does not change the underlying list.
+ * results.
+ *
+ * Does not change the underlying list.
*
* @param <MappedType>
* The type of the flattened list
*
- * @param elementExpander
+ * @param expander
* The function to apply to each member of the list.
* @return A new list containing the flattened results of applying the
* provided function.
*/
<MappedType> IList<MappedType> flatMap(
- Function<ContainedType, IList<MappedType>> elementExpander);
+ Function<ContainedType, IList<MappedType>> expander);
/**
* Apply a given action for each member of the list
@@ -149,11 +152,11 @@ public interface IList<ContainedType> {
/**
* Apply a given function to each element in the list and its index.
*
- * @param indexedAction
+ * @param action
* The function to apply to each element in the list and its
* index.
*/
- void forEachIndexed(BiConsumer<Integer, ContainedType> indexedAction);
+ void forEachIndexed(BiConsumer<Integer, ContainedType> action);
/**
* Retrieve a value in the list by its index.
@@ -167,12 +170,11 @@ public interface IList<ContainedType> {
/**
* Retrieve a list containing all elements matching a predicate
*
- * @param matchPredicate
+ * @param predicate
* The predicate to match by
* @return A list containing all elements that match the predicate
*/
- IList<ContainedType> getMatching(
- Predicate<ContainedType> matchPredicate);
+ IList<ContainedType> getMatching(Predicate<ContainedType> predicate);
/**
* Retrieve the size of the wrapped list
@@ -190,17 +192,19 @@ public interface IList<ContainedType> {
/**
* Create a new list by applying the given function to each element in
- * the list. Does not change the underlying list.
+ * the list.
+ *
+ * Does not change the underlying list.
*
* @param <MappedType>
* The type of the transformed list
*
- * @param elementTransformer
+ * @param transformer
* The function to apply to each element in the list
* @return A new list containing the mapped elements of this list.
*/
<MappedType> IList<MappedType> map(
- Function<ContainedType, MappedType> elementTransformer);
+ Function<ContainedType, MappedType> transformer);
/**
* Zip two lists into a list of pairs
@@ -208,22 +212,21 @@ public interface IList<ContainedType> {
* @param <OtherType>
* The type of the second list
*
- * @param rightList
+ * @param list
* The list to use as the left side of the pair
* @return A list containing pairs of this element and the specified
* list
*/
- <OtherType> IList<IPair<ContainedType, OtherType>> pairWith(
- IList<OtherType> rightList);
+ <OtherType> IList<IPair<ContainedType, OtherType>> pairWith(IList<OtherType> list);
/**
* Partition this list into a list of sublists
*
- * @param numberPerPartition
+ * @param partitionSize
* The size of elements to put into each one of the sublists
* @return A list partitioned into partitions of size nPerPart
*/
- IList<IList<ContainedType>> partition(int numberPerPartition);
+ IList<IList<ContainedType>> partition(int partitionSize);
/**
* Prepend an item to the list
@@ -261,37 +264,37 @@ public interface IList<ContainedType> {
* @param <ReducedType>
* The final value type
*
- * @param initialValue
+ * @param initial
* The initial value of the accumulative state.
- * @param stateAccumulator
+ * @param accumulator
* The function to use to combine a list element with the
* accumulative state.
- * @param resultTransformer
+ * @param transformer
* 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.
*/
- <StateType, ReducedType> ReducedType reduceAux(StateType initialValue,
- BiFunction<ContainedType, StateType, StateType> stateAccumulator,
- Function<StateType, ReducedType> resultTransformer);
+ <StateType, ReducedType> ReducedType reduceAux(StateType initial,
+ BiFunction<ContainedType, StateType, StateType> accumulator,
+ Function<StateType, ReducedType> transformer);
/**
* Remove all elements that match a given predicate
*
- * @param removePredicate
+ * @param predicate
* The predicate to use to determine elements to delete
* @return Whether there was anything that satisfied the predicate
*/
- boolean removeIf(Predicate<ContainedType> removePredicate);
+ boolean removeIf(Predicate<ContainedType> predicate);
/**
* Remove all parameters that match a given parameter
*
- * @param desiredElement
+ * @param element
* The object to remove all matching copies of
*/
- void removeMatching(ContainedType desiredElement);
+ void removeMatching(ContainedType element);
/**
* Reverse the contents of this list in place
@@ -300,22 +303,25 @@ public interface IList<ContainedType> {
/**
* 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.
+ * means of comparing elements.
+ *
+ * Since this IS a binary search, the list must have been sorted before hand.
*
- * @param searchKey
+ * @param key
* The key to search for.
* @param comparator
* 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 searchKey,
+ ContainedType search(ContainedType key,
Comparator<ContainedType> comparator);
/**
* Sort the elements of this list using the provided way of comparing
- * elements. Does change the underlying list.
+ * elements.
+ *
+ * Does change the underlying list.
*
* @param comparator
* The way to compare elements for sorting. Pass null to use
@@ -333,16 +339,16 @@ public interface IList<ContainedType> {
/**
* Convert this list into an array
*
- * @param arrType
+ * @param type
* The type of array to return
* @return The list, as an array
*/
- public ContainedType[] toArray(ContainedType[] arrType);
+ public ContainedType[] toArray(ContainedType[] type);
/**
- * Convert the list into a iterable
+ * Convert the list into a Iterable
*
* @return An iterable view onto the list
*/
public Iterable<ContainedType> toIterable();
-} \ No newline at end of file
+}
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 e69495a..9e74628 100644
--- a/BJC-Utils2/src/main/java/bjc/utils/funcdata/IMap.java
+++ b/BJC-Utils2/src/main/java/bjc/utils/funcdata/IMap.java
@@ -146,7 +146,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 recieved
+ * 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
*/
@@ -158,4 +158,4 @@ public interface IMap<KeyType, ValueType> {
* @return A list of values in this map
*/
IList<ValueType> valueList();
-} \ No newline at end of file
+}
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 d43e8d5..c87c34b 100644
--- a/BJC-Utils2/src/main/java/bjc/utils/funcdata/TransformedValueMap.java
+++ b/BJC-Utils2/src/main/java/bjc/utils/funcdata/TransformedValueMap.java
@@ -18,23 +18,23 @@ import java.util.function.Function;
*/
final class TransformedValueMap<OldKey, OldValue, NewValue>
implements IMap<OldKey, NewValue> {
- private IMap<OldKey, OldValue> mapToTransform;
+ private IMap<OldKey, OldValue> backing;
private Function<OldValue, NewValue> transformer;
- public TransformedValueMap(IMap<OldKey, OldValue> destMap,
+ public TransformedValueMap(IMap<OldKey, OldValue> backingMap,
Function<OldValue, NewValue> transform) {
- mapToTransform = destMap;
+ backing = backingMap;
transformer = transform;
}
@Override
public void clear() {
- mapToTransform.clear();
+ backing.clear();
}
@Override
public boolean containsKey(OldKey key) {
- return mapToTransform.containsKey(key);
+ return backing.containsKey(key);
}
@Override
@@ -44,62 +44,61 @@ final class TransformedValueMap<OldKey, OldValue, NewValue>
@Override
public void forEach(BiConsumer<OldKey, NewValue> action) {
- mapToTransform.forEach((key, val) -> {
- action.accept(key, transformer.apply(val));
+ backing.forEach((key, value) -> {
+ action.accept(key, transformer.apply(value));
});
}
@Override
public void forEachKey(Consumer<OldKey> action) {
- mapToTransform.forEachKey(action);
+ backing.forEachKey(action);
}
@Override
public void forEachValue(Consumer<NewValue> action) {
- mapToTransform.forEachValue((val) -> {
- action.accept(transformer.apply(val));
+ backing.forEachValue((value) -> {
+ action.accept(transformer.apply(value));
});
}
@Override
public NewValue get(OldKey key) {
- return transformer.apply(mapToTransform.get(key));
+ return transformer.apply(backing.get(key));
}
@Override
public int getSize() {
- return mapToTransform.getSize();
+ return backing.getSize();
}
@Override
public IList<OldKey> keyList() {
- return mapToTransform.keyList();
+ return backing.keyList();
}
@Override
- public <MappedValue> IMap<OldKey, MappedValue> mapValues(
- Function<NewValue, MappedValue> transform) {
+ public <MappedValue> IMap<OldKey, MappedValue>
+ mapValues(Function<NewValue, MappedValue> transform) {
return new TransformedValueMap<>(this, transform);
}
@Override
- public NewValue put(OldKey key, NewValue val) {
- throw new UnsupportedOperationException(
- "Can't add items to transformed map");
+ public NewValue put(OldKey key, NewValue value) {
+ throw new UnsupportedOperationException("Can't add items to transformed map");
}
@Override
public NewValue remove(OldKey key) {
- return transformer.apply(mapToTransform.remove(key));
+ return transformer.apply(backing.remove(key));
}
@Override
public String toString() {
- return mapToTransform.toString();
+ return backing.toString();
}
@Override
public IList<NewValue> valueList() {
- return mapToTransform.valueList().map(transformer);
+ return backing.valueList().map(transformer);
}
-} \ No newline at end of file
+}
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 865f4d6..b595946 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
@@ -17,20 +17,20 @@ import bjc.utils.funcdata.IList;
* The data type stored in the node.
*/
public class BinarySearchTree<T> {
- /**
+ /*
* The comparator for use in ordering items
*/
private Comparator<T> comparator;
- /**
+ /*
* The current count of elements in the tree
*/
private int elementCount;
- /**
+ /*
* The root element of the tree
*/
- private ITreePart<T> rootElement;
+ private ITreePart<T> root;
/**
* Create a new tree using the specified way to compare elements.
@@ -56,10 +56,10 @@ public class BinarySearchTree<T> {
public void addNode(T element) {
elementCount++;
- if (rootElement == null) {
- rootElement = new BinarySearchTreeNode<>(element, null, null);
+ if (root == null) {
+ root = new BinarySearchTreeNode<>(element, null, null);
} else {
- rootElement.add(element, comparator);
+ root.add(element, comparator);
}
}
@@ -74,25 +74,23 @@ 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) {
- return (pivot - pivotAdjustment) >= 0
- && (pivot + pivotAdjustment) < elements.getSize();
+ private boolean adjustedPivotInBounds(IList<T> elements, int pivot, int pivotAdjustment) {
+ return (pivot - pivotAdjustment) >= 0 && (pivot + pivotAdjustment) < elements.getSize();
}
/**
- * Balance the tree, and remove soft-deleted nodes for free. Takes O(N)
- * time, but also O(N) space.
+ * Balance the tree, and remove soft-deleted nodes for free.
+ *
+ * Takes O(N) time, but also O(N) space.
*/
public void balance() {
IList<T> elements = new FunctionalList<>();
// Add each element to the list in sorted order
- rootElement.forEach(TreeLinearizationMethod.INORDER,
- element -> elements.add(element));
+ root.forEach(TreeLinearizationMethod.INORDER, element -> elements.add(element));
// Clear the tree
- rootElement = null;
+ root = null;
// Set up the pivot and adjustment for readding elements
int pivot = elements.getSize() / 2;
@@ -100,19 +98,14 @@ public class BinarySearchTree<T> {
// Add elements until there aren't any left
while (adjustedPivotInBounds(elements, pivot, pivotAdjustment)) {
- if (rootElement == null) {
+ if (root == null) {
// Create a new root element
- rootElement = new BinarySearchTreeNode<>(
- elements.getByIndex(pivot), null, null);
+ root = new BinarySearchTreeNode<>(elements.getByIndex(pivot), null, null);
} else {
// Add the left and right elements in a balanced manner
- rootElement.add(
- elements.getByIndex(pivot + pivotAdjustment),
- comparator);
+ root.add(elements.getByIndex(pivot + pivotAdjustment), comparator);
- rootElement.add(
- elements.getByIndex(pivot - pivotAdjustment),
- comparator);
+ root.add(elements.getByIndex(pivot - pivotAdjustment), comparator);
}
// Increase the distance from the pivot
@@ -121,18 +114,17 @@ public class BinarySearchTree<T> {
// Add any trailing unbalanced elements
if ((pivot - pivotAdjustment) >= 0) {
- rootElement.add(elements.getByIndex(pivot - pivotAdjustment),
- comparator);
+ root.add(elements.getByIndex(pivot - pivotAdjustment), comparator);
} else if ((pivot + pivotAdjustment) < elements.getSize()) {
- rootElement.add(elements.getByIndex(pivot + pivotAdjustment),
- comparator);
+ root.add(elements.getByIndex(pivot + pivotAdjustment), comparator);
}
}
/**
- * 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-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.
*
* @param element
* The node to delete
@@ -140,7 +132,7 @@ public class BinarySearchTree<T> {
public void deleteNode(T element) {
elementCount--;
- rootElement.delete(element, comparator);
+ root.delete(element, comparator);
}
/**
@@ -149,7 +141,7 @@ public class BinarySearchTree<T> {
* @return The root of the tree.
*/
public ITreePart<T> getRoot() {
- return rootElement;
+ return root;
}
/**
@@ -160,7 +152,7 @@ public class BinarySearchTree<T> {
* @return Whether or not the node is in the tree.
*/
public boolean isInTree(T element) {
- return rootElement.contains(element, comparator);
+ return root.contains(element, comparator);
}
/**
@@ -171,16 +163,14 @@ public class BinarySearchTree<T> {
* @param traversalPredicate
* The function to use until it fails
*/
- public void traverse(TreeLinearizationMethod linearizationMethod,
- Predicate<T> traversalPredicate) {
+ public void traverse(TreeLinearizationMethod linearizationMethod, Predicate<T> traversalPredicate) {
if (linearizationMethod == null) {
- throw new NullPointerException(
- "Linearization method must not be null");
+ throw new NullPointerException("Linearization method must not be null");
} else if (traversalPredicate == null) {
throw new NullPointerException("Predicate must not be nulls");
}
- rootElement.forEach(linearizationMethod, traversalPredicate);
+ root.forEach(linearizationMethod, traversalPredicate);
}
/**
@@ -196,9 +186,9 @@ public class BinarySearchTree<T> {
});
// Clear the tree
- rootElement = null;
+ root = null;
// Add the nodes to the tree in the order they were inserted
nodes.forEach(node -> addNode(node));
}
-} \ No newline at end of file
+}
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 8ceb554..d647742 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
@@ -34,27 +34,13 @@ public class BinarySearchTreeLeaf<T> implements ITreePart<T> {
data = element;
}
- /*
- * Can't add things to a leaf. (non-Javadoc)
- *
- * @see bjc.utils.data.bst.ITreePart#add(java.lang.Object,
- * java.util.Comparator)
- */
@Override
public void add(T element, Comparator<T> comparator) {
throw new IllegalArgumentException("Can't add to a leaf.");
}
- /*
- * Just transform our data. (non-Javadoc)
- *
- * @see
- * bjc.utils.data.bst.ITreePart#collapse(java.util.function.Function,
- * java.util.function.BiFunction)
- */
@Override
- public <E> E collapse(Function<T, E> leafTransformer,
- BiFunction<E, E, E> branchCollapser) {
+ public <E> E collapse(Function<T, E> leafTransformer, BiFunction<E, E, E> branchCollapser) {
if (leafTransformer == null) {
throw new NullPointerException("Transformer must not be null");
}
@@ -62,33 +48,16 @@ public class BinarySearchTreeLeaf<T> implements ITreePart<T> {
return leafTransformer.apply(data);
}
- /*
- * Only check our data. (non-Javadoc)
- *
- * @see bjc.utils.data.bst.ITreePart#contains(java.lang.Object,
- * java.util.Comparator)
- */
@Override
public boolean contains(T element, Comparator<T> comparator) {
return this.data.equals(element);
}
- /*
- * Just get the data (non-Javadoc)
- *
- * @see bjc.utils.data.bst.ITreePart#data()
- */
@Override
public T data() {
return data;
}
- /*
- * Just mark ourselves as "not here" (non-Javadoc)
- *
- * @see bjc.utils.data.bst.ITreePart#delete(java.lang.Object,
- * java.util.Comparator)
- */
@Override
public void delete(T element, Comparator<T> comparator) {
if (data.equals(element)) {
@@ -96,13 +65,6 @@ public class BinarySearchTreeLeaf<T> implements ITreePart<T> {
}
}
- /*
- * Just walk our data and only succede if the walk does, because
- * there's nowhere left to go. (non-Javadoc)
- *
- * @see bjc.utils.data.bst.ITreePart#directedWalk(bjc.utils.data.bst.
- * DirectedWalkFunction)
- */
@Override
public boolean directedWalk(DirectedWalkFunction<T> treeWalker) {
if (treeWalker == null) {
@@ -121,16 +83,8 @@ public class BinarySearchTreeLeaf<T> implements ITreePart<T> {
}
}
- /*
- * Just check our data. (non-Javadoc)
- *
- * @see
- * bjc.utils.data.bst.ITreePart#forEach(bjc.utils.data.bst.ITreePart.
- * TreeLinearizationMethod, java.util.function.Predicate)
- */
@Override
- public boolean forEach(TreeLinearizationMethod linearizationMethod,
- Predicate<T> traversalPredicate) {
+ public boolean forEach(TreeLinearizationMethod linearizationMethod, Predicate<T> traversalPredicate) {
if (traversalPredicate == null) {
throw new NullPointerException("Predicate must not be null");
}
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 9817f91..fa3add0 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
@@ -19,39 +19,33 @@ import java.util.function.Predicate;
* The data type stored in the tree.
*/
public class BinarySearchTreeNode<T> extends BinarySearchTreeLeaf<T> {
- /**
+ /*
* The left child of this node
*/
- private ITreePart<T> leftBranch;
+ private ITreePart<T> left;
- /**
+ /*
* The right child of this node
*/
- private ITreePart<T> rightBranch;
+ private ITreePart<T> right;
/**
* Create a new node with the specified data and children.
*
* @param element
* The data to store in this node.
- * @param left
+ * @param lft
* The left child of this node.
- * @param right
+ * @param rght
* The right child of this node.
*/
- public BinarySearchTreeNode(T element, ITreePart<T> left,
- ITreePart<T> right) {
+ public BinarySearchTreeNode(T element, ITreePart<T> lft,
+ ITreePart<T> rght) {
super(element);
- this.leftBranch = left;
- this.rightBranch = right;
+ this.left = lft;
+ this.right = rght;
}
- /*
- * Either adds it to the left/right, or undeletes itself. (non-Javadoc)
- *
- * @see bjc.utils.data.bst.TreeLeaf#add(java.lang.Object,
- * java.util.Comparator)
- */
@Override
public void add(T element, Comparator<T> comparator) {
if (comparator == null) {
@@ -60,65 +54,57 @@ public class BinarySearchTreeNode<T> extends BinarySearchTreeLeaf<T> {
switch (comparator.compare(data, element)) {
case -1:
- if (leftBranch == null) {
- leftBranch = new BinarySearchTreeNode<>(element, null,
- null);
+ if (left == null) {
+ left = new BinarySearchTreeNode<>(element, null, null);
} else {
- leftBranch.add(element, comparator);
+ left.add(element, comparator);
}
+ break;
case 0:
if (isDeleted) {
isDeleted = false;
} else {
- throw new IllegalArgumentException(
- "Can't add duplicate values");
+ throw new IllegalArgumentException("Can't add duplicate values");
}
+ break;
case 1:
- if (rightBranch == null) {
- rightBranch = new BinarySearchTreeNode<>(element, null,
- null);
+ if (right == null) {
+ right = new BinarySearchTreeNode<>(element, null, null);
} else {
- rightBranch.add(element, comparator);
+ right.add(element, comparator);
}
+ break;
default:
- throw new IllegalStateException(
- "Error: Comparator yielded invalid value");
+ throw new IllegalStateException("Error: Comparator yielded invalid value");
}
}
@Override
- 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) {
if (nodeCollapser == null || branchCollapser == null) {
throw new NullPointerException("Collapser must not be null");
}
E collapsedNode = nodeCollapser.apply(data);
- if (leftBranch != null) {
- E collapsedLeftBranch = leftBranch.collapse(nodeCollapser,
- branchCollapser);
- if (rightBranch != null) {
- E collapsedRightBranch = rightBranch
- .collapse(nodeCollapser, branchCollapser);
+ if (left != null) {
+ E collapsedLeftBranch = left.collapse(nodeCollapser, branchCollapser);
- E collapsedBranches = branchCollapser
- .apply(collapsedLeftBranch, collapsedRightBranch);
+ if (right != null) {
+ E collapsedRightBranch = right.collapse(nodeCollapser, branchCollapser);
- return branchCollapser.apply(collapsedNode,
- collapsedBranches);
+ E collapsedBranches = branchCollapser.apply(collapsedLeftBranch, collapsedRightBranch);
+
+ return branchCollapser.apply(collapsedNode, collapsedBranches);
}
- return branchCollapser.apply(collapsedNode,
- collapsedLeftBranch);
+ return branchCollapser.apply(collapsedNode, collapsedLeftBranch);
}
- if (rightBranch != null) {
- E collapsedRightBranch = rightBranch.collapse(nodeCollapser,
- branchCollapser);
+ if (right != null) {
+ E collapsedRightBranch = right.collapse(nodeCollapser, branchCollapser);
- return branchCollapser.apply(collapsedNode,
- collapsedRightBranch);
+ return branchCollapser.apply(collapsedNode, collapsedRightBranch);
}
return collapsedNode;
@@ -153,12 +139,12 @@ public class BinarySearchTreeNode<T> extends BinarySearchTreeLeaf<T> {
directedWalk(currentElement -> {
switch (comparator.compare(data, element)) {
case -1:
- return leftBranch == null ? FAILURE : LEFT;
+ return left == null ? FAILURE : LEFT;
case 0:
isDeleted = true;
return FAILURE;
case 1:
- return rightBranch == null ? FAILURE : RIGHT;
+ return right == null ? FAILURE : RIGHT;
default:
return FAILURE;
}
@@ -175,9 +161,9 @@ public class BinarySearchTreeNode<T> extends BinarySearchTreeLeaf<T> {
case SUCCESS:
return true;
case LEFT:
- return leftBranch.directedWalk(treeWalker);
+ return left.directedWalk(treeWalker);
case RIGHT:
- return rightBranch.directedWalk(treeWalker);
+ return right.directedWalk(treeWalker);
case FAILURE:
return false;
default:
@@ -186,25 +172,20 @@ public class BinarySearchTreeNode<T> extends BinarySearchTreeLeaf<T> {
}
@Override
- public boolean forEach(TreeLinearizationMethod linearizationMethod,
- Predicate<T> traversalPredicate) {
+ public boolean forEach(TreeLinearizationMethod linearizationMethod, Predicate<T> traversalPredicate) {
if (linearizationMethod == null) {
- throw new NullPointerException(
- "Linearization method must not be null");
+ throw new NullPointerException("Linearization method must not be null");
} else if (traversalPredicate == null) {
throw new NullPointerException("Predicate must not be null");
}
switch (linearizationMethod) {
case PREORDER:
- return preorderTraverse(linearizationMethod,
- traversalPredicate);
+ return preorderTraverse(linearizationMethod, traversalPredicate);
case INORDER:
- return inorderTraverse(linearizationMethod,
- traversalPredicate);
+ return inorderTraverse(linearizationMethod, traversalPredicate);
case POSTORDER:
- return postorderTraverse(linearizationMethod,
- traversalPredicate);
+ return postorderTraverse(linearizationMethod, traversalPredicate);
default:
throw new IllegalArgumentException(
"Passed an incorrect TreeLinearizationMethod "
@@ -212,10 +193,7 @@ public class BinarySearchTreeNode<T> extends BinarySearchTreeLeaf<T> {
}
}
- private boolean inorderTraverse(
- TreeLinearizationMethod linearizationMethod,
- Predicate<T> traversalPredicate) {
-
+ private boolean inorderTraverse( TreeLinearizationMethod linearizationMethod, Predicate<T> traversalPredicate) {
if (!traverseLeftBranch(linearizationMethod, traversalPredicate)) {
return false;
}
@@ -224,24 +202,19 @@ public class BinarySearchTreeNode<T> extends BinarySearchTreeLeaf<T> {
return false;
}
- if (!traverseRightBranch(linearizationMethod,
- traversalPredicate)) {
+ if (!traverseRightBranch(linearizationMethod, traversalPredicate)) {
return false;
}
return true;
}
- private boolean postorderTraverse(
- TreeLinearizationMethod linearizationMethod,
- Predicate<T> traversalPredicate) {
-
+ private boolean postorderTraverse(TreeLinearizationMethod linearizationMethod, Predicate<T> traversalPredicate) {
if (!traverseLeftBranch(linearizationMethod, traversalPredicate)) {
return false;
}
- if (!traverseRightBranch(linearizationMethod,
- traversalPredicate)) {
+ if (!traverseRightBranch(linearizationMethod, traversalPredicate)) {
return false;
}
@@ -253,10 +226,7 @@ public class BinarySearchTreeNode<T> extends BinarySearchTreeLeaf<T> {
}
- private boolean preorderTraverse(
- TreeLinearizationMethod linearizationMethod,
- Predicate<T> traversalPredicate) {
-
+ private boolean preorderTraverse(TreeLinearizationMethod linearizationMethod, Predicate<T> traversalPredicate) {
if (!traverseElement(traversalPredicate)) {
return false;
}
@@ -265,8 +235,7 @@ public class BinarySearchTreeNode<T> extends BinarySearchTreeLeaf<T> {
return false;
}
- if (!traverseRightBranch(linearizationMethod,
- traversalPredicate)) {
+ if (!traverseRightBranch(linearizationMethod, traversalPredicate)) {
return false;
}
@@ -275,37 +244,37 @@ public class BinarySearchTreeNode<T> extends BinarySearchTreeLeaf<T> {
private boolean traverseElement(Predicate<T> traversalPredicate) {
boolean nodeSuccesfullyTraversed;
+
if (isDeleted) {
nodeSuccesfullyTraversed = true;
} else {
nodeSuccesfullyTraversed = traversalPredicate.test(data);
}
+
return nodeSuccesfullyTraversed;
}
- private boolean traverseLeftBranch(
- TreeLinearizationMethod linearizationMethod,
- Predicate<T> traversalPredicate) {
+ private boolean traverseLeftBranch(TreeLinearizationMethod linearizationMethod, Predicate<T> traversalPredicate) {
boolean leftSuccesfullyTraversed;
- if (leftBranch == null) {
+
+ if (left == null) {
leftSuccesfullyTraversed = true;
} else {
- leftSuccesfullyTraversed = leftBranch
- .forEach(linearizationMethod, traversalPredicate);
+ leftSuccesfullyTraversed = left.forEach(linearizationMethod, traversalPredicate);
}
+
return leftSuccesfullyTraversed;
}
- private boolean traverseRightBranch(
- TreeLinearizationMethod linearizationMethod,
- Predicate<T> traversalPredicate) {
+ private boolean traverseRightBranch(TreeLinearizationMethod linearizationMethod, Predicate<T> traversalPredicate) {
boolean rightSuccesfullyTraversed;
- if (rightBranch == null) {
+
+ if (right == null) {
rightSuccesfullyTraversed = true;
} else {
- rightSuccesfullyTraversed = rightBranch
- .forEach(linearizationMethod, traversalPredicate);
+ rightSuccesfullyTraversed = right.forEach(linearizationMethod, traversalPredicate);
}
+
return rightSuccesfullyTraversed;
}
-} \ No newline at end of file
+}
diff --git a/BJC-Utils2/src/main/java/bjc/utils/funcdata/bst/ITreePart.java b/BJC-Utils2/src/main/java/bjc/utils/funcdata/bst/ITreePart.java
index cbd7229..c574196 100644
--- a/BJC-Utils2/src/main/java/bjc/utils/funcdata/bst/ITreePart.java
+++ b/BJC-Utils2/src/main/java/bjc/utils/funcdata/bst/ITreePart.java
@@ -74,11 +74,11 @@ public interface ITreePart<T> {
/**
* Execute a directed walk through the tree.
*
- * @param treeWalker
+ * @param walker
* The function to use to direct the walk through the tree.
* @return Whether the directed walk finished successfully.
*/
- public boolean directedWalk(DirectedWalkFunction<T> treeWalker);
+ public boolean directedWalk(DirectedWalkFunction<T> walker);
/**
* Execute a provided function for each element of tree it succesfully
@@ -86,11 +86,11 @@ public interface ITreePart<T> {
*
* @param linearizationMethod
* The way to linearize the tree for executing
- * @param traversalPredicate
- * The function to apply to each element, where it returning
+ * @param predicate
+ * 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> traversalPredicate);
+ Predicate<T> predicate);
}
diff --git a/BJC-Utils2/src/main/java/bjc/utils/funcdata/theory/Functor.java b/BJC-Utils2/src/main/java/bjc/utils/funcdata/theory/Functor.java
index 10cfffe..9749d95 100644
--- a/BJC-Utils2/src/main/java/bjc/utils/funcdata/theory/Functor.java
+++ b/BJC-Utils2/src/main/java/bjc/utils/funcdata/theory/Functor.java
@@ -27,10 +27,9 @@ public interface Functor<ContainedType> {
* @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