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/LambdaLock.java25
-rw-r--r--base/src/main/java/bjc/utils/funcutils/ListUtils.java178
-rw-r--r--base/src/main/java/bjc/utils/funcutils/SetUtils.java29
-rw-r--r--base/src/main/java/bjc/utils/funcutils/TestUtils.java34
-rw-r--r--base/src/main/java/bjc/utils/funcutils/TreeUtils.java32
5 files changed, 105 insertions, 193 deletions
diff --git a/base/src/main/java/bjc/utils/funcutils/LambdaLock.java b/base/src/main/java/bjc/utils/funcutils/LambdaLock.java
index c7ba09a..c974c8c 100644
--- a/base/src/main/java/bjc/utils/funcutils/LambdaLock.java
+++ b/base/src/main/java/bjc/utils/funcutils/LambdaLock.java
@@ -5,12 +5,10 @@ import java.util.concurrent.locks.ReadWriteLock;
import java.util.concurrent.locks.ReentrantReadWriteLock;
import java.util.function.Supplier;
-/**
- * A wrapper around a {@link ReadWriteLock} to ensure that the lock is used
+/** A wrapper around a {@link ReadWriteLock} to ensure that the lock is used
* properly.
*
- * @author EVE
- */
+ * @author EVE */
public class LambdaLock {
/* The read lock. */
private final Lock readLock;
@@ -22,26 +20,21 @@ public class LambdaLock {
this(new ReentrantReadWriteLock());
}
- /**
- * Create a new lambda-enabled lock.
+ /** Create a new lambda-enabled lock.
*
- * @param lck
- * The lock to wrap.
- */
+ * @param lck The lock to wrap. */
public LambdaLock(final ReadWriteLock lck) {
readLock = lck.readLock();
writeLock = lck.writeLock();
}
- /**
- * Execute an action with the read lock taken.
+ /** Execute an action with the read lock taken.
*
* @param <T> The type returned by the action.
*
* @param supp 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();
@@ -59,8 +52,7 @@ public class LambdaLock {
*
* @param supp 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 +66,7 @@ public class LambdaLock {
/**
* Execute an action with the read lock taken.
*
- * @param action
- * The action to call.
+ * @param action The action to call.
*/
public void read(final Runnable action) {
readLock.lock();
diff --git a/base/src/main/java/bjc/utils/funcutils/ListUtils.java b/base/src/main/java/bjc/utils/funcutils/ListUtils.java
index ab32ccc..47141c2 100644
--- a/base/src/main/java/bjc/utils/funcutils/ListUtils.java
+++ b/base/src/main/java/bjc/utils/funcutils/ListUtils.java
@@ -6,42 +6,32 @@ import java.util.function.*;
import bjc.funcdata.FunctionalList;
import bjc.funcdata.ListEx;
-/**
- * Utilities for manipulating FunctionalLists and regular Collections lists that
+/** Utilities for manipulating FunctionalLists and regular Collections lists that
* don't belong in the class itself.
*
- * @author ben
- */
+ * @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.
+ /** Collapse a string of tokens into a single string without adding any spaces.
*
- * @param input
- * The list of tokens to collapse.
+ * @param input The list of tokens to collapse.
*
- * @return The collapsed string of tokens.
- */
+ * @return The collapsed string of tokens. */
public static String collapseTokens(final ListEx<String> input) {
if (input == null) throw new NullPointerException("Input must not be null");
return collapseTokens(input, "");
}
- /**
- * Collapse a string of tokens into a single string, adding the desired
+ /** Collapse a string of tokens into a single string, adding the desired
* separator after each token.
*
- * @param input
- * The list of tokens to collapse.
- *
- * @param seperator
- * The separator to use for separating tokens.
+ * @param input The list of tokens to collapse.
+ * @param seperator The separator to use for separating tokens.
*
- * @return The collapsed string of tokens.
- */
+ * @return The collapsed string of tokens. */
public static String collapseTokens(final ListEx<String> input,
final String seperator) {
if (input == null) throw new NullPointerException("Input must not be null");
@@ -67,25 +57,17 @@ public class ListUtils {
}
}
- /**
- * Select a number of random items from the list without replacement.
- *
- * @param <E>
- * The type of items to select.
+ /** Select a number of random items from the list without replacement.
*
- * @param list
- * The list to select from.
+ * @param <E> The type of items to select.
*
- * @param number
- * The number of items to selet.
- *
- * @param rng
- * A function that creates a random number from 0 to the desired
+ * @param list The list to select from.
+ * @param number 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.
- */
+ * from the specified list without replacement. */
public static <E> ListEx<E> drawWithoutReplacement(final ListEx<E> list,
final int number, final Function<Integer, Integer> rng) {
final ListEx<E> selected = new FunctionalList<>(new ArrayList<>(number));
@@ -111,25 +93,17 @@ public class ListUtils {
return selected;
}
- /**
- * Select a number of random items from the list, with replacement.
- *
- * @param <E>
- * The type of items to select.
+ /** Select a number of random items from the list, with replacement.
*
- * @param list
- * The list to select from.
+ * @param <E> The type of items to select.
*
- * @param number
- * The number of items to select.
- *
- * @param rng
- * A function that creates a random number from 0 to the desired
+ * @param list The list to select from.
+ * @param number 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.
- */
+ * from the specified list. */
public static <E> ListEx<E> drawWithReplacement(final ListEx<E> list, final int number,
final Function<Integer, Integer> rng) {
final ListEx<E> selected = new FunctionalList<>(new ArrayList<>(number));
@@ -139,25 +113,16 @@ public class ListUtils {
return selected;
}
- /**
- * Partition a list into a list of lists, where each element can count for more
+ /** Partition a list into a list of lists, where each element can count for more
* than one element in a partition.
*
- * @param <E>
- * The type of elements in the list to partition.
- *
- * @param input
- * The list to partition.
+ * @param <E> The type of elements in the list to partition.
*
- * @param counter
- * The function to determine the count for each element
- * for.
+ * @param input The list to partition.
+ * @param counter The function to determine the count for each element for.
+ * @param partitionSize The number of elements to put in each partition.
*
- * @param partitionSize
- * 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> ListEx<ListEx<E>> groupPartition(final ListEx<E> input,
final Function<E, Integer> counter, final int partitionSize) {
if (input == null) {
@@ -200,17 +165,13 @@ public class ListUtils {
throw new IllegalArgumentException(msg);
}
- /**
- * 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.
+ * @param <E> The type of value in this lists.
*
- * @param lists
- * The values in the lists to merge.
+ * @param lists The values in the lists to merge.
*
- * @return A list containing all the elements of the lists.
- */
+ * @return A list containing all the elements of the lists. */
@SafeVarargs
public static <E> ListEx<E> mergeLists(final ListEx<E>... lists) {
final ListEx<E> returned = new FunctionalList<>();
@@ -222,30 +183,19 @@ public class ListUtils {
return returned;
}
- /**
- * Pad the provided list out to the desired size.
- *
- * @param <E>
- * The type of elements in the list.
+ /** Pad the provided list out to the desired size.
*
- * @param list
- * The list to pad out.
+ * @param <E> The type of elements in the list.
*
- * @param counter
- * The function to count elements with.
- *
- * @param size
- * The desired size of the list.
- *
- * @param padder
- * The function to get elements to pad with.
+ * @param list The list to pad out.
+ * @param counter The function to count elements with.
+ * @param size The desired size of the list.
+ * @param padder 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.
- */
+ * @throws IllegalArgumentException If the list couldn't be padded to the
+ * desired size. */
public static <E> ListEx<E> padList(final ListEx<E> list,
final Function<E, Integer> counter, final int size,
final Supplier<E> padder) {
@@ -293,13 +243,11 @@ public class ListUtils {
return returned;
}
- /**
- * Convert a list of longs into an array of longs.
+ /** Convert a list of longs into an array of longs.
*
- * @param list
- * The list to convert.
- * @return The list as an array.
- */
+ * @param list The list to convert.
+ *
+ * @return The list as an array. */
public static long[] toPrimitive(List<Long> list) {
long[] res = new long[list.size()];
@@ -313,16 +261,15 @@ public class ListUtils {
return res;
}
- /**
- * Generate all of the permuations of a list.
+ /** Generate all of the permuations of a list.
*
* This is a version of Algorith P (Plain Changes) from Knuth (vol 4A, pg 322)
*
- * @param <T> The type of elements in the list.
- *
+ * @param <T> The type of element in the list.
+ *
* @param list The list to generate permutations from.
- * @return The list of permutations of the list.
- */
+ *
+ * @return The list of permutations of the list. */
public static <T> List<List<T>> permuteList(List<T> list) {
List<List<T>> permutes = new LinkedList<>();
@@ -410,14 +357,11 @@ public class ListUtils {
}
}
-/**
- * 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
- */
+ * @param <E> The type of element in the list being partitioned */
class GroupPartIteration<E> implements Consumer<E> {
/* The list we're returning. */
private final ListEx<ListEx<E>> returnedList;
@@ -435,23 +379,14 @@ class GroupPartIteration<E> implements Consumer<E> {
/* 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.
+ /** Create a new group partitioning iteration.
*
- * @param rejects
- * The items that have been rejected from a partition for being
+ * @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
+ * @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.
- */
+ * @param eleCount The function to use to determine the value of an item. */
public GroupPartIteration(final ListEx<ListEx<E>> returned, final ListEx<E> rejects,
final int nPerPart, final Function<E, Integer> eleCount) {
this.returnedList = returned;
@@ -465,8 +400,7 @@ class GroupPartIteration<E> implements Consumer<E> {
@Override
public void accept(final E value) {
- final boolean shouldStartPartition
- = numberInCurrentPartition >= numberPerPartition;
+ final boolean shouldStartPartition = numberInCurrentPartition >= numberPerPartition;
if (shouldStartPartition) {
returnedList.add(currentPartition);
diff --git a/base/src/main/java/bjc/utils/funcutils/SetUtils.java b/base/src/main/java/bjc/utils/funcutils/SetUtils.java
index b721d10..799eadc 100644
--- a/base/src/main/java/bjc/utils/funcutils/SetUtils.java
+++ b/base/src/main/java/bjc/utils/funcutils/SetUtils.java
@@ -5,20 +5,17 @@ import java.util.HashSet;
import java.util.List;
import java.util.Set;
-/**
- * Various utility functions dealing with sets.
+/** Various utility functions dealing with sets.
*
- * @author bjculkin
- *
- */
+ * @author bjculkin */
public class SetUtils {
- /**
- * Create a power-set (set of all subsets) of a given set.
+ /** Create a power-set (set of all subsets) of a given set.
*
- * @param <T> The type of elements contained in the set.
+ * @param <T> The type of element in the set.
+ *
* @param originalSet The set to create a power-set of.
- * @return The power-set of the set.
- */
+ *
+ * @return The power-set of the set. */
public static <T> Set<Set<T>> powerSet(Set<T> originalSet) {
Set<Set<T>> sets = new HashSet<>();
@@ -52,19 +49,17 @@ public class SetUtils {
return sets;
}
- /**
- * Utility method for set construction.
+ /** Utility method for set construction.
*
- * @param <T> The type of elements in the set.
+ * @param <T> The type of element in the set.
+ *
* @param elms The elements to stick in the set.
- * @return A set containing the specified elements.
- */
+ *
+ * @return A set containing the specified elements. */
@SafeVarargs
public static <T> Set<T> toSet(T... elms) {
Set<T> set = new HashSet<>();
-
for (T elm : elms) set.add(elm);
-
return set;
}
}
diff --git a/base/src/main/java/bjc/utils/funcutils/TestUtils.java b/base/src/main/java/bjc/utils/funcutils/TestUtils.java
index bf38cbe..860a565 100644
--- a/base/src/main/java/bjc/utils/funcutils/TestUtils.java
+++ b/base/src/main/java/bjc/utils/funcutils/TestUtils.java
@@ -5,33 +5,28 @@ import static org.junit.Assert.assertEquals;
import java.util.Iterator;
import java.util.List;
-/**
- * Utilities for testing.
+/** Utilities for testing.
*
- * @author bjculkin
- *
- */
+ * @author bjculkin */
public class TestUtils {
- /**
- * Assert an iterator provides a particular sequence of values.
+ /** Assert an iterator provides a particular sequence of values.
+ *
+ * @param <T> The type of value.
*
- * @param <T> The type of the values.
* @param src The iterator to pull values from.
- * @param vals The values to expect from the iterator.
- */
+ * @param vals The values to expect from the iterator. */
@SafeVarargs
public static <T> void assertIteratorEquals(Iterator<T> src, T... vals) {
for (T val : vals) assertEquals(val, src.next());
}
- /**
- * Assert an iterator provides a particular sequence of values.
+ /** Assert an iterator provides a particular sequence of values.
+ *
+ * @param <T> The type of value.
*
- * @param <T> The type of the values.
* @param src The iterator to pull values from.
* @param hasMore The expected value of hasNext for the iterator.
- * @param vals The values to expect from the iterator.
- */
+ * @param vals The values to expect from the iterator. */
@SafeVarargs
public static <T> void assertIteratorEquals(boolean hasMore, Iterator<T> src,
T... vals) {
@@ -49,13 +44,12 @@ public class TestUtils {
assertEquals(hasMore, src.hasNext());
}
- /**
- * Assert that a list has a given set of contents.
+ /** Assert that a list has a given set of contents.
+ *
+ * @param <T> The type of value in the list.
*
- * @param <T> The type of the values.
* @param src The list of actual elements.
- * @param exps The list of expected elements.
- */
+ * @param exps The list of expected elements. */
@SafeVarargs
public static <T> void assertListEquals(List<T> src, T... exps) {
assertEquals(exps.length, src.size());
diff --git a/base/src/main/java/bjc/utils/funcutils/TreeUtils.java b/base/src/main/java/bjc/utils/funcutils/TreeUtils.java
index 5a1d9c8..daab8a1 100644
--- a/base/src/main/java/bjc/utils/funcutils/TreeUtils.java
+++ b/base/src/main/java/bjc/utils/funcutils/TreeUtils.java
@@ -6,20 +6,18 @@ import java.util.function.*;
import bjc.data.*;
import bjc.funcdata.*;
-/**
- * Implements various utilities for trees.
+/** Implements various utilities for trees.
*
- * @author Benjamin Culkin
- */
+ * @author Benjamin Culkin */
public class TreeUtils {
- /**
- * Convert a tree into a list of outline nodes that match a certain path.
+ /** Convert a tree into a list of outline nodes that match a certain path.
+ *
+ * @param <T> The type contained in the tree.
*
- * @param <T> The type of the values.
* @param tre The tree to outline.
* @param leafMarker The path to mark nodes with.
- * @return The list of marked paths.
- */
+ *
+ * @return The list of marked paths. */
public static <T> ListEx<ListEx<T>> outlineTree(Tree<T> tre, Predicate<T> leafMarker) {
ListEx<ListEx<T>> paths = new FunctionalList<>();
@@ -53,15 +51,15 @@ public class TreeUtils {
}
}
- /**
- * Performs 'variable substitution' or something along those lines on a tree.
+ /** Performs 'variable substitution' or something along those lines on a tree.
*
* @param <ContainedType> The type of element contained in the tree.
+ *
* @param tree The tree to do expansion in.
* @param marker The function to mark which nodes should be expanded.
* @param expander The function to expand nodes.
- * @return A transformed copy of the tree.
- */
+ *
+ * @return A transformed copy of the tree. */
public static <ContainedType> Tree<ContainedType> substitute(
Tree<ContainedType> tree,
Predicate<ContainedType> marker,
@@ -75,14 +73,14 @@ public class TreeUtils {
return tree;
}
- /**
- * Performs 'variable substitution' or something along those lines on a tree.
+ /** Performs 'variable substitution' or something along those lines on a tree.
*
* @param <ContainedType> The type of element contained in the tree.
+ *
* @param tree The tree to do expansion in.
* @param environment A map which contains the variables to substitute.
- * @return A transformed copy of the tree.
- */
+ *
+ * @return A transformed copy of the tree. */
public static <ContainedType> Tree<ContainedType> substitute(
Tree<ContainedType> tree,
MapEx<ContainedType, Tree<ContainedType>> environment) {