From 63d88eb8db1f7a6d5924ec2a8b7f462373d5ac9a Mon Sep 17 00:00:00 2001 From: bjculkin Date: Fri, 7 Apr 2017 10:51:31 -0400 Subject: Cleanup --- .../main/java/bjc/utils/funcutils/ListUtils.java | 58 ++++++++++++---------- 1 file changed, 32 insertions(+), 26 deletions(-) (limited to 'BJC-Utils2/src/main/java/bjc/utils/funcutils/ListUtils.java') 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