diff options
Diffstat (limited to 'BJC-Utils2/src/main/java/bjc/utils/funcutils')
12 files changed, 198 insertions, 198 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 9a8cca3..a044bfd 100644 --- a/BJC-Utils2/src/main/java/bjc/utils/funcutils/CollectorUtils.java +++ b/BJC-Utils2/src/main/java/bjc/utils/funcutils/CollectorUtils.java @@ -1,10 +1,10 @@ package bjc.utils.funcutils; +import java.util.stream.Collector; + import bjc.utils.data.IHolder; import bjc.utils.data.IPair; -import java.util.stream.Collector; - /** * Utilities for producing implementations of {@link Collector} * @@ -32,8 +32,8 @@ public class CollectorUtils { * @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> first, - Collector<InitialType, AuxType2, FinalType2> second) { + final Collector<InitialType, AuxType1, FinalType1> first, + final 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 ef259d5..35695bc 100644 --- a/BJC-Utils2/src/main/java/bjc/utils/funcutils/CompoundCollector.java +++ b/BJC-Utils2/src/main/java/bjc/utils/funcutils/CompoundCollector.java @@ -1,10 +1,5 @@ package bjc.utils.funcutils; -import bjc.utils.data.IHolder; -import bjc.utils.data.IPair; -import bjc.utils.data.Identity; -import bjc.utils.data.Pair; - import java.util.Set; import java.util.function.BiConsumer; import java.util.function.BinaryOperator; @@ -12,16 +7,21 @@ import java.util.function.Function; import java.util.function.Supplier; import java.util.stream.Collector; +import bjc.utils.data.IHolder; +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; + private final Set<java.util.stream.Collector.Characteristics> characteristicSet; - private Collector<InitialType, AuxType1, FinalType1> first; - private Collector<InitialType, AuxType2, FinalType2> second; + private final Collector<InitialType, AuxType1, FinalType1> first; + private final Collector<InitialType, AuxType2, FinalType2> second; - public CompoundCollector(Collector<InitialType, AuxType1, FinalType1> first, - Collector<InitialType, AuxType2, FinalType2> second) { + public CompoundCollector(final Collector<InitialType, AuxType1, FinalType1> first, + final Collector<InitialType, AuxType2, FinalType2> second) { this.first = first; this.second = second; @@ -31,8 +31,8 @@ final class CompoundCollector<InitialType, AuxType1, AuxType2, FinalType1, Final @Override public BiConsumer<IHolder<IPair<AuxType1, AuxType2>>, InitialType> accumulator() { - BiConsumer<AuxType1, InitialType> firstAccumulator = first.accumulator(); - BiConsumer<AuxType2, InitialType> secondAccumulator = second.accumulator(); + final BiConsumer<AuxType1, InitialType> firstAccumulator = first.accumulator(); + final BiConsumer<AuxType2, InitialType> secondAccumulator = second.accumulator(); return (state, value) -> { state.doWith(statePair -> { @@ -51,8 +51,8 @@ final class CompoundCollector<InitialType, AuxType1, AuxType2, FinalType1, Final @Override public BinaryOperator<IHolder<IPair<AuxType1, AuxType2>>> combiner() { - BinaryOperator<AuxType1> firstCombiner = first.combiner(); - BinaryOperator<AuxType2> secondCombiner = second.combiner(); + final BinaryOperator<AuxType1> firstCombiner = first.combiner(); + final BinaryOperator<AuxType2> secondCombiner = second.combiner(); return (leftState, rightState) -> { return leftState.unwrap(leftPair -> { @@ -68,8 +68,8 @@ final class CompoundCollector<InitialType, AuxType1, AuxType2, FinalType1, Final return state -> { return state.unwrap(pair -> { return pair.bind((left, right) -> { - FinalType1 finalLeft = first.finisher().apply(left); - FinalType2 finalRight = second.finisher().apply(right); + final FinalType1 finalLeft = first.finisher().apply(left); + final FinalType2 finalRight = second.finisher().apply(right); return new Pair<>(finalLeft, finalRight); }); @@ -80,8 +80,8 @@ final class CompoundCollector<InitialType, AuxType1, AuxType2, FinalType1, Final @Override public Supplier<IHolder<IPair<AuxType1, AuxType2>>> supplier() { return () -> { - AuxType1 initialLeft = first.supplier().get(); - AuxType2 initialRight = second.supplier().get(); + final AuxType1 initialLeft = first.supplier().get(); + final AuxType2 initialRight = second.supplier().get(); return new Identity<>(new Pair<>(initialLeft, initialRight)); }; 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 cab444d..e4c0bda 100644 --- a/BJC-Utils2/src/main/java/bjc/utils/funcutils/EnumUtils.java +++ b/BJC-Utils2/src/main/java/bjc/utils/funcutils/EnumUtils.java @@ -1,11 +1,11 @@ package bjc.utils.funcutils; -import bjc.utils.funcdata.FunctionalList; -import bjc.utils.funcdata.IList; - import java.util.Random; import java.util.function.Consumer; +import bjc.utils.funcdata.FunctionalList; +import bjc.utils.funcdata.IList; + /** * Utility methods on enums * @@ -27,16 +27,16 @@ public class EnumUtils { * @param rnd * The source of randomness to use */ - public static <E extends Enum<E>> void doForValues(Class<E> clasz, int nValues, Consumer<E> action, - Random rnd) { - E[] enumValues = clasz.getEnumConstants(); + public static <E extends Enum<E>> void doForValues(final Class<E> clasz, final int nValues, + final Consumer<E> action, final Random rnd) { + final E[] enumValues = clasz.getEnumConstants(); - IList<E> valueList = new FunctionalList<>(enumValues); + final IList<E> valueList = new FunctionalList<>(enumValues); - int randomValueCount = enumValues.length - nValues; + final int randomValueCount = enumValues.length - nValues; - for(int i = 0; i <= randomValueCount; i++) { - E rDir = valueList.randItem(rnd::nextInt); + for (int i = 0; i <= randomValueCount; i++) { + final E rDir = valueList.randItem(rnd::nextInt); valueList.removeMatching(rDir); } @@ -55,8 +55,8 @@ public class EnumUtils { * The random source to use * @return A random value from the specified enum */ - public static <E extends Enum<E>> E getRandomValue(Class<E> clasz, Random rnd) { - E[] enumValues = clasz.getEnumConstants(); + public static <E extends Enum<E>> E getRandomValue(final Class<E> clasz, final Random rnd) { + final 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 4c9c525..87199b1 100644 --- a/BJC-Utils2/src/main/java/bjc/utils/funcutils/FileUtils.java +++ b/BJC-Utils2/src/main/java/bjc/utils/funcutils/FileUtils.java @@ -33,8 +33,8 @@ public class FileUtils { * for this with all the buttons and knobs from * walkFileTree */ - public static void traverseDirectory(Path root, BiPredicate<Path, BasicFileAttributes> predicate, - BiPredicate<Path, BasicFileAttributes> action) throws IOException { + public static void traverseDirectory(final Path root, final BiPredicate<Path, BasicFileAttributes> predicate, + final BiPredicate<Path, BasicFileAttributes> action) throws IOException { 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 4c0abaf..0d1a688 100644 --- a/BJC-Utils2/src/main/java/bjc/utils/funcutils/FuncUtils.java +++ b/BJC-Utils2/src/main/java/bjc/utils/funcutils/FuncUtils.java @@ -26,7 +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(final BiFunction<A, B, C> func) { return arg1 -> arg2 -> { return func.apply(arg1, arg2); }; @@ -40,7 +40,7 @@ public class FuncUtils { * @param cons * The action to perform */ - public static void doTimes(int nTimes, Consumer<Integer> cons) { + public static void doTimes(final int nTimes, final Consumer<Integer> cons) { for (int i = 0; i < nTimes; i++) { cons.accept(i); } 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 8c3e1eb..4310416 100644 --- a/BJC-Utils2/src/main/java/bjc/utils/funcutils/FunctionalFileVisitor.java +++ b/BJC-Utils2/src/main/java/bjc/utils/funcutils/FunctionalFileVisitor.java @@ -8,27 +8,25 @@ import java.nio.file.attribute.BasicFileAttributes; import java.util.function.BiPredicate; final class FunctionalFileVisitor extends SimpleFileVisitor<Path> { - private BiPredicate<Path, BasicFileAttributes> predicate; - private BiPredicate<Path, BasicFileAttributes> action; + private final BiPredicate<Path, BasicFileAttributes> predicate; + private final BiPredicate<Path, BasicFileAttributes> action; - public FunctionalFileVisitor(BiPredicate<Path, BasicFileAttributes> predicate, - BiPredicate<Path, BasicFileAttributes> action) { + public FunctionalFileVisitor(final BiPredicate<Path, BasicFileAttributes> predicate, + final BiPredicate<Path, BasicFileAttributes> action) { this.predicate = predicate; this.action = action; } @Override - public FileVisitResult preVisitDirectory(Path dir, BasicFileAttributes attrs) throws IOException { - if (predicate.test(dir, attrs)) - return FileVisitResult.CONTINUE; + public FileVisitResult preVisitDirectory(final Path dir, final BasicFileAttributes attrs) throws IOException { + if (predicate.test(dir, attrs)) return FileVisitResult.CONTINUE; return FileVisitResult.SKIP_SUBTREE; } @Override - public FileVisitResult visitFile(Path file, BasicFileAttributes attrs) throws IOException { - if (action.test(file, attrs)) - return FileVisitResult.CONTINUE; + public FileVisitResult visitFile(final Path file, final BasicFileAttributes attrs) throws IOException { + if (action.test(file, attrs)) return FileVisitResult.CONTINUE; return FileVisitResult.TERMINATE; } 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 a65f83a..f3b2254 100644 --- a/BJC-Utils2/src/main/java/bjc/utils/funcutils/GroupPartIteration.java +++ b/BJC-Utils2/src/main/java/bjc/utils/funcutils/GroupPartIteration.java @@ -1,11 +1,11 @@ package bjc.utils.funcutils; -import bjc.utils.funcdata.FunctionalList; -import bjc.utils.funcdata.IList; - import java.util.function.Consumer; import java.util.function.Function; +import bjc.utils.funcdata.FunctionalList; +import bjc.utils.funcdata.IList; + /** * Implements a single group partitioning pass on a list * @@ -15,18 +15,18 @@ import java.util.function.Function; * The type of element in the list being partitioned */ final class GroupPartIteration<E> implements Consumer<E> { - private IList<IList<E>> returnedList; + private final IList<IList<E>> returnedList; public IList<E> currentPartition; - private IList<E> rejectedItems; + private final IList<E> rejectedItems; - private int numberInCurrentPartition; - private int numberPerPartition; + private int numberInCurrentPartition; + private final int numberPerPartition; - private Function<E, Integer> elementCounter; + private final Function<E, Integer> elementCounter; - public GroupPartIteration(IList<IList<E>> returned, IList<E> rejects, int nPerPart, - Function<E, Integer> eleCount) { + public GroupPartIteration(final IList<IList<E>> returned, final IList<E> rejects, final int nPerPart, + final Function<E, Integer> eleCount) { this.returnedList = returned; this.rejectedItems = rejects; this.numberPerPartition = nPerPart; @@ -37,8 +37,8 @@ final class GroupPartIteration<E> implements Consumer<E> { } @Override - public void accept(E value) { - boolean shouldStartPartition = numberInCurrentPartition >= numberPerPartition; + public void accept(final E value) { + final boolean shouldStartPartition = numberInCurrentPartition >= numberPerPartition; if (shouldStartPartition) { returnedList.add(currentPartition); @@ -46,9 +46,10 @@ final class GroupPartIteration<E> implements Consumer<E> { currentPartition = new FunctionalList<>(); numberInCurrentPartition = 0; } else { - int currentElementCount = elementCounter.apply(value); + final int currentElementCount = elementCounter.apply(value); - boolean shouldReject = numberInCurrentPartition + currentElementCount >= numberPerPartition; + final boolean shouldReject = numberInCurrentPartition + + currentElementCount >= numberPerPartition; if (shouldReject) { rejectedItems.add(value); diff --git a/BJC-Utils2/src/main/java/bjc/utils/funcutils/LambdaLock.java b/BJC-Utils2/src/main/java/bjc/utils/funcutils/LambdaLock.java index 23745d8..62c5d32 100644 --- a/BJC-Utils2/src/main/java/bjc/utils/funcutils/LambdaLock.java +++ b/BJC-Utils2/src/main/java/bjc/utils/funcutils/LambdaLock.java @@ -8,13 +8,13 @@ import java.util.function.Supplier; /** * A wrapper around a {@link ReadWriteLock} to ensure that the lock is used * properly. - * + * * @author EVE * */ public class LambdaLock { - private Lock readLock; - private Lock writeLock; + private final Lock readLock; + private final Lock writeLock; /** * Create a new lambda-enabled lock around a new lock. @@ -25,24 +25,24 @@ public class LambdaLock { /** * Create a new lambda-enabled lock. - * + * * @param lck * The lock to wrap. */ - public LambdaLock(ReadWriteLock lck) { + public LambdaLock(final ReadWriteLock lck) { readLock = lck.readLock(); writeLock = lck.writeLock(); } /** * Execute an action with the read lock taken. - * + * * @param supp * The action to call. - * + * * @return The result of the action. */ - public <T> T read(Supplier<T> supp) { + public <T> T read(final Supplier<T> supp) { readLock.lock(); try { @@ -54,13 +54,13 @@ public class LambdaLock { /** * Execute an action with the write lock taken. - * + * * @param supp * The action to call. - * + * * @return The result of the action. */ - public <T> T write(Supplier<T> supp) { + public <T> T write(final Supplier<T> supp) { writeLock.lock(); try { @@ -72,12 +72,12 @@ public class LambdaLock { /** * Execute an action with the read lock taken. - * + * * @param action * The action to call. - * + * */ - public void read(Runnable action) { + public void read(final Runnable action) { readLock.lock(); try { @@ -89,11 +89,11 @@ public class LambdaLock { /** * Execute an action with the write lock taken. - * + * * @param action * The action to call. */ - public void write(Runnable action) { + public void write(final Runnable action) { writeLock.lock(); try { 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 ad0f565..52a2437 100644 --- a/BJC-Utils2/src/main/java/bjc/utils/funcutils/ListUtils.java +++ b/BJC-Utils2/src/main/java/bjc/utils/funcutils/ListUtils.java @@ -1,13 +1,13 @@ package bjc.utils.funcutils; -import bjc.utils.funcdata.FunctionalList; -import bjc.utils.funcdata.IList; - import java.util.ArrayList; import java.util.Iterator; import java.util.function.Function; import java.util.function.Supplier; +import bjc.utils.funcdata.FunctionalList; +import bjc.utils.funcdata.IList; + /** * Utilities for manipulating FunctionalLists that don't belong in the class * itself @@ -26,8 +26,8 @@ public class ListUtils { * The list of tokens to collapse * @return The collapsed string of tokens */ - public static String collapseTokens(IList<String> input) { - if(input == null) throw new NullPointerException("Input must not be null"); + public static String collapseTokens(final IList<String> input) { + if (input == null) throw new NullPointerException("Input must not be null"); return collapseTokens(input, ""); } @@ -42,23 +42,23 @@ public class ListUtils { * The separator to use for separating tokens * @return The collapsed string of tokens */ - public static String collapseTokens(IList<String> input, String seperator) { - if(input == null) + public static String collapseTokens(final IList<String> input, final String seperator) { + 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 { - StringBuilder state = new StringBuilder(); + final StringBuilder state = new StringBuilder(); int i = 1; - for(String itm : input.toIterable()) { + for (final String itm : input.toIterable()) { state.append(itm); - if(i != input.getSize()) { + if (i != input.getSize()) { state.append(seperator); } @@ -85,23 +85,24 @@ public class ListUtils { * selected from the specified list without replacement */ - public static <E> IList<E> drawWithoutReplacement(IList<E> list, int number, Function<Integer, Integer> rng) { - IList<E> selected = new FunctionalList<>(new ArrayList<>(number)); + 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)); - int total = list.getSize(); + final int total = list.getSize(); - Iterator<E> itr = list.toIterable().iterator(); + final Iterator<E> itr = list.toIterable().iterator(); E element = null; - for(int index = 0; itr.hasNext(); element = itr.next()) { + for (final int index = 0; itr.hasNext(); element = itr.next()) { // n - m - int winningChance = number - selected.getSize(); + final int winningChance = number - selected.getSize(); // N - t - int totalChance = total - (index - 1); + final int totalChance = total - (index - 1); // Probability of selecting the t+1'th element - if(NumberUtils.isProbable(winningChance, totalChance, rng)) { + if (NumberUtils.isProbable(winningChance, totalChance, rng)) { selected.add(element); } } @@ -124,10 +125,11 @@ public class ListUtils { * @return A new list containing the desired number of items randomly * selected from the specified list */ - public static <E> IList<E> drawWithReplacement(IList<E> list, int number, Function<Integer, Integer> rng) { - IList<E> selected = new FunctionalList<>(new ArrayList<>(number)); + public static <E> IList<E> drawWithReplacement(final IList<E> list, final int number, + final Function<Integer, Integer> rng) { + final IList<E> selected = new FunctionalList<>(new ArrayList<>(number)); - for(int i = 0; i < number; i++) { + for (int i = 0; i < number; i++) { selected.add(list.randItem(rng)); } @@ -151,15 +153,15 @@ public class ListUtils { * * @return A list partitioned according to the above rules */ - public static <E> IList<IList<E>> groupPartition(IList<E> input, Function<E, Integer> counter, - int partitionSize) { - if(input == null) + public static <E> IList<IList<E>> groupPartition(final IList<E> input, final Function<E, Integer> counter, + final int partitionSize) { + 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()) { - String fmt = "%d is not a valid partition size. Must be between 1 and %d"; - String msg = String.format(fmt, 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); } @@ -167,23 +169,23 @@ public class ListUtils { /* * List that holds our results */ - IList<IList<E>> returned = new FunctionalList<>(); + final IList<IList<E>> returned = new FunctionalList<>(); /* * List that holds elements rejected during current pass */ - IList<E> rejected = new FunctionalList<>(); + final IList<E> rejected = new FunctionalList<>(); - GroupPartIteration<E> it = new GroupPartIteration<>(returned, rejected, partitionSize, counter); + final GroupPartIteration<E> it = new GroupPartIteration<>(returned, rejected, partitionSize, counter); /* * Run up to a certain number of passes */ - for(int numberOfIterations = 0; numberOfIterations < MAX_NTRIESPART + for (int numberOfIterations = 0; numberOfIterations < MAX_NTRIESPART && !rejected.isEmpty(); numberOfIterations++) { input.forEach(it); - if(rejected.isEmpty()) // Nothing was rejected, so + if (rejected.isEmpty()) // Nothing was rejected, so // we're // done return returned; @@ -206,11 +208,11 @@ public class ListUtils { * @return A list containing all the elements of the lists */ @SafeVarargs - public static <E> IList<E> mergeLists(IList<E>... lists) { - IList<E> returned = new FunctionalList<>(); + public static <E> IList<E> mergeLists(final IList<E>... lists) { + final IList<E> returned = new FunctionalList<>(); - for(IList<E> list : lists) { - for(E itm : list.toIterable()) { + for (final IList<E> list : lists) { + for (final E itm : list.toIterable()) { returned.add(itm); } } @@ -235,27 +237,28 @@ public class ListUtils { * @throws IllegalArgumentException * if the list couldn't be padded to the desired size */ - public static <E> IList<E> padList(IList<E> list, Function<E, Integer> counter, int size, Supplier<E> padder) { + public static <E> IList<E> padList(final IList<E> list, final Function<E, Integer> counter, final int size, + final Supplier<E> padder) { int count = 0; - IList<E> returned = new FunctionalList<>(); + final IList<E> returned = new FunctionalList<>(); - for(E itm : list.toIterable()) { + for (final E itm : list.toIterable()) { count += counter.apply(itm); returned.add(itm); } - if(count % size != 0) { + if (count % size != 0) { // We need to pad int needed = count % size; int threshold = 0; - while(needed > 0 && threshold <= MAX_NTRIESPART) { - E val = padder.get(); - int newCount = counter.apply(val); + while (needed > 0 && threshold <= MAX_NTRIESPART) { + final E val = padder.get(); + final int newCount = counter.apply(val); - if(newCount <= needed) { + if (newCount <= needed) { returned.add(val); threshold = 0; @@ -266,7 +269,7 @@ public class ListUtils { } } - if(threshold > MAX_NTRIESPART) + if (threshold > MAX_NTRIESPART) throw new IllegalArgumentException("Heuristic (more than " + MAX_NTRIESPART + " iterations of attempting to pad) detected unpaddable list "); } 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 8a14d7d..770d3a5 100644 --- a/BJC-Utils2/src/main/java/bjc/utils/funcutils/NumberUtils.java +++ b/BJC-Utils2/src/main/java/bjc/utils/funcutils/NumberUtils.java @@ -18,15 +18,15 @@ public class NumberUtils { * The power to do the falling factorial for * @return The falling factorial of the number to the power */ - public static int fallingFactorial(int value, int power) { - if(power == 0) + public static int fallingFactorial(final int value, final int power) { + if (power == 0) return 1; - else if(power == 1) + else if (power == 1) return value; else { int result = 1; - for(int currentSub = 0; currentSub < power + 1; currentSub++) { + for (int currentSub = 0; currentSub < power + 1; currentSub++) { result *= value - currentSub; } @@ -45,25 +45,25 @@ public class NumberUtils { * 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 winning, int total, Function<Integer, Integer> rng) { + public static boolean isProbable(final int winning, final int total, final Function<Integer, Integer> rng) { return rng.apply(total) < winning; } /** * Check if a number is in an inclusive range. - * + * * @param min * The minimum value of the range. - * + * * @param max * The maximum value of the range. - * + * * @param i * The number to check. - * + * * @return Whether the number is in the range. */ - public static boolean between(int min, int max, int i) { - return (i >= min) && (i <= max); + public static boolean between(final int min, final int max, final int i) { + return i >= min && i <= max; } } 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 d5eaadf..6e24e50 100644 --- a/BJC-Utils2/src/main/java/bjc/utils/funcutils/StringUtils.java +++ b/BJC-Utils2/src/main/java/bjc/utils/funcutils/StringUtils.java @@ -1,9 +1,9 @@ package bjc.utils.funcutils; -import com.ibm.icu.text.BreakIterator; - import java.util.Deque; +import com.ibm.icu.text.BreakIterator; + /** * Utility methods for operations on strings * @@ -16,25 +16,25 @@ public class StringUtils { * 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(String input, String rRegex) { + public static boolean containsOnly(final String input, final String rRegex) { if (input == null) throw new NullPointerException("Input must not be null"); - else if (rRegex == null) - throw new NullPointerException("Regex must not be null"); + else if (rRegex == null) throw new NullPointerException("Regex must not be null"); /* * This regular expression is fairly simple. * * 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 + * 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 */ return input.matches("\\A(?:" + rRegex + ")+\\Z"); } @@ -43,51 +43,51 @@ public class StringUtils { * 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(StringBuilder builder, int levels) { + public static void indentNLevels(final StringBuilder builder, final int levels) { for (int i = 0; i < levels; i++) { builder.append("\t"); } } /** - * Print out a deque with a special case for easily showing a deque is empty + * Print out a deque with a special case for easily showing a deque is + * 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(Deque<ContainedType> queue) { + public static <ContainedType> String printDeque(final Deque<ContainedType> queue) { return queue.isEmpty() ? "(none)" : queue.toString(); } /** * 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. */ - public static String toEnglishList(Object[] objects, String join, String comma) { - if (objects == null) { - throw new NullPointerException("Sequence must not be null"); - } + public static String toEnglishList(final Object[] objects, final String join, final String comma) { + if (objects == null) throw new NullPointerException("Sequence must not be null"); - StringBuilder sb = new StringBuilder(); + final StringBuilder sb = new StringBuilder(); - String joiner = join; - String coma = comma; + final String joiner = join; + final String coma = comma; switch (objects.length) { case 0: @@ -119,9 +119,9 @@ public class StringUtils { } /* * Uncomment this to remove serial commas. - * + * * int lc = sb.length() - 1; - * + * * sb.delete(lc - coma.length(), lc); */ sb.append(joiner + " "); @@ -133,47 +133,45 @@ 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. */ - public static String toEnglishList(Object[] objects, String join) { + public static String toEnglishList(final Object[] objects, final String join) { return toEnglishList(objects, join, ","); } /** * 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. */ - public static String toEnglishList(Object[] objects, boolean and) { - if (and) { + public static String toEnglishList(final Object[] objects, final boolean 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. */ - public static int graphemeCount(String value) { - BreakIterator it = BreakIterator.getCharacterInstance(); + public static int graphemeCount(final String value) { + final BreakIterator it = BreakIterator.getCharacterInstance(); it.setText(value); int count = 0; diff --git a/BJC-Utils2/src/main/java/bjc/utils/funcutils/TriConsumer.java b/BJC-Utils2/src/main/java/bjc/utils/funcutils/TriConsumer.java index c161ed7..f30386c 100644 --- a/BJC-Utils2/src/main/java/bjc/utils/funcutils/TriConsumer.java +++ b/BJC-Utils2/src/main/java/bjc/utils/funcutils/TriConsumer.java @@ -2,9 +2,9 @@ package bjc.utils.funcutils; /** * Consumer that takes three arguments. - * + * * @author EVE - * + * * @param <A> * Type of the first argument. * @param <B> @@ -17,13 +17,13 @@ package bjc.utils.funcutils; public interface TriConsumer<A, B, C> { /** * Perform the action. - * + * * @param a * The first parameter. - * + * * @param b * The second parameter. - * + * * @param c * The third parameter. */ |
