diff options
Diffstat (limited to 'base/src/main/java/bjc/utils/funcutils')
| -rw-r--r-- | base/src/main/java/bjc/utils/funcutils/FileUtils.java | 20 | ||||
| -rw-r--r-- | base/src/main/java/bjc/utils/funcutils/FuncUtils.java | 43 | ||||
| -rw-r--r-- | base/src/main/java/bjc/utils/funcutils/TestUtils.java | 44 |
3 files changed, 87 insertions, 20 deletions
diff --git a/base/src/main/java/bjc/utils/funcutils/FileUtils.java b/base/src/main/java/bjc/utils/funcutils/FileUtils.java index 0fd3db0..04ac6b0 100644 --- a/base/src/main/java/bjc/utils/funcutils/FileUtils.java +++ b/base/src/main/java/bjc/utils/funcutils/FileUtils.java @@ -13,26 +13,28 @@ import java.util.function.BiPredicate; */ public class FileUtils { /* - * @NOTE If it becomes necessary, write another overload for this with - * all the buttons and knobs from walkFileTree. + * @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. * * @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. * */ public static void traverseDirectory(final Path root, final BiPredicate<Path, BasicFileAttributes> predicate, diff --git a/base/src/main/java/bjc/utils/funcutils/FuncUtils.java b/base/src/main/java/bjc/utils/funcutils/FuncUtils.java index 2e55a3d..ff9fefb 100644 --- a/base/src/main/java/bjc/utils/funcutils/FuncUtils.java +++ b/base/src/main/java/bjc/utils/funcutils/FuncUtils.java @@ -1,6 +1,7 @@ package bjc.utils.funcutils; import java.util.function.BiFunction; +import java.util.function.BiPredicate; import java.util.function.Consumer; import java.util.function.Function; import java.util.function.UnaryOperator; @@ -16,16 +17,16 @@ public class FuncUtils { * 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. + * The function to transform. * * @return The function transformed into a unary function returning a * function. @@ -40,13 +41,13 @@ public class FuncUtils { * 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++) { + for (int i = 0; i < nTimes; i++) { cons.accept(i); } } @@ -55,15 +56,35 @@ public class FuncUtils { * Return an operator that executes until it converges. * * @param op - * The operator to execute. + * 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. * * @return The requested operator. */ public static <T> UnaryOperator<T> converge(final UnaryOperator<T> op, final int maxTries) { + return converge(op, (nw, old) -> nw.equals(old), maxTries); + } + + /** + * Return an operator that executes until it converges. + * + * @param op + * The operator to execute. + * @param converged + * The predicate to execute to check if the function has + * converged. + * + * @param maxTries + * The maximum amount of times to apply the function in + * an attempt to cause it to converge. + * + * @return The requested operator. + */ + public static <T> UnaryOperator<T> converge(final UnaryOperator<T> op, final BiPredicate<T, T> converged, + final int maxTries) { return (val) -> { T newVal = op.apply(val); T oldVal; @@ -75,7 +96,7 @@ public class FuncUtils { newVal = op.apply(newVal); tries += 1; - } while(!newVal.equals(oldVal) && tries < maxTries); + } while (!converged.test(newVal, oldVal) && tries < maxTries); return newVal; }; diff --git a/base/src/main/java/bjc/utils/funcutils/TestUtils.java b/base/src/main/java/bjc/utils/funcutils/TestUtils.java new file mode 100644 index 0000000..df44e7a --- /dev/null +++ b/base/src/main/java/bjc/utils/funcutils/TestUtils.java @@ -0,0 +1,44 @@ +package bjc.utils.funcutils; + +import static org.junit.Assert.assertEquals; + +import java.util.Collection; +import java.util.Iterator; + +/** + * Utilities for testing. + * + * @author bjculkin + * + */ +public class TestUtils { + /** + * Assert an iterator provides a particular sequence of values. + * + * @param src + * The iterator to pull values from. + * @param vals + * The values to expect from the iterator. + */ + 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. + * + * @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. + */ + public static <T> void assertIteratorEquals(Iterator<T> src, boolean hasMore, T... vals) { + assertIteratorEquals(src, vals); + + assertEquals(hasMore, src.hasNext()); + } +} |
