diff options
| author | bculkin2442 <bjculkin@mix.wvu.edu> | 2017-02-09 11:50:31 -0500 |
|---|---|---|
| committer | bculkin2442 <bjculkin@mix.wvu.edu> | 2017-02-09 11:50:31 -0500 |
| commit | d2af58b0f68ebfbba2be7e7679efec6c8c0af12f (patch) | |
| tree | 2b16fbf014db350126e8c1b5f081312276f85f62 /BJC-Utils2/src/main/java/bjc/utils/funcutils | |
| parent | 187e1815488e3c1ed22e7592f304e632cffefb82 (diff) | |
Update
Diffstat (limited to 'BJC-Utils2/src/main/java/bjc/utils/funcutils')
11 files changed, 157 insertions, 184 deletions
diff --git a/BJC-Utils2/src/main/java/bjc/utils/funcutils/CollectorUtils.java b/BJC-Utils2/src/main/java/bjc/utils/funcutils/CollectorUtils.java index 0a1efd5..eb3695e 100644 --- a/BJC-Utils2/src/main/java/bjc/utils/funcutils/CollectorUtils.java +++ b/BJC-Utils2/src/main/java/bjc/utils/funcutils/CollectorUtils.java @@ -25,20 +25,17 @@ public class CollectorUtils { * The final type of the first collector * @param <FinalType2> * The final type of the second collector - * @param firstCollector + * @param first * The first collector to use - * @param secondCollector + * @param second * 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( - Collector<InitialType, AuxType1, - FinalType1> firstCollector, - Collector<InitialType, AuxType2, - FinalType2> secondCollector) { - return new CompoundCollector<>(firstCollector, secondCollector); + public static <InitialType, AuxType1, AuxType2, FinalType1, FinalType2> + Collector<InitialType, IHolder<IPair<AuxType1, AuxType2>>, IPair<FinalType1, FinalType2>> + compoundCollect( + Collector<InitialType, AuxType1, FinalType1> first, + Collector<InitialType, AuxType2, FinalType2> second) { + return new CompoundCollector<>(first, second); } } diff --git a/BJC-Utils2/src/main/java/bjc/utils/funcutils/CompoundCollector.java b/BJC-Utils2/src/main/java/bjc/utils/funcutils/CompoundCollector.java index 257f005..de5fe85 100644 --- a/BJC-Utils2/src/main/java/bjc/utils/funcutils/CompoundCollector.java +++ b/BJC-Utils2/src/main/java/bjc/utils/funcutils/CompoundCollector.java @@ -12,77 +12,66 @@ import bjc.utils.data.IPair; import bjc.utils.data.Identity; import bjc.utils.data.Pair; -final class CompoundCollector<InitialType, AuxType1, AuxType2, FinalType1, - FinalType2> implements - Collector<InitialType, IHolder<IPair<AuxType1, AuxType2>>, - IPair<FinalType1, FinalType2>> { - private Set< - java.util.stream.Collector.Characteristics> characteristicSet; +final class CompoundCollector<InitialType, AuxType1, AuxType2, FinalType1, FinalType2> + implements Collector<InitialType, IHolder<IPair<AuxType1, AuxType2>>, IPair<FinalType1, FinalType2>> { - private Collector<InitialType, AuxType1, FinalType1> firstCollector; - private Collector<InitialType, AuxType2, - FinalType2> secondCollector; + private Set<java.util.stream.Collector.Characteristics> characteristicSet; + + private Collector<InitialType, AuxType1, FinalType1> first; + private Collector<InitialType, AuxType2, FinalType2> second; public CompoundCollector( - Collector<InitialType, AuxType1, FinalType1> firstCollector, - Collector<InitialType, AuxType2, FinalType2> secondCollector) { - this.firstCollector = firstCollector; - this.secondCollector = secondCollector; + Collector<InitialType, AuxType1, FinalType1> first, + Collector<InitialType, AuxType2, FinalType2> second) { + this.first = first; + this.second = second; - characteristicSet = firstCollector.characteristics(); - characteristicSet.addAll(secondCollector.characteristics()); + characteristicSet = first.characteristics(); + characteristicSet.addAll(second.characteristics()); } @Override - public BiConsumer<IHolder<IPair<AuxType1, AuxType2>>, - InitialType> accumulator() { - BiConsumer<AuxType1, InitialType> firstAccumulator = firstCollector - .accumulator(); - BiConsumer<AuxType2, - InitialType> secondAccumulator = secondCollector - .accumulator(); + public BiConsumer<IHolder<IPair<AuxType1, AuxType2>>, InitialType> accumulator() { + BiConsumer<AuxType1, InitialType> firstAccumulator = first.accumulator(); + BiConsumer<AuxType2, InitialType> secondAccumulator = second.accumulator(); return (state, value) -> { state.doWith((statePair) -> { - statePair.doWith((leftState, rightState) -> { - firstAccumulator.accept(leftState, value); - secondAccumulator.accept(rightState, value); + statePair.doWith((left, right) -> { + firstAccumulator.accept(left, value); + secondAccumulator.accept(right, value); }); }); }; } @Override - public Set< - java.util.stream.Collector.Characteristics> characteristics() { + public Set<java.util.stream.Collector.Characteristics> characteristics() { return characteristicSet; } @Override public BinaryOperator<IHolder<IPair<AuxType1, AuxType2>>> combiner() { - BinaryOperator<AuxType1> firstCombiner = firstCollector.combiner(); - BinaryOperator< - AuxType2> secondCombiner = secondCollector.combiner(); + BinaryOperator<AuxType1> firstCombiner = first.combiner(); + BinaryOperator<AuxType2> secondCombiner = second.combiner(); return (leftState, rightState) -> { return leftState.unwrap((leftPair) -> { return rightState.transform((rightPair) -> { - return leftPair.combine(rightPair, firstCombiner, - secondCombiner); + return leftPair.combine(rightPair, firstCombiner, secondCombiner); }); }); }; } @Override - public Function<IHolder<IPair<AuxType1, AuxType2>>, - IPair<FinalType1, FinalType2>> finisher() { + public Function<IHolder<IPair<AuxType1, AuxType2>>, IPair<FinalType1, FinalType2>> finisher() { return (state) -> { return state.unwrap((pair) -> { - return pair.bind((leftVal, rightVal) -> { + return pair.bind((left, right) -> { return new Pair<>( - firstCollector.finisher().apply(leftVal), - secondCollector.finisher().apply(rightVal)); + first.finisher().apply(left), + second.finisher().apply(right)); }); }); }; @@ -90,8 +79,10 @@ final class CompoundCollector<InitialType, AuxType1, AuxType2, FinalType1, @Override public Supplier<IHolder<IPair<AuxType1, AuxType2>>> supplier() { - return () -> new Identity<>( - new Pair<>(firstCollector.supplier().get(), - secondCollector.supplier().get())); + return () -> { + return new Identity<>(new Pair<>( + first.supplier().get(), + second.supplier().get())); + }; } -}
\ No newline at end of file +} diff --git a/BJC-Utils2/src/main/java/bjc/utils/funcutils/EnumUtils.java b/BJC-Utils2/src/main/java/bjc/utils/funcutils/EnumUtils.java index 61b13ea..9be6080 100644 --- a/BJC-Utils2/src/main/java/bjc/utils/funcutils/EnumUtils.java +++ b/BJC-Utils2/src/main/java/bjc/utils/funcutils/EnumUtils.java @@ -18,7 +18,7 @@ public class EnumUtils { * * @param <E> * The type of the enum - * @param enumClass + * @param clasz * The enum class * @param nValues * The number of values to execute the action on @@ -27,9 +27,9 @@ public class EnumUtils { * @param rnd * The source of randomness to use */ - public static <E extends Enum<E>> void doForValues(Class<E> enumClass, + public static <E extends Enum<E>> void doForValues(Class<E> clasz, int nValues, Consumer<E> action, Random rnd) { - E[] enumValues = enumClass.getEnumConstants(); + E[] enumValues = clasz.getEnumConstants(); IList<E> valueList = new FunctionalList<>(enumValues); @@ -49,15 +49,15 @@ public class EnumUtils { * * @param <E> * The type of the enum - * @param enumClass + * @param clasz * The class of the enum * @param rnd * The random source to use * @return A random value from the specified enum */ - public static <E extends Enum<E>> E getRandomValue(Class<E> enumClass, + public static <E extends Enum<E>> E getRandomValue(Class<E> clasz, Random rnd) { - E[] enumValues = enumClass.getEnumConstants(); + E[] enumValues = clasz.getEnumConstants(); return new FunctionalList<>(enumValues).randItem(rnd::nextInt); } diff --git a/BJC-Utils2/src/main/java/bjc/utils/funcutils/FileUtils.java b/BJC-Utils2/src/main/java/bjc/utils/funcutils/FileUtils.java index ea5c72e..6fc09ff 100644 --- a/BJC-Utils2/src/main/java/bjc/utils/funcutils/FileUtils.java +++ b/BJC-Utils2/src/main/java/bjc/utils/funcutils/FileUtils.java @@ -19,10 +19,10 @@ public class FileUtils { * * @param root * The directory to start the traversal at - * @param traversalPredicate + * @param predicate * The predicate to determine whether or not to traverse a * directory - * @param traversalAction + * @param action * The action to invoke upon each file in the directory. * Returning true means to continue the traversal, returning * false stops it @@ -33,10 +33,9 @@ public class FileUtils { * this with all the buttons and knobs from walkFileTree */ public static void traverseDirectory(Path root, - BiPredicate<Path, BasicFileAttributes> traversalPredicate, - BiPredicate<Path, BasicFileAttributes> traversalAction) + BiPredicate<Path, BasicFileAttributes> predicate, + BiPredicate<Path, BasicFileAttributes> action) throws IOException { - Files.walkFileTree(root, new FunctionalFileVisitor( - traversalPredicate, traversalAction)); + Files.walkFileTree(root, new FunctionalFileVisitor(predicate, action)); } } diff --git a/BJC-Utils2/src/main/java/bjc/utils/funcutils/FuncUtils.java b/BJC-Utils2/src/main/java/bjc/utils/funcutils/FuncUtils.java index 43603d6..679af52 100644 --- a/BJC-Utils2/src/main/java/bjc/utils/funcutils/FuncUtils.java +++ b/BJC-Utils2/src/main/java/bjc/utils/funcutils/FuncUtils.java @@ -26,8 +26,7 @@ public class FuncUtils { * @return The function transformed into a unary function returning a * function */ - public static <A, B, C> Function<A, Function<B, C>> curry2( - BiFunction<A, B, C> func) { + public static <A, B, C> Function<A, Function<B, C>> curry2(BiFunction<A, B, C> func) { return (arg1) -> (arg2) -> { return func.apply(arg1, arg2); }; diff --git a/BJC-Utils2/src/main/java/bjc/utils/funcutils/FunctionalFileVisitor.java b/BJC-Utils2/src/main/java/bjc/utils/funcutils/FunctionalFileVisitor.java index 2308d7c..cd833d9 100644 --- a/BJC-Utils2/src/main/java/bjc/utils/funcutils/FunctionalFileVisitor.java +++ b/BJC-Utils2/src/main/java/bjc/utils/funcutils/FunctionalFileVisitor.java @@ -8,20 +8,20 @@ import java.nio.file.attribute.BasicFileAttributes; import java.util.function.BiPredicate; final class FunctionalFileVisitor extends SimpleFileVisitor<Path> { - private BiPredicate<Path, BasicFileAttributes> traversalPredicate; - private BiPredicate<Path, BasicFileAttributes> traversalAction; + private BiPredicate<Path, BasicFileAttributes> predicate; + private BiPredicate<Path, BasicFileAttributes> action; public FunctionalFileVisitor( - BiPredicate<Path, BasicFileAttributes> traversalPredicate, - BiPredicate<Path, BasicFileAttributes> traversalAction) { - this.traversalPredicate = traversalPredicate; - this.traversalAction = traversalAction; + BiPredicate<Path, BasicFileAttributes> predicate, + BiPredicate<Path, BasicFileAttributes> action) { + this.predicate = predicate; + this.action = action; } @Override - public FileVisitResult preVisitDirectory(Path dir, - BasicFileAttributes attrs) throws IOException { - if (traversalPredicate.test(dir, attrs)) { + public FileVisitResult preVisitDirectory( + Path dir, BasicFileAttributes attrs) throws IOException { + if (predicate.test(dir, attrs)) { return FileVisitResult.CONTINUE; } @@ -31,10 +31,10 @@ final class FunctionalFileVisitor extends SimpleFileVisitor<Path> { @Override public FileVisitResult visitFile(Path file, BasicFileAttributes attrs) throws IOException { - if (traversalAction.test(file, attrs)) { + if (action.test(file, attrs)) { return FileVisitResult.CONTINUE; } return FileVisitResult.TERMINATE; } -}
\ No newline at end of file +} diff --git a/BJC-Utils2/src/main/java/bjc/utils/funcutils/GroupPartIteration.java b/BJC-Utils2/src/main/java/bjc/utils/funcutils/GroupPartIteration.java index 81781f6..229d271 100644 --- a/BJC-Utils2/src/main/java/bjc/utils/funcutils/GroupPartIteration.java +++ b/BJC-Utils2/src/main/java/bjc/utils/funcutils/GroupPartIteration.java @@ -17,10 +17,13 @@ import bjc.utils.funcdata.IList; */ final class GroupPartIteration<E> implements Consumer<E> { private IList<IList<E>> returnedList; + private IHolder<IList<E>> currentPartition; private IList<E> rejectedItems; + private IHolder<Integer> numberInCurrentPartition; private int numberPerPartition; + private Function<E, Integer> elementCounter; public GroupPartIteration(IList<IList<E>> returned, @@ -37,26 +40,24 @@ final class GroupPartIteration<E> implements Consumer<E> { @Override public void accept(E value) { - if (numberInCurrentPartition - .unwrap((number) -> number >= numberPerPartition)) { - returnedList.add( - currentPartition.unwrap((partition) -> partition)); + if (numberInCurrentPartition.unwrap((number) -> number >= numberPerPartition)) { + returnedList.add(currentPartition.unwrap((partition) -> partition)); - currentPartition - .transform((partition) -> new FunctionalList<>()); + currentPartition.transform((partition) -> new FunctionalList<>()); numberInCurrentPartition.transform((number) -> 0); } else { int currentElementCount = elementCounter.apply(value); - if (numberInCurrentPartition.unwrap((number) -> (number - + currentElementCount) >= numberPerPartition)) { + boolean shouldReject = numberInCurrentPartition.unwrap((number) -> { + return (number + currentElementCount) >= numberPerPartition; + }); + + if (shouldReject) { rejectedItems.add(value); } else { - currentPartition - .unwrap((partition) -> partition.add(value)); - numberInCurrentPartition.transform( - (number) -> number + currentElementCount); + currentPartition.unwrap((partition) -> partition.add(value)); + numberInCurrentPartition.transform((number) -> number + currentElementCount); } } } -}
\ No newline at end of file +} diff --git a/BJC-Utils2/src/main/java/bjc/utils/funcutils/ListUtils.java b/BJC-Utils2/src/main/java/bjc/utils/funcutils/ListUtils.java index 94571f5..4b17b41 100644 --- a/BJC-Utils2/src/main/java/bjc/utils/funcutils/ListUtils.java +++ b/BJC-Utils2/src/main/java/bjc/utils/funcutils/ListUtils.java @@ -47,8 +47,7 @@ public class ListUtils { * The seperator to use for seperating tokens * @return The collapsed string of tokens */ - public static String collapseTokens(IList<String> input, - String seperator) { + public static String collapseTokens(IList<String> input, String seperator) { if (input == null) { throw new NullPointerException("Input must not be null"); } else if (seperator == null) { @@ -60,12 +59,13 @@ public class ListUtils { } else if (input.getSize() == 1) { return input.first(); } else { - return input.reduceAux("", (currentString, state) -> { - return state + currentString + seperator; - }, (strang) -> { - return strang.substring(0, - strang.length() - seperator.length()); - }); + return input.reduceAux("", + (currentString, state) -> { + return state + currentString + seperator; + }, + (strang) -> { + return strang.substring(0, strang.length() - seperator.length()); + }); } } @@ -84,21 +84,20 @@ public class ListUtils { if (input == null) { throw new NullPointerException("Input must not be null"); } else if (operators == null) { - throw new NullPointerException( - "Set of operators must not be null"); + throw new NullPointerException("Set of operators must not be null"); } - IHolder<IList<String>> returnedList = new Identity<>(input); + IHolder<IList<String>> returned = new Identity<>(input); operators.forEach((operator) -> { - returnedList.transform((oldReturn) -> { - return oldReturn.flatMap((token) -> { + returned.transform((old) -> { + return old.flatMap((token) -> { return operator.merge(new TokenDeaffixer(token)); }); }); }); - return returnedList.unwrap((list) -> list); + return returned.unwrap((list) -> list); } /** @@ -108,7 +107,7 @@ public class ListUtils { * The type of items to select * @param list * The list to select from - * @param numberOfItems + * @param number * The number of items to selet * @param rng * A function that creates a random number from 0 to the @@ -117,26 +116,25 @@ public class ListUtils { * selected from the specified list without replacement */ - public static <E> IList<E> drawWithoutReplacement(IList<E> list, - int numberOfItems, Function<Integer, Integer> rng) { - IList<E> selectedItems = new FunctionalList<>( - new ArrayList<>(numberOfItems)); + public static <E> IList<E> drawWithoutReplacement( + IList<E> list, int number, Function<Integer, Integer> rng) { + IList<E> selected = new FunctionalList<>(new ArrayList<>(number)); - int totalItems = list.getSize(); + int total = list.getSize(); list.forEachIndexed((index, element) -> { - int winningChance = numberOfItems - selectedItems.getSize(); + int winningChance = number - selected.getSize(); // n - m - int totalChance = totalItems - (index - 1); + int totalChance = total - (index - 1); // N - t // Probability of selecting the t+1'th element if (NumberUtils.isProbable(winningChance, totalChance, rng)) { - selectedItems.add(element); + selected.add(element); } }); - return selectedItems; + return selected; } /** @@ -146,7 +144,7 @@ public class ListUtils { * The type of items to select * @param list * The list to select from - * @param numberOfItems + * @param number * The number of items to selet * @param rng * A function that creates a random number from 0 to the @@ -155,15 +153,14 @@ public class ListUtils { * selected from the specified list */ public static <E> IList<E> drawWithReplacement(IList<E> list, - int numberOfItems, Function<Integer, Integer> rng) { - IList<E> selectedItems = new FunctionalList<>( - new ArrayList<>(numberOfItems)); + int number, Function<Integer, Integer> rng) { + IList<E> selected = new FunctionalList<>(new ArrayList<>(number)); - for (int i = 0; i < numberOfItems; i++) { - selectedItems.add(list.randItem(rng)); + for (int i = 0; i < number; i++) { + selected.add(list.randItem(rng)); } - return selectedItems; + return selected; } /** @@ -175,23 +172,22 @@ public class ListUtils { * * @param input * The list to partition - * @param elementCounter + * @param counter * The function to determine the count for each element for - * @param numberPerPartition + * @param partitionSize * The number of elements to put in each partition * * @return A list partitioned according to the above rules */ - public static <E> IList<IList<E>> groupPartition(IList<E> input, - Function<E, Integer> elementCounter, int numberPerPartition) { + public static <E> IList<IList<E>> groupPartition( + IList<E> input, Function<E, Integer> counter, int partitionSize) { if (input == null) { throw new NullPointerException("Input list must not be null"); - } else if (elementCounter == null) { + } else if (counter == null) { throw new NullPointerException("Counter must not be null"); - } else if (numberPerPartition < 1 - || numberPerPartition > input.getSize()) { + } else if (partitionSize < 1 || partitionSize > input.getSize()) { throw new IllegalArgumentException( - "" + numberPerPartition + " is not a valid" + "" + partitionSize + " is not a valid" + " partition size. Must be between 1 and " + input.getSize()); } @@ -199,36 +195,36 @@ public class ListUtils { /* * List that holds our results */ - IList<IList<E>> returnedList = new FunctionalList<>(); + IList<IList<E>> returned = new FunctionalList<>(); /* * List that holds current partition */ - IHolder<IList<E>> currentPartition = new Identity<>( - new FunctionalList<>()); + IHolder<IList<E>> current = new Identity<>(new FunctionalList<>()); + /* * List that holds elements rejected during current pass */ - IList<E> rejectedElements = new FunctionalList<>(); + IList<E> rejected = new FunctionalList<>(); /* * The effective number of elements in the current partitition */ - IHolder<Integer> numberInCurrentPartition = new Identity<>(0); + IHolder<Integer> currentSize = new Identity<>(0); /* * Run up to a certain number of passes */ for (int numberOfIterations = 0; numberOfIterations < MAX_NTRIESPART - && !rejectedElements.isEmpty(); numberOfIterations++) { - input.forEach(new GroupPartIteration<>(returnedList, - currentPartition, rejectedElements, - numberInCurrentPartition, numberPerPartition, - elementCounter)); + && !rejected.isEmpty(); numberOfIterations++) { + input.forEach(new GroupPartIteration<>(returned, + current, rejected, + currentSize, partitionSize, + counter)); - if (rejectedElements.isEmpty()) { + if (rejected.isEmpty()) { // Nothing was rejected, so we're done - return returnedList; + return returned; } } @@ -237,11 +233,11 @@ public class ListUtils { + " iterations of partitioning) detected unpartitionable list " + input.toString() + "\nThe following elements were not partitioned: " - + rejectedElements.toString() + + rejected.toString() + "\nCurrent group in formation: " - + currentPartition.unwrap((vl) -> vl.toString()) + + current.unwrap((vl) -> vl.toString()) + "\nPreviously formed groups: " - + returnedList.toString()); + + returned.toString()); } /** @@ -255,13 +251,13 @@ public class ListUtils { */ @SafeVarargs public static <E> IList<E> mergeLists(IList<E>... lists) { - IList<E> returnedList = new FunctionalList<>(); + IList<E> returned = new FunctionalList<>(); for (IList<E> list : lists) { - list.forEach(returnedList::add); + list.forEach(returned::add); } - return returnedList; + return returned; } /** @@ -275,7 +271,7 @@ public class ListUtils { * The function to count elements with * @param size * The desired size of the list - * @param padSource + * @param padder * The function to get elements to pad with * @return The list, padded to the desired size * @throws IllegalArgumentException @@ -283,26 +279,26 @@ public class ListUtils { */ public static <E> IList<E> padList(IList<E> list, Function<E, Integer> counter, int size, - Supplier<E> padSource) { - IHolder<Integer> countHolder = new Identity<>(0); + Supplier<E> padder) { + IHolder<Integer> count = new Identity<>(0); - IList<E> ret = list.map((elm) -> { - countHolder.map((val) -> val + counter.apply(elm)); + IList<E> returned = list.map((elm) -> { + count.map((val) -> val + counter.apply(elm)); return elm; }); - if (countHolder.unwrap((val) -> val % size != 0)) { + if (count.unwrap((val) -> val % size != 0)) { // We need to pad - int needed = countHolder.unwrap((val) -> val % size); + int needed = count.unwrap((val) -> val % size); int threshold = 0; while (needed > 0 && threshold <= MAX_NTRIESPART) { - E val = padSource.get(); + E val = padder.get(); int count = counter.apply(val); if (count <= needed) { - ret.add(val); + returned.add(val); threshold = 0; @@ -319,7 +315,7 @@ public class ListUtils { } } - return ret; + return returned; } /** @@ -341,20 +337,19 @@ public class ListUtils { if (input == null) { throw new NullPointerException("Input must not be null"); } else if (operators == null) { - throw new NullPointerException( - "Set of operators must not be null"); + throw new NullPointerException("Set of operators must not be null"); } - IHolder<IList<String>> returnedList = new Identity<>(input); + IHolder<IList<String>> returned = new Identity<>(input); operators.forEach((operator) -> { - returnedList.transform((oldReturn) -> { + returned.transform((oldReturn) -> { return oldReturn.flatMap((token) -> { return operator.merge(new TokenSplitter(token)); }); }); }); - return returnedList.getValue(); + return returned.getValue(); } } diff --git a/BJC-Utils2/src/main/java/bjc/utils/funcutils/NumberUtils.java b/BJC-Utils2/src/main/java/bjc/utils/funcutils/NumberUtils.java index 419c787..24c2014 100644 --- a/BJC-Utils2/src/main/java/bjc/utils/funcutils/NumberUtils.java +++ b/BJC-Utils2/src/main/java/bjc/utils/funcutils/NumberUtils.java @@ -26,8 +26,7 @@ public class NumberUtils { } else { int result = 1; - for (int currentSub = 0; currentSub < power + 1; - currentSub++) { + for (int currentSub = 0; currentSub < power + 1; currentSub++) { result *= value - currentSub; } @@ -38,16 +37,16 @@ public class NumberUtils { /** * Evaluates a linear probability distribution * - * @param topExp + * @param winning * The number of winning possibilities - * @param bottomExp + * @param total * 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 */ - public static boolean isProbable(int topExp, int bottomExp, + public static boolean isProbable(int winning, int total, Function<Integer, Integer> rng) { - return rng.apply(bottomExp) < topExp; + return rng.apply(total) < winning; } } diff --git a/BJC-Utils2/src/main/java/bjc/utils/funcutils/StringUtils.java b/BJC-Utils2/src/main/java/bjc/utils/funcutils/StringUtils.java index 7573bfb..6770df2 100644 --- a/BJC-Utils2/src/main/java/bjc/utils/funcutils/StringUtils.java +++ b/BJC-Utils2/src/main/java/bjc/utils/funcutils/StringUtils.java @@ -9,7 +9,6 @@ import java.util.Deque; * */ public class StringUtils { - /** * Checks if the given expression contains the specified operator in a * situation that indicates its use as an infix operator. @@ -21,12 +20,10 @@ public class StringUtils { * @return Whether or not the given expression contains the specified * operator as a infix operator */ - public static boolean containsInfixOperator(String expression, - String operator) { + public static boolean containsInfixOperator(String expression, String operator) { // Bit annoying to have to use a full class name, but what are you // going to do? - return org.apache.commons.lang3.StringUtils - .countMatches(expression, operator) == 1 + return org.apache.commons.lang3.StringUtils.countMatches(expression, operator) == 1 && !expression.equalsIgnoreCase(operator) && !expression.startsWith(operator); } @@ -85,8 +82,7 @@ public class StringUtils { * @return A string version of the deque, with allowance for an empty * deque */ - public static < - ContainedType> String printDeque(Deque<ContainedType> queue) { + public static <ContainedType> String printDeque(Deque<ContainedType> queue) { return queue.isEmpty() ? "(none)" : queue.toString(); } } diff --git a/BJC-Utils2/src/main/java/bjc/utils/funcutils/TokenDeaffixer.java b/BJC-Utils2/src/main/java/bjc/utils/funcutils/TokenDeaffixer.java index 709538b..79f6e3b 100644 --- a/BJC-Utils2/src/main/java/bjc/utils/funcutils/TokenDeaffixer.java +++ b/BJC-Utils2/src/main/java/bjc/utils/funcutils/TokenDeaffixer.java @@ -16,23 +16,19 @@ final class TokenDeaffixer @Override public IList<String> apply(String operatorName, String operatorRegex) { if (operatorName == null) { - throw new NullPointerException( - "Operator name must not be null"); + throw new NullPointerException("Operator name must not be null"); } else if (operatorRegex == null) { - throw new NullPointerException( - "Operator regex must not be null"); + throw new NullPointerException("Operator regex must not be null"); } if (StringUtils.containsOnly(token, operatorRegex)) { return new FunctionalList<>(token); } else if (token.startsWith(operatorName)) { - return new FunctionalList<>(operatorName, - token.split(operatorRegex)[1]); + return new FunctionalList<>(operatorName, token.split(operatorRegex)[1]); } else if (token.endsWith(operatorName)) { - return new FunctionalList<>(token.split(operatorRegex)[0], - operatorName); + return new FunctionalList<>(token.split(operatorRegex)[0], operatorName); } else { return new FunctionalList<>(token); } } -}
\ No newline at end of file +} |
