summaryrefslogtreecommitdiff
path: root/base/src/main/java/bjc/utils/funcutils
diff options
context:
space:
mode:
Diffstat (limited to 'base/src/main/java/bjc/utils/funcutils')
-rw-r--r--base/src/main/java/bjc/utils/funcutils/CollectorUtils.java27
-rw-r--r--base/src/main/java/bjc/utils/funcutils/CompoundCollector.java33
-rw-r--r--base/src/main/java/bjc/utils/funcutils/EnumUtils.java33
-rw-r--r--base/src/main/java/bjc/utils/funcutils/FileUtils.java31
-rw-r--r--base/src/main/java/bjc/utils/funcutils/FuncUtils.java33
-rw-r--r--base/src/main/java/bjc/utils/funcutils/FunctionalFileVisitor.java15
-rw-r--r--base/src/main/java/bjc/utils/funcutils/GroupPartIteration.java38
-rw-r--r--base/src/main/java/bjc/utils/funcutils/IBuilder.java18
-rw-r--r--base/src/main/java/bjc/utils/funcutils/Isomorphism.java23
-rw-r--r--base/src/main/java/bjc/utils/funcutils/LambdaLock.java30
-rw-r--r--base/src/main/java/bjc/utils/funcutils/ListUtils.java166
-rw-r--r--base/src/main/java/bjc/utils/funcutils/NumberUtils.java42
-rw-r--r--base/src/main/java/bjc/utils/funcutils/StringUtils.java105
-rw-r--r--base/src/main/java/bjc/utils/funcutils/TreeUtils.java9
-rw-r--r--base/src/main/java/bjc/utils/funcutils/TriConsumer.java15
15 files changed, 363 insertions, 255 deletions
diff --git a/base/src/main/java/bjc/utils/funcutils/CollectorUtils.java b/base/src/main/java/bjc/utils/funcutils/CollectorUtils.java
index a044bfd..2da5967 100644
--- a/base/src/main/java/bjc/utils/funcutils/CollectorUtils.java
+++ b/base/src/main/java/bjc/utils/funcutils/CollectorUtils.java
@@ -9,27 +9,34 @@ import bjc.utils.data.IPair;
* Utilities for producing implementations of {@link Collector}
*
* @author ben
- *
*/
public class CollectorUtils {
/**
- * Create a collector that applies two collectors at once
+ * Create a collector that applies two collectors at once.
*
* @param <InitialType>
- * The type of the collection to collect from
+ * The type of the collection to collect from.
+ *
* @param <AuxType1>
- * The intermediate type of the first collector
+ * The intermediate type of the first collector.
+ *
* @param <AuxType2>
- * The intermediate type of the second collector
+ * The intermediate type of the second collector.
+ *
* @param <FinalType1>
- * The final type of the first collector
+ * The final type of the first collector.
+ *
* @param <FinalType2>
- * The final type of the second collector
+ * The final type of the second collector.
+ *
* @param first
- * The first collector to use
+ * The first collector to use.
+ *
* @param second
- * The second collector to use
- * @return A collector that functions as mentioned above
+ * The second collector to use.
+ *
+ * @return
+ * A collector that functions as mentioned above.
*/
public static <InitialType, AuxType1, AuxType2, FinalType1, FinalType2> Collector<InitialType, IHolder<IPair<AuxType1, AuxType2>>, IPair<FinalType1, FinalType2>> compoundCollect(
final Collector<InitialType, AuxType1, FinalType1> first,
diff --git a/base/src/main/java/bjc/utils/funcutils/CompoundCollector.java b/base/src/main/java/bjc/utils/funcutils/CompoundCollector.java
index 35695bc..928c3f4 100644
--- a/base/src/main/java/bjc/utils/funcutils/CompoundCollector.java
+++ b/base/src/main/java/bjc/utils/funcutils/CompoundCollector.java
@@ -12,26 +12,43 @@ import bjc.utils.data.IPair;
import bjc.utils.data.Identity;
import bjc.utils.data.Pair;
+/**
+ * Implementation of a collecter that uses two collectors.
+ *
+ * @author Ben Culkin
+ */
final class CompoundCollector<InitialType, AuxType1, AuxType2, FinalType1, FinalType2>
implements Collector<InitialType, IHolder<IPair<AuxType1, AuxType2>>, IPair<FinalType1, FinalType2>> {
-
+ /* Our characteristics. */
private final Set<java.util.stream.Collector.Characteristics> characteristicSet;
- private final Collector<InitialType, AuxType1, FinalType1> first;
- private final Collector<InitialType, AuxType2, FinalType2> second;
-
+ /* The first collector. */
+ private final Collector<InitialType, AuxType1, FinalType1> first;
+ /* The second collector. */
+ private final Collector<InitialType, AuxType2, FinalType2> second;
+
+ /**
+ * Create a collector that uses two collectors.
+ *
+ * @param first
+ * The first collector.
+ *
+ * @param second
+ * The second collector.
+ */
public CompoundCollector(final Collector<InitialType, AuxType1, FinalType1> first,
final Collector<InitialType, AuxType2, FinalType2> second) {
this.first = first;
this.second = second;
+ /* Accumulate characteristics. */
characteristicSet = first.characteristics();
characteristicSet.addAll(second.characteristics());
}
@Override
public BiConsumer<IHolder<IPair<AuxType1, AuxType2>>, InitialType> accumulator() {
- final BiConsumer<AuxType1, InitialType> firstAccumulator = first.accumulator();
+ final BiConsumer<AuxType1, InitialType> firstAccumulator = first.accumulator();
final BiConsumer<AuxType2, InitialType> secondAccumulator = second.accumulator();
return (state, value) -> {
@@ -51,7 +68,7 @@ final class CompoundCollector<InitialType, AuxType1, AuxType2, FinalType1, Final
@Override
public BinaryOperator<IHolder<IPair<AuxType1, AuxType2>>> combiner() {
- final BinaryOperator<AuxType1> firstCombiner = first.combiner();
+ final BinaryOperator<AuxType1> firstCombiner = first.combiner();
final BinaryOperator<AuxType2> secondCombiner = second.combiner();
return (leftState, rightState) -> {
@@ -68,7 +85,7 @@ final class CompoundCollector<InitialType, AuxType1, AuxType2, FinalType1, Final
return state -> {
return state.unwrap(pair -> {
return pair.bind((left, right) -> {
- final FinalType1 finalLeft = first.finisher().apply(left);
+ final FinalType1 finalLeft = first.finisher().apply(left);
final FinalType2 finalRight = second.finisher().apply(right);
return new Pair<>(finalLeft, finalRight);
@@ -80,7 +97,7 @@ final class CompoundCollector<InitialType, AuxType1, AuxType2, FinalType1, Final
@Override
public Supplier<IHolder<IPair<AuxType1, AuxType2>>> supplier() {
return () -> {
- final AuxType1 initialLeft = first.supplier().get();
+ final AuxType1 initialLeft = first.supplier().get();
final AuxType2 initialRight = second.supplier().get();
return new Identity<>(new Pair<>(initialLeft, initialRight));
diff --git a/base/src/main/java/bjc/utils/funcutils/EnumUtils.java b/base/src/main/java/bjc/utils/funcutils/EnumUtils.java
index e4c0bda..2039b97 100644
--- a/base/src/main/java/bjc/utils/funcutils/EnumUtils.java
+++ b/base/src/main/java/bjc/utils/funcutils/EnumUtils.java
@@ -7,25 +7,28 @@ import bjc.utils.funcdata.FunctionalList;
import bjc.utils.funcdata.IList;
/**
- * Utility methods on enums
+ * Utility methods on enums.
*
* @author ben
- *
*/
public class EnumUtils {
/**
- * Do an action for a random number of enum values
+ * Do an action for a random number of enum values.
*
* @param <E>
- * The type of the enum
+ * The type of the enum.
+ *
* @param clasz
- * The enum class
+ * The enum class.
+ *
* @param nValues
- * The number of values to execute the action on
+ * The number of values to execute the action on.
+ *
* @param action
- * The action to perform on random values
+ * The action to perform on random values.
+ *
* @param rnd
- * The source of randomness to use
+ * The source of randomness to use.
*/
public static <E extends Enum<E>> void doForValues(final Class<E> clasz, final int nValues,
final Consumer<E> action, final Random rnd) {
@@ -45,15 +48,19 @@ public class EnumUtils {
}
/**
- * Get a random value from an enum
+ * Get a random value from an enum.
*
* @param <E>
- * The type of the enum
+ * The type of the enum.
+ *
* @param clasz
- * The class of the enum
+ * The class of the enum.
+ *
* @param rnd
- * The random source to use
- * @return A random value from the specified enum
+ * The random source to use.
+ *
+ * @return
+ * A random value from the specified enum.
*/
public static <E extends Enum<E>> E getRandomValue(final Class<E> clasz, final Random rnd) {
final E[] enumValues = clasz.getEnumConstants();
diff --git a/base/src/main/java/bjc/utils/funcutils/FileUtils.java b/base/src/main/java/bjc/utils/funcutils/FileUtils.java
index 87199b1..6b897df 100644
--- a/base/src/main/java/bjc/utils/funcutils/FileUtils.java
+++ b/base/src/main/java/bjc/utils/funcutils/FileUtils.java
@@ -7,31 +7,34 @@ import java.nio.file.attribute.BasicFileAttributes;
import java.util.function.BiPredicate;
/**
- * Utilities for doing things with files
+ * Utilities for doing things with files.
*
* @author ben
- *
*/
public class FileUtils {
+ /*
+ * @NOTE
+ * If it becomes necessary, write another overload
+ * for this with all the buttons and knobs from
+ * walkFileTree.
+ */
/**
- * Traverse a directory recursively. This is a depth-first traversal
- *
+ * Traverse a directory recursively. This is a depth-first traversal.
*
* @param root
- * The directory to start the traversal at
+ * The directory to start the traversal at.
+ *
* @param predicate
- * The predicate to determine whether or not to traverse
- * a directory
+ * The predicate to determine whether or not to traverse
+ * a directory.
+ *
* @param action
- * The action to invoke upon each file in the directory.
- * Returning true means to continue the traversal,
- * returning false stops it
+ * The action to invoke upon each file in the directory. Returning
+ * true means to continue the traversal, returning false stops it.
+ *
* @throws IOException
- * if the walk throws an exception
+ * If the walk throws an exception.
*
- * TODO If it becomes necessary, write another overload
- * for this with all the buttons and knobs from
- * walkFileTree
*/
public static void traverseDirectory(final Path root, final BiPredicate<Path, BasicFileAttributes> predicate,
final BiPredicate<Path, BasicFileAttributes> action) throws IOException {
diff --git a/base/src/main/java/bjc/utils/funcutils/FuncUtils.java b/base/src/main/java/bjc/utils/funcutils/FuncUtils.java
index 9950add..4be6d78 100644
--- a/base/src/main/java/bjc/utils/funcutils/FuncUtils.java
+++ b/base/src/main/java/bjc/utils/funcutils/FuncUtils.java
@@ -6,26 +6,29 @@ import java.util.function.Function;
import java.util.function.UnaryOperator;
/**
- * Utility things for functions
+ * Utility things for functions.
*
* @author ben
- *
*/
public class FuncUtils {
/**
* Convert a binary function into a unary function that returns a
- * function
+ * function.
*
* @param <A>
- * The initial type of the function
+ * The initial type of the function.
+ *
* @param <B>
- * The intermediate type of the function
+ * The intermediate type of the function.
+ *
* @param <C>
- * The terminal type of the function
+ * The terminal type of the function.
+ *
* @param func
- * The function to transform
- * @return The function transformed into a unary function returning a
- * function
+ * The function to transform.
+ *
+ * @return
+ * The function transformed into a unary function returning a function.
*/
public static <A, B, C> Function<A, Function<B, C>> curry2(final BiFunction<A, B, C> func) {
return arg1 -> arg2 -> {
@@ -34,12 +37,13 @@ public class FuncUtils {
}
/**
- * Do the specified action the specified number of times
+ * Do the specified action the specified number of times.
*
* @param nTimes
- * The number of times to do the action
+ * The number of times to do the action.
+ *
* @param cons
- * The action to perform
+ * The action to perform.
*/
public static void doTimes(final int nTimes, final Consumer<Integer> cons) {
for (int i = 0; i < nTimes; i++) {
@@ -52,9 +56,10 @@ public class FuncUtils {
*
* @param op
* The operator to execute.
+ *
* @param maxTries
- * The maximum amount of times to apply the function in an
- * attempt to cause it to converge.
+ * The maximum amount of times to apply the function in an attempt
+ * to cause it to converge.
*/
public static <T> UnaryOperator<T> converge(final UnaryOperator<T> op, final int maxTries) {
return (val) -> {
diff --git a/base/src/main/java/bjc/utils/funcutils/FunctionalFileVisitor.java b/base/src/main/java/bjc/utils/funcutils/FunctionalFileVisitor.java
index db6c43b..b0dd162 100644
--- a/base/src/main/java/bjc/utils/funcutils/FunctionalFileVisitor.java
+++ b/base/src/main/java/bjc/utils/funcutils/FunctionalFileVisitor.java
@@ -7,13 +7,26 @@ import java.nio.file.SimpleFileVisitor;
import java.nio.file.attribute.BasicFileAttributes;
import java.util.function.BiPredicate;
-/*
+/**
* Functional implementation of a file visitor.
+ *
+ * @author Ben Culkin
*/
final class FunctionalFileVisitor extends SimpleFileVisitor<Path> {
+ /* Our predicate to pick files. */
private final BiPredicate<Path, BasicFileAttributes> predicate;
+ /* Our action to aply to files. */
private final BiPredicate<Path, BasicFileAttributes> action;
+ /**
+ * Create a new file visitor, powered by functions.
+ *
+ * @param predicate
+ * The predicate to use to pick which files to traverse.
+ *
+ * @param action
+ * The function to execute on every file.
+ */
public FunctionalFileVisitor(final BiPredicate<Path, BasicFileAttributes> predicate,
final BiPredicate<Path, BasicFileAttributes> action) {
this.predicate = predicate;
diff --git a/base/src/main/java/bjc/utils/funcutils/GroupPartIteration.java b/base/src/main/java/bjc/utils/funcutils/GroupPartIteration.java
index f3b2254..9e4b43a 100644
--- a/base/src/main/java/bjc/utils/funcutils/GroupPartIteration.java
+++ b/base/src/main/java/bjc/utils/funcutils/GroupPartIteration.java
@@ -7,24 +7,46 @@ import bjc.utils.funcdata.FunctionalList;
import bjc.utils.funcdata.IList;
/**
- * Implements a single group partitioning pass on a list
+ * Implements a single group partitioning pass on a list.
*
* @author ben
*
* @param <E>
- * The type of element in the list being partitioned
+ * The type of element in the list being partitioned
*/
final class GroupPartIteration<E> implements Consumer<E> {
+ /* The list we're returning. */
private final IList<IList<E>> returnedList;
- public IList<E> currentPartition;
- private final IList<E> rejectedItems;
+ /* The current partition of the list. */
+ public IList<E> currentPartition;
+ /* The items rejected from the current partition. */
+ private final IList<E> rejectedItems;
- private int numberInCurrentPartition;
- private final int numberPerPartition;
+ /* The number of items in the current partition. */
+ private int numberInCurrentPartition;
+ /* The number of items in each partition. */
+ private final int numberPerPartition;
+ /* The function to use to count an item. */
private final Function<E, Integer> elementCounter;
+ /**
+ * Create a new group partitioning iteration.
+ *
+ * @param returned
+ * The list containing all of the existing partitions.
+ *
+ * @param rejects
+ * The items that have been rejected from a partition for being too
+ * large.
+ *
+ * @param nPerPart
+ * The combined value of items that should go into each partition.
+ *
+ * @param eleCount
+ * The function to use to determine the value of an item.
+ */
public GroupPartIteration(final IList<IList<E>> returned, final IList<E> rejects, final int nPerPart,
final Function<E, Integer> eleCount) {
this.returnedList = returned;
@@ -48,8 +70,8 @@ final class GroupPartIteration<E> implements Consumer<E> {
} else {
final int currentElementCount = elementCounter.apply(value);
- final boolean shouldReject = numberInCurrentPartition
- + currentElementCount >= numberPerPartition;
+ final boolean shouldReject =
+ (numberInCurrentPartition + currentElementCount) >= numberPerPartition;
if (shouldReject) {
rejectedItems.add(value);
diff --git a/base/src/main/java/bjc/utils/funcutils/IBuilder.java b/base/src/main/java/bjc/utils/funcutils/IBuilder.java
index a96a4d6..6cbb838 100644
--- a/base/src/main/java/bjc/utils/funcutils/IBuilder.java
+++ b/base/src/main/java/bjc/utils/funcutils/IBuilder.java
@@ -1,29 +1,31 @@
package bjc.utils.funcutils;
/**
- * Generic interface for objects that implement the builder pattern
+ * Generic interface for objects that implement the builder pattern.
*
* @author ben
*
* @param <E>
- * The type of object being built
+ * The type of object being built.
*/
public interface IBuilder<E> {
/**
- * Build the object this builder is building
+ * Build the object this builder is building.
+ *
+ * @return
+ * The built object.
*
- * @return The built object
* @throws IllegalStateException
- * if the data in the builder cannot be built into its
- * corresponding object at this point in time
+ * If the data in the builder cannot be built into its
+ * corresponding object at this point in time.
*/
public E build();
/**
- * Reset the state of this builder to its initial state
+ * Reset the state of this builder to its initial state.
*
* @throws UnsupportedOperationException
- * if the builder doesn't support resetting its state
+ * If the builder doesn't support resetting its state.
*/
public default void reset() {
throw new UnsupportedOperationException("Builder doesn't support state resetting");
diff --git a/base/src/main/java/bjc/utils/funcutils/Isomorphism.java b/base/src/main/java/bjc/utils/funcutils/Isomorphism.java
index 2d3655e..d86ee9f 100644
--- a/base/src/main/java/bjc/utils/funcutils/Isomorphism.java
+++ b/base/src/main/java/bjc/utils/funcutils/Isomorphism.java
@@ -8,26 +8,25 @@ import java.util.function.Function;
* @author bjculkin
*
* @param <S>
- * The source type of the isomorphism.
+ * The source type of the isomorphism.
*
* @param <D>
- * The destination type of isomorphism.
- *
+ * The destination type of isomorphism.
*/
public class Isomorphism<S, D> {
+ /* The function to the destination type. */
private Function<S, D> toFunc;
+ /* The function to the source type. */
private Function<D, S> fromFunc;
/**
* Create a new isomorphism.
*
* @param to
- * The 'forward' function, from the source to the
- * definition.
+ * The 'forward' function, from the source to the definition.
*
* @param from
- * The 'backward' function, from the definition to the
- * source.
+ * The 'backward' function, from the definition to the source.
*/
public Isomorphism(Function<S, D> to, Function<D, S> from) {
toFunc = to;
@@ -38,9 +37,10 @@ public class Isomorphism<S, D> {
* Apply the isomorphism forward.
*
* @param val
- * The source value.
+ * The source value.
*
- * @return The destination value.
+ * @return
+ * The destination value.
*/
public D to(S val) {
return toFunc.apply(val);
@@ -50,9 +50,10 @@ public class Isomorphism<S, D> {
* Apply the isomorphism backward.
*
* @param val
- * The destination value.
+ * The destination value.
*
- * @return The source value.
+ * @return
+ * The source value.
*/
public S from(D val) {
return fromFunc.apply(val);
diff --git a/base/src/main/java/bjc/utils/funcutils/LambdaLock.java b/base/src/main/java/bjc/utils/funcutils/LambdaLock.java
index 62c5d32..2506d53 100644
--- a/base/src/main/java/bjc/utils/funcutils/LambdaLock.java
+++ b/base/src/main/java/bjc/utils/funcutils/LambdaLock.java
@@ -10,15 +10,14 @@ import java.util.function.Supplier;
* properly.
*
* @author EVE
- *
*/
public class LambdaLock {
- private final Lock readLock;
- private final Lock writeLock;
+ /* The read lock. */
+ private final Lock readLock;
+ /* The write lock. */
+ private final Lock writeLock;
- /**
- * Create a new lambda-enabled lock around a new lock.
- */
+ /** Create a new lambda-enabled lock around a new lock. */
public LambdaLock() {
this(new ReentrantReadWriteLock());
}
@@ -27,7 +26,7 @@ public class LambdaLock {
* Create a new lambda-enabled lock.
*
* @param lck
- * The lock to wrap.
+ * The lock to wrap.
*/
public LambdaLock(final ReadWriteLock lck) {
readLock = lck.readLock();
@@ -38,9 +37,10 @@ public class LambdaLock {
* Execute an action with the read lock taken.
*
* @param supp
- * The action to call.
+ * The action to call.
*
- * @return The result of the action.
+ * @return
+ * The result of the action.
*/
public <T> T read(final Supplier<T> supp) {
readLock.lock();
@@ -56,9 +56,10 @@ public class LambdaLock {
* Execute an action with the write lock taken.
*
* @param supp
- * The action to call.
+ * The action to call.
*
- * @return The result of the action.
+ * @return
+ * The result of the action.
*/
public <T> T write(final Supplier<T> supp) {
writeLock.lock();
@@ -74,8 +75,7 @@ public class LambdaLock {
* Execute an action with the read lock taken.
*
* @param action
- * The action to call.
- *
+ * The action to call.
*/
public void read(final Runnable action) {
readLock.lock();
@@ -91,7 +91,7 @@ public class LambdaLock {
* Execute an action with the write lock taken.
*
* @param action
- * The action to call.
+ * The action to call.
*/
public void write(final Runnable action) {
writeLock.lock();
@@ -102,4 +102,4 @@ public class LambdaLock {
writeLock.unlock();
}
}
-} \ No newline at end of file
+}
diff --git a/base/src/main/java/bjc/utils/funcutils/ListUtils.java b/base/src/main/java/bjc/utils/funcutils/ListUtils.java
index c0daa1e..2441a80 100644
--- a/base/src/main/java/bjc/utils/funcutils/ListUtils.java
+++ b/base/src/main/java/bjc/utils/funcutils/ListUtils.java
@@ -10,21 +10,23 @@ import bjc.utils.funcdata.IList;
/**
* Utilities for manipulating FunctionalLists that don't belong in the class
- * itself
+ * itself.
*
* @author ben
- *
*/
public class ListUtils {
+ /* The max amount of times to try to partition things. */
private static final int MAX_NTRIESPART = 50;
/**
* Collapse a string of tokens into a single string without adding any
- * spaces
+ * spaces.
*
* @param input
- * The list of tokens to collapse
- * @return The collapsed string of tokens
+ * The list of tokens to collapse.
+ *
+ * @return
+ * The collapsed string of tokens.
*/
public static String collapseTokens(final IList<String> input) {
if (input == null) throw new NullPointerException("Input must not be null");
@@ -34,24 +36,29 @@ public class ListUtils {
/**
* Collapse a string of tokens into a single string, adding the desired
- * separator after each token
+ * separator after each token.
*
* @param input
- * The list of tokens to collapse
+ * The list of tokens to collapse.
+ *
* @param seperator
- * The separator to use for separating tokens
- * @return The collapsed string of tokens
+ * The separator to use for separating tokens.
+ *
+ * @return
+ * The collapsed string of tokens.
*/
public static String collapseTokens(final IList<String> input, final String seperator) {
- if (input == null)
+ if (input == null) {
throw new NullPointerException("Input must not be null");
- else if (seperator == null) throw new NullPointerException("Seperator must not be null");
+ } else if (seperator == null) {
+ throw new NullPointerException("Seperator must not be null");
+ }
- if (input.getSize() < 1)
+ if (input.getSize() < 1) {
return "";
- else if (input.getSize() == 1)
+ } else if (input.getSize() == 1) {
return input.first();
- else {
+ } else {
final StringBuilder state = new StringBuilder();
int i = 1;
@@ -70,21 +77,25 @@ public class ListUtils {
}
/**
- * Select a number of random items from the list without replacement
+ * Select a number of random items from the list without replacement.
*
* @param <E>
- * The type of items to select
+ * The type of items to select.
+ *
* @param list
- * The list to select from
+ * The list to select from.
+ *
* @param number
- * The number of items to selet
+ * The number of items to selet.
+ *
* @param rng
- * A function that creates a random number from 0 to the
- * desired number
- * @return A new list containing the desired number of items randomly
- * selected from the specified list without replacement
+ * A function that creates a random number from 0 to the
+ * desired number.
+ *
+ * @return
+ * A new list containing the desired number of items randomly
+ * selected from the specified list without replacement.
*/
-
public static <E> IList<E> drawWithoutReplacement(final IList<E> list, final int number,
final Function<Integer, Integer> rng) {
final IList<E> selected = new FunctionalList<>(new ArrayList<>(number));
@@ -95,19 +106,13 @@ public class ListUtils {
E element = null;
for (final int index = 0; itr.hasNext(); element = itr.next()) {
- /*
- * n - m
- */
+ /* n - m */
final int winningChance = number - selected.getSize();
- /*
- * N - t
- */
+ /* N - t */
final int totalChance = total - (index - 1);
- /*
- * Probability of selecting the t+1'th element
- */
+ /* Probability of selecting the t+1'th element */
if (NumberUtils.isProbable(winningChance, totalChance, rng)) {
selected.add(element);
}
@@ -117,19 +122,24 @@ public class ListUtils {
}
/**
- * Select a number of random items from the list, with replacement
+ * Select a number of random items from the list, with replacement.
*
* @param <E>
- * The type of items to select
+ * The type of items to select.
+ *
* @param list
- * The list to select from
+ * The list to select from.
+ *
* @param number
- * The number of items to selet
+ * The number of items to select.
+ *
* @param rng
- * A function that creates a random number from 0 to the
- * desired number
- * @return A new list containing the desired number of items randomly
- * selected from the specified list
+ * A function that creates a random number from 0 to the
+ * desired number.
+ *
+ * @return
+ * A new list containing the desired number of items randomly
+ * selected from the specified list.
*/
public static <E> IList<E> drawWithReplacement(final IList<E> list, final int number,
final Function<Integer, Integer> rng) {
@@ -144,57 +154,51 @@ public class ListUtils {
/**
* Partition a list into a list of lists, where each element can count
- * for more than one element in a partition
+ * for more than one element in a partition.
*
* @param <E>
- * The type of elements in the list to partition
+ * The type of elements in the list to partition.
*
* @param input
- * The list to partition
+ * The list to partition.
+ *
* @param counter
- * The function to determine the count for each element
- * for
+ * The function to determine the count for each element for.
+ *
* @param partitionSize
- * The number of elements to put in each partition
+ * The number of elements to put in each partition.
*
- * @return A list partitioned according to the above rules
+ * @return
+ * A list partitioned according to the above rules.
*/
public static <E> IList<IList<E>> groupPartition(final IList<E> input, final Function<E, Integer> counter,
final int partitionSize) {
- if (input == null)
+ if (input == null) {
throw new NullPointerException("Input list must not be null");
- else if (counter == null)
+ } else if (counter == null) {
throw new NullPointerException("Counter must not be null");
- else if (partitionSize < 1 || partitionSize > input.getSize()) {
+ } else if (partitionSize < 1 || partitionSize > input.getSize()) {
final String fmt = "%d is not a valid partition size. Must be between 1 and %d";
final String msg = String.format(fmt, partitionSize, input.getSize());
throw new IllegalArgumentException(msg);
}
- /*
- * List that holds our results
- */
+ /* List that holds our results. */
final IList<IList<E>> returned = new FunctionalList<>();
- /*
- * List that holds elements rejected during current pass
- */
+ /* List that holds elements rejected during current pass. */
final IList<E> rejected = new FunctionalList<>();
final GroupPartIteration<E> it = new GroupPartIteration<>(returned, rejected, partitionSize, counter);
- /*
- * Run up to a certain number of passes
- */
+ /* Run up to a certain number of passes. */
for (int numberOfIterations = 0; numberOfIterations < MAX_NTRIESPART
&& !rejected.isEmpty(); numberOfIterations++) {
input.forEach(it);
if (rejected.isEmpty()) {
- /*
- * Nothing was rejected, so we're done
- */
+ /* Nothing was rejected, so we're done. */
return returned;
}
}
@@ -208,13 +212,16 @@ public class ListUtils {
}
/**
- * Merge the contents of a bunch of lists together into a single list
+ * Merge the contents of a bunch of lists together into a single list.
*
* @param <E>
- * The type of value in this lists
+ * The type of value in this lists.
+ *
* @param lists
- * The values in the lists to merge
- * @return A list containing all the elements of the lists
+ * The values in the lists to merge.
+ *
+ * @return
+ * A list containing all the elements of the lists.
*/
@SafeVarargs
public static <E> IList<E> mergeLists(final IList<E>... lists) {
@@ -230,21 +237,28 @@ public class ListUtils {
}
/**
- * Pad the provided list out to the desired size
+ * Pad the provided list out to the desired size.
*
* @param <E>
- * The type of elements in the list
+ * The type of elements in the list.
+ *
* @param list
- * The list to pad out
+ * The list to pad out.
+ *
* @param counter
- * The function to count elements with
+ * The function to count elements with.
+ *
* @param size
- * The desired size of the list
+ * The desired size of the list.
+ *
* @param padder
- * The function to get elements to pad with
- * @return The list, padded to the desired size
+ * The function to get elements to pad with.
+ *
+ * @return
+ * The list, padded to the desired size.
+ *
* @throws IllegalArgumentException
- * if the list couldn't be padded to the desired size
+ * If the list couldn't be padded to the desired size.
*/
public static <E> IList<E> padList(final IList<E> list, final Function<E, Integer> counter, final int size,
final Supplier<E> padder) {
@@ -259,9 +273,7 @@ public class ListUtils {
}
if (count % size != 0) {
- /*
- * We need to pad
- */
+ /* We need to pad */
int needed = count % size;
int threshold = 0;
diff --git a/base/src/main/java/bjc/utils/funcutils/NumberUtils.java b/base/src/main/java/bjc/utils/funcutils/NumberUtils.java
index 770d3a5..c3d52fa 100644
--- a/base/src/main/java/bjc/utils/funcutils/NumberUtils.java
+++ b/base/src/main/java/bjc/utils/funcutils/NumberUtils.java
@@ -3,27 +3,29 @@ package bjc.utils.funcutils;
import java.util.function.Function;
/**
- * Utility functions for dealing with numbers
+ * Utility functions for dealing with numbers.
*
* @author ben
- *
*/
public class NumberUtils {
/**
- * Compute the falling factorial of a number
+ * Compute the falling factorial of a number.
*
* @param value
- * The number to compute
+ * The number to compute.
+ *
* @param power
- * The power to do the falling factorial for
- * @return The falling factorial of the number to the power
+ * The power to do the falling factorial for.
+ *
+ * @return
+ * The falling factorial of the number to the power.
*/
public static int fallingFactorial(final int value, final int power) {
- if (power == 0)
+ if (power == 0) {
return 1;
- else if (power == 1)
+ } else if (power == 1) {
return value;
- else {
+ } else {
int result = 1;
for (int currentSub = 0; currentSub < power + 1; currentSub++) {
@@ -35,15 +37,18 @@ public class NumberUtils {
}
/**
- * Evaluates a linear probability distribution
+ * Evaluates a linear probability distribution.
*
* @param winning
- * The number of winning possibilities
+ * The number of winning possibilities.
+ *
* @param total
- * The number of total possibilities
+ * The number of total possibilities.
+ *
* @param rng
- * The function to use to generate a random possibility
- * @return Whether or not a random possibility was a winning one
+ * The function to use to generate a random possibility.
+ *
+ * @return Whether or not a random possibility was a winning one.
*/
public static boolean isProbable(final int winning, final int total, final Function<Integer, Integer> rng) {
return rng.apply(total) < winning;
@@ -53,15 +58,16 @@ public class NumberUtils {
* Check if a number is in an inclusive range.
*
* @param min
- * The minimum value of the range.
+ * The minimum value of the range.
*
* @param max
- * The maximum value of the range.
+ * The maximum value of the range.
*
* @param i
- * The number to check.
+ * The number to check.
*
- * @return Whether the number is in the range.
+ * @return
+ * Whether the number is in the range.
*/
public static boolean between(final int min, final int max, final int i) {
return i >= min && i <= max;
diff --git a/base/src/main/java/bjc/utils/funcutils/StringUtils.java b/base/src/main/java/bjc/utils/funcutils/StringUtils.java
index 62f78f5..ba4ebca 100644
--- a/base/src/main/java/bjc/utils/funcutils/StringUtils.java
+++ b/base/src/main/java/bjc/utils/funcutils/StringUtils.java
@@ -7,23 +7,24 @@ import java.util.regex.Pattern;
import com.ibm.icu.text.BreakIterator;
/**
- * Utility methods for operations on strings
+ * Utility methods for operations on strings.
*
* @author ben
- *
*/
public class StringUtils {
/**
* Check if a string consists only of one or more matches of a regular
- * expression
+ * expression.
*
* @param input
- * The string to check
+ * The string to check.
+ *
* @param rRegex
- * The regex to see if the string only contains matches
- * of
- * @return Whether or not the string consists only of multiple matches
- * of the provided regex
+ * The regex to see if the string only contains matches of.
+ *
+ * @return
+ * Whether or not the string consists only of multiple matches of
+ * the provided regex.
*/
public static boolean containsOnly(final String input, final String rRegex) {
if (input == null)
@@ -36,18 +37,19 @@ public class StringUtils {
* First, we match the beginning of the string. Then, we start a
* non-capturing group whose contents are the passed in regex.
* That group is then matched one or more times and the pattern
- * matches to the end of the string
+ * matches to the end of the string.
*/
return input.matches("\\A(?:" + rRegex + ")+\\Z");
}
/**
- * Indent the string being built in a StringBuilder n levels
+ * Indent the string being built in a StringBuilder n levels.
*
* @param builder
- * The builder to indent in
+ * The builder to indent in.
+ *
* @param levels
- * The number of levels to indent
+ * The number of levels to indent.
*/
public static void indentNLevels(final StringBuilder builder, final int levels) {
for (int i = 0; i < levels; i++) {
@@ -57,14 +59,17 @@ public class StringUtils {
/**
* Print out a deque with a special case for easily showing a deque is
- * empty
+ * empty.
*
* @param <ContainedType>
- * The type in the deque
+ * The type in the deque.
+ *
* @param queue
- * The deque to print
- * @return A string version of the deque, with allowance for an empty
- * deque
+ * The deque to print.
+ *
+ * @return
+ * A string version of the deque, with allowance for an empty
+ * deque.
*/
public static <ContainedType> String printDeque(final Deque<ContainedType> queue) {
return queue.isEmpty() ? "(none)" : queue.toString();
@@ -74,14 +79,16 @@ public class StringUtils {
* Converts a sequence to an English list.
*
* @param objects
- * The sequence to convert to an English list.
+ * The sequence to convert to an English list.
+ *
* @param join
- * The string to use for separating the last element from
- * the rest.
+ * The string to use for separating the last element from the rest.
+ *
* @param comma
- * The string to use as a comma
+ * The string to use as a comma
*
- * @return The sequence as an English list.
+ * @return
+ * The sequence as an English list.
*/
public static String toEnglishList(final Object[] objects, final String join, final String comma) {
if (objects == null) throw new NullPointerException("Sequence must not be null");
@@ -93,32 +100,25 @@ public class StringUtils {
switch (objects.length) {
case 0:
- /*
- * Empty list.
- */
+ /* Empty list. */
break;
case 1:
- /*
- * One item.
- */
+ /* One item. */
sb.append(objects[0].toString());
break;
case 2:
- /*
- * Two items.
- */
+ /* Two items. */
sb.append(objects[0].toString());
sb.append(" " + joiner + " ");
sb.append(objects[1].toString());
break;
default:
- /*
- * Three or more items.
- */
+ /* Three or more items. */
for (int i = 0; i < objects.length - 1; i++) {
sb.append(objects[i].toString());
sb.append(coma + " ");
}
+
/*
* Uncomment this to remove serial commas.
*
@@ -137,12 +137,13 @@ public class StringUtils {
* Converts a sequence to an English list.
*
* @param objects
- * The sequence to convert to an English list.
+ * The sequence to convert to an English list.
+ *
* @param join
- * The string to use for separating the last element from
- * the rest.
+ * The string to use for separating the last element from the rest.
*
- * @return The sequence as an English list.
+ * @return
+ * The sequence as an English list.
*/
public static String toEnglishList(final Object[] objects, final String join) {
return toEnglishList(objects, join, ",");
@@ -152,25 +153,30 @@ public class StringUtils {
* Converts a sequence to an English list.
*
* @param objects
- * The sequence to convert to an English list.
+ * The sequence to convert to an English list.
+ *
* @param and
- * Whether to use 'and' or 'or'.
+ * Whether to use 'and' or 'or'.
*
- * @return The sequence as an English list.
+ * @return
+ * The sequence as an English list.
*/
public static String toEnglishList(final Object[] objects, final boolean and) {
- if (and)
+ if (and) {
return toEnglishList(objects, "and");
- else return toEnglishList(objects, "or");
+ } else {
+ return toEnglishList(objects, "or");
+ }
}
/**
* Count the number of graphemes in a string.
*
* @param value
- * The string to check.
+ * The string to check.
*
- * @return The number of graphemes in the string.
+ * @return
+ * The number of graphemes in the string.
*/
public static int graphemeCount(final String value) {
final BreakIterator it = BreakIterator.getCharacterInstance();
@@ -184,6 +190,15 @@ public class StringUtils {
return count;
}
+ /**
+ * Count the number of times a pattern matches in a given string.
+ *
+ * @param value
+ * The string to count occurances in.
+ *
+ * @param pattern
+ * The pattern to count occurances of.
+ */
public static int countMatches(final String value, final String pattern) {
Matcher mat = Pattern.compile(pattern).matcher(value);
diff --git a/base/src/main/java/bjc/utils/funcutils/TreeUtils.java b/base/src/main/java/bjc/utils/funcutils/TreeUtils.java
index dcd5738..3308fb8 100644
--- a/base/src/main/java/bjc/utils/funcutils/TreeUtils.java
+++ b/base/src/main/java/bjc/utils/funcutils/TreeUtils.java
@@ -28,11 +28,10 @@ public class TreeUtils {
return paths;
}
+ /* Find a path in a tree. */
private static <T> void findPath(ITree<T> subtree, LinkedList<T> path, Predicate<T> leafMarker, IList<IList<T>> paths) {
if(subtree.getChildrenCount() == 0 && leafMarker.test(subtree.getHead())) {
- /*
- * We're at a matching leaf node. Add it.
- */
+ /* We're at a matching leaf node. Add it. */
IList<T> finalPath = new FunctionalList<>();
for(T ePath : path) {
@@ -43,9 +42,7 @@ public class TreeUtils {
paths.add(finalPath);
} else {
- /*
- * Check the children of this node.
- */
+ /* Check the children of this node. */
path.add(subtree.getHead());
subtree.doForChildren((child) -> findPath(child, path, leafMarker, paths));
diff --git a/base/src/main/java/bjc/utils/funcutils/TriConsumer.java b/base/src/main/java/bjc/utils/funcutils/TriConsumer.java
index f30386c..a41cde0 100644
--- a/base/src/main/java/bjc/utils/funcutils/TriConsumer.java
+++ b/base/src/main/java/bjc/utils/funcutils/TriConsumer.java
@@ -6,12 +6,13 @@ package bjc.utils.funcutils;
* @author EVE
*
* @param <A>
- * Type of the first argument.
+ * Type of the first argument.
+ *
* @param <B>
- * Type of the second argument.
- * @param <C>
- * Type of the third argument.
+ * Type of the second argument.
*
+ * @param <C>
+ * Type of the third argument.
*/
@FunctionalInterface
public interface TriConsumer<A, B, C> {
@@ -19,13 +20,13 @@ public interface TriConsumer<A, B, C> {
* Perform the action.
*
* @param a
- * The first parameter.
+ * The first parameter.
*
* @param b
- * The second parameter.
+ * The second parameter.
*
* @param c
- * The third parameter.
+ * The third parameter.
*/
public void accept(A a, B b, C c);
}