From 63d88eb8db1f7a6d5924ec2a8b7f462373d5ac9a Mon Sep 17 00:00:00 2001 From: bjculkin Date: Fri, 7 Apr 2017 10:51:31 -0400 Subject: Cleanup --- .../bjc/utils/funcutils/CompoundCollector.java | 20 +++++--- .../main/java/bjc/utils/funcutils/FuncUtils.java | 4 +- .../bjc/utils/funcutils/FunctionalFileVisitor.java | 6 ++- .../bjc/utils/funcutils/GroupPartIteration.java | 4 +- .../main/java/bjc/utils/funcutils/ListUtils.java | 58 ++++++++++++---------- 5 files changed, 53 insertions(+), 39 deletions(-) (limited to 'BJC-Utils2/src/main/java/bjc/utils/funcutils') 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 7e7b003..ef259d5 100644 --- a/BJC-Utils2/src/main/java/bjc/utils/funcutils/CompoundCollector.java +++ b/BJC-Utils2/src/main/java/bjc/utils/funcutils/CompoundCollector.java @@ -35,7 +35,7 @@ final class CompoundCollector secondAccumulator = second.accumulator(); return (state, value) -> { - state.doWith((statePair) -> { + state.doWith(statePair -> { statePair.doWith((left, right) -> { firstAccumulator.accept(left, value); secondAccumulator.accept(right, value); @@ -55,8 +55,8 @@ final class CompoundCollector secondCombiner = second.combiner(); return (leftState, rightState) -> { - return leftState.unwrap((leftPair) -> { - return rightState.transform((rightPair) -> { + return leftState.unwrap(leftPair -> { + return rightState.transform(rightPair -> { return leftPair.combine(rightPair, firstCombiner, secondCombiner); }); }); @@ -65,10 +65,13 @@ final class CompoundCollector>, IPair> finisher() { - return (state) -> { - return state.unwrap((pair) -> { + return state -> { + return state.unwrap(pair -> { return pair.bind((left, right) -> { - return new Pair<>(first.finisher().apply(left), second.finisher().apply(right)); + FinalType1 finalLeft = first.finisher().apply(left); + FinalType2 finalRight = second.finisher().apply(right); + + return new Pair<>(finalLeft, finalRight); }); }); }; @@ -77,7 +80,10 @@ final class CompoundCollector>> supplier() { return () -> { - return new Identity<>(new Pair<>(first.supplier().get(), second.supplier().get())); + AuxType1 initialLeft = first.supplier().get(); + AuxType2 initialRight = second.supplier().get(); + + return new Identity<>(new Pair<>(initialLeft, initialRight)); }; } } 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 cb15d40..4c0abaf 100644 --- a/BJC-Utils2/src/main/java/bjc/utils/funcutils/FuncUtils.java +++ b/BJC-Utils2/src/main/java/bjc/utils/funcutils/FuncUtils.java @@ -27,7 +27,7 @@ public class FuncUtils { * function */ public static Function> curry2(BiFunction func) { - return (arg1) -> (arg2) -> { + return arg1 -> arg2 -> { return func.apply(arg1, arg2); }; } @@ -41,7 +41,7 @@ public class FuncUtils { * The action to perform */ public static void doTimes(int nTimes, Consumer cons) { - for(int i = 0; i < nTimes; i++) { + 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 c806d50..8c3e1eb 100644 --- a/BJC-Utils2/src/main/java/bjc/utils/funcutils/FunctionalFileVisitor.java +++ b/BJC-Utils2/src/main/java/bjc/utils/funcutils/FunctionalFileVisitor.java @@ -19,14 +19,16 @@ final class FunctionalFileVisitor extends SimpleFileVisitor { @Override public FileVisitResult preVisitDirectory(Path dir, BasicFileAttributes attrs) throws IOException { - if(predicate.test(dir, attrs)) return FileVisitResult.CONTINUE; + 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; + 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 f736dcb..a65f83a 100644 --- a/BJC-Utils2/src/main/java/bjc/utils/funcutils/GroupPartIteration.java +++ b/BJC-Utils2/src/main/java/bjc/utils/funcutils/GroupPartIteration.java @@ -40,7 +40,7 @@ final class GroupPartIteration implements Consumer { public void accept(E value) { boolean shouldStartPartition = numberInCurrentPartition >= numberPerPartition; - if(shouldStartPartition) { + if (shouldStartPartition) { returnedList.add(currentPartition); currentPartition = new FunctionalList<>(); @@ -50,7 +50,7 @@ final class GroupPartIteration implements Consumer { boolean shouldReject = numberInCurrentPartition + currentElementCount >= numberPerPartition; - if(shouldReject) { + if (shouldReject) { rejectedItems.add(value); } else { currentPartition.add(value); 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 791598f..27666dd 100644 --- a/BJC-Utils2/src/main/java/bjc/utils/funcutils/ListUtils.java +++ b/BJC-Utils2/src/main/java/bjc/utils/funcutils/ListUtils.java @@ -29,7 +29,8 @@ public class ListUtils { * @return The collapsed string of tokens */ public static String collapseTokens(IList input) { - if(input == null) throw new NullPointerException("Input must not be null"); + if (input == null) + throw new NullPointerException("Input must not be null"); return collapseTokens(input, ""); } @@ -45,22 +46,23 @@ public class ListUtils { * @return The collapsed string of tokens */ public static String collapseTokens(IList input, 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 { StringBuilder state = new StringBuilder(); int i = 1; - for(String itm : input.toIterable()) { + for (String itm : input.toIterable()) { state.append(itm); - if(i != input.getSize()) { + if (i != input.getSize()) { state.append(seperator); } @@ -111,15 +113,15 @@ public class ListUtils { Iterator itr = list.toIterable().iterator(); E element = null; - for(int index = 0; itr.hasNext(); element = itr.next()) { - int winningChance = number - selected.getSize(); + for (int index = 0; itr.hasNext(); element = itr.next()) { // n - m + int winningChance = number - selected.getSize(); - int totalChance = total - (index - 1); // N - t + 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); } } @@ -145,7 +147,7 @@ public class ListUtils { public static IList drawWithReplacement(IList list, int number, Function rng) { IList 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)); } @@ -171,13 +173,16 @@ public class ListUtils { */ public static IList> groupPartition(IList input, Function counter, 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()) - throw new IllegalArgumentException("" + partitionSize + " is not a valid" - + " partition size. Must be between 1 and " + input.getSize()); + 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()); + + throw new IllegalArgumentException(msg); + } /* * List that holds our results @@ -194,11 +199,12 @@ public class ListUtils { /* * 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 we're + if (rejected.isEmpty()) // Nothing was rejected, so + // we're // done return returned; } @@ -223,8 +229,8 @@ public class ListUtils { public static IList mergeLists(IList... lists) { IList returned = new FunctionalList<>(); - for(IList list : lists) { - for(E itm : list.toIterable()) { + for (IList list : lists) { + for (E itm : list.toIterable()) { returned.add(itm); } } @@ -254,22 +260,22 @@ public class ListUtils { IList returned = new FunctionalList<>(); - for(E itm : list.toIterable()) { + for (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) { + while (needed > 0 && threshold <= MAX_NTRIESPART) { E val = padder.get(); int newCount = counter.apply(val); - if(newCount <= needed) { + if (newCount <= needed) { returned.add(val); threshold = 0; @@ -280,7 +286,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 "); } -- cgit v1.2.3