diff options
| author | bjculkin <bjculkin@mix.wvu.edu> | 2018-02-12 22:44:26 -0500 |
|---|---|---|
| committer | bjculkin <bjculkin@mix.wvu.edu> | 2018-02-12 22:44:26 -0500 |
| commit | ae51c587c53f7ca311e556e3cbd0c5566d6c2843 (patch) | |
| tree | a6bbb0baaa20300ad9949425455ea890c021d046 /base/src/main | |
| parent | 32f5da54c628408c96db09d279f3a7ef44b3bd19 (diff) | |
Update
Diffstat (limited to 'base/src/main')
11 files changed, 286 insertions, 109 deletions
diff --git a/base/src/main/java/bjc/utils/data/Lazy.java b/base/src/main/java/bjc/utils/data/Lazy.java index fcebb70..4ffcead 100644 --- a/base/src/main/java/bjc/utils/data/Lazy.java +++ b/base/src/main/java/bjc/utils/data/Lazy.java @@ -15,7 +15,7 @@ import bjc.utils.funcdata.IList; * @author ben * * @param <ContainedType> - * The type of the value being held. + * The type of the value being held. */ public class Lazy<ContainedType> implements IHolder<ContainedType> { /* The supplier of the type. */ @@ -32,7 +32,7 @@ public class Lazy<ContainedType> implements IHolder<ContainedType> { * Create a new lazy value from the specified seed value. * * @param value - * The seed value to use. + * The seed value to use. */ public Lazy(final ContainedType value) { heldValue = value; @@ -44,7 +44,7 @@ public class Lazy<ContainedType> implements IHolder<ContainedType> { * Create a new lazy value from the specified value source. * * @param supp - * The source of a value to use. + * The source of a value to use. */ public Lazy(final Supplier<ContainedType> supp) { valueSupplier = new SingleSupplier<>(supp); @@ -66,7 +66,7 @@ public class Lazy<ContainedType> implements IHolder<ContainedType> { actions.forEach(pendingActions::add); final Supplier<ContainedType> supplier = () -> { - if (valueMaterialized) return heldValue; + if(valueMaterialized) return heldValue; return valueSupplier.get(); }; @@ -92,7 +92,7 @@ public class Lazy<ContainedType> implements IHolder<ContainedType> { return new Lazy<>(() -> { ContainedType currVal = heldValue; - if (!valueMaterialized) { + if(!valueMaterialized) { currVal = valueSupplier.get(); } @@ -103,10 +103,12 @@ public class Lazy<ContainedType> implements IHolder<ContainedType> { @Override public String toString() { - if (valueMaterialized) { - if (actions.isEmpty()) + if(valueMaterialized) { + if(actions.isEmpty()) { return String.format("value[v='%s']", heldValue); - else return String.format("value[v='%s'] (has pending transforms)", heldValue); + } + + return String.format("value[v='%s'] (has pending transforms)", heldValue); } return "(unmaterialized)"; @@ -121,7 +123,7 @@ public class Lazy<ContainedType> implements IHolder<ContainedType> { @Override public <UnwrappedType> UnwrappedType unwrap(final Function<ContainedType, UnwrappedType> unwrapper) { - if (!valueMaterialized) { + if(!valueMaterialized) { heldValue = valueSupplier.get(); valueMaterialized = true; @@ -150,23 +152,24 @@ public class Lazy<ContainedType> implements IHolder<ContainedType> { @Override public boolean equals(final Object obj) { - if (this == obj) return true; - if (obj == null) return false; - if (!(obj instanceof Lazy<?>)) return false; + if(this == obj) return true; + if(obj == null) return false; + if(!(obj instanceof Lazy<?>)) return false; final Lazy<?> other = (Lazy<?>) obj; - if (valueMaterialized != other.valueMaterialized) return false; + if(valueMaterialized != other.valueMaterialized) return false; - if (valueMaterialized) { - if (heldValue == null) { - if (other.heldValue != null) return false; - } else if (!heldValue.equals(other.heldValue)) return false; - } else return false; + if(valueMaterialized) { + if(heldValue == null) { + if(other.heldValue != null) return false; + } else if(!heldValue.equals(other.heldValue)) return false; + } else + return false; - if (actions == null) { - if (other.actions != null) return false; - } else if (actions.getSize() > 0 || other.actions.getSize() > 0) return false; + if(actions == null) { + if(other.actions != null) return false; + } else if(actions.getSize() > 0 || other.actions.getSize() > 0) return false; return true; } @@ -175,10 +178,9 @@ public class Lazy<ContainedType> implements IHolder<ContainedType> { * Create a new lazy container with an already present value. * * @param val - * The value for the lazy container. + * The value for the lazy container. * - * @return - * A new lazy container holding that value. + * @return A new lazy container holding that value. */ public static <ContainedType> Lazy<ContainedType> lazy(final ContainedType val) { return new Lazy<>(val); @@ -188,11 +190,10 @@ public class Lazy<ContainedType> implements IHolder<ContainedType> { * Create a new lazy container with a suspended value. * * @param supp - * The suspended value for the lazy container. + * The suspended value for the lazy container. * - * @return - * A new lazy container that will un-suspend the value when - * necessary. + * @return A new lazy container that will un-suspend the value when + * necessary. */ public static <ContainedType> Lazy<ContainedType> lazy(final Supplier<ContainedType> supp) { return new Lazy<>(supp); diff --git a/base/src/main/java/bjc/utils/data/internals/HalfBoundLazyPair.java b/base/src/main/java/bjc/utils/data/internals/HalfBoundLazyPair.java index c3606ef..5467255 100644 --- a/base/src/main/java/bjc/utils/data/internals/HalfBoundLazyPair.java +++ b/base/src/main/java/bjc/utils/data/internals/HalfBoundLazyPair.java @@ -22,6 +22,7 @@ import bjc.utils.data.LazyPair; * * @author Ben Culkin */ +@SuppressWarnings("javadoc") public class HalfBoundLazyPair<OldType, NewLeft, NewRight> implements IPair<NewLeft, NewRight> { /* The supplier of the old value. */ private final Supplier<OldType> oldSupplier; diff --git a/base/src/main/java/bjc/utils/funcdata/IMap.java b/base/src/main/java/bjc/utils/funcdata/IMap.java index 0262c13..35dc64b 100644 --- a/base/src/main/java/bjc/utils/funcdata/IMap.java +++ b/base/src/main/java/bjc/utils/funcdata/IMap.java @@ -83,7 +83,7 @@ public interface IMap<KeyType, ValueType> { default ValueType getOrDefault(final KeyType key, final ValueType defaultValue) { try { return get(key); - } catch (final IllegalArgumentException iaex) { + } catch(@SuppressWarnings("unused") final IllegalArgumentException iaex) { /* * We don't care about this, because it indicates a key * is missing. diff --git a/base/src/main/java/bjc/utils/funcutils/FuncUtils.java b/base/src/main/java/bjc/utils/funcutils/FuncUtils.java index 4be6d78..2e55a3d 100644 --- a/base/src/main/java/bjc/utils/funcutils/FuncUtils.java +++ b/base/src/main/java/bjc/utils/funcutils/FuncUtils.java @@ -16,19 +16,19 @@ 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. + * @return The function transformed into a unary function returning a + * function. */ public static <A, B, C> Function<A, Function<B, C>> curry2(final BiFunction<A, B, C> func) { return arg1 -> arg2 -> { @@ -40,13 +40,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,11 +55,13 @@ 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 (val) -> { diff --git a/base/src/main/java/bjc/utils/ioutils/blocks/FilteredBlockReader.java b/base/src/main/java/bjc/utils/ioutils/blocks/FilteredBlockReader.java index c575f05..070713d 100644 --- a/base/src/main/java/bjc/utils/ioutils/blocks/FilteredBlockReader.java +++ b/base/src/main/java/bjc/utils/ioutils/blocks/FilteredBlockReader.java @@ -5,6 +5,12 @@ import java.io.IOException; import java.util.function.Consumer; import java.util.function.Predicate; +/** + * A block reader that only yields blocks that pass a predicate. + * + * @author EVE + * + */ public class FilteredBlockReader implements BlockReader { /* * The source of blocks. @@ -34,10 +40,29 @@ public class FilteredBlockReader implements BlockReader { */ private Consumer<Block> failAction; + /** + * Create a new filtered block reader with a given filter. + * + * @param src + * The place to read blocks from. + * @param predic + * The predicate to use to pass blocks. + */ public FilteredBlockReader(BlockReader src, Predicate<Block> predic) { this(src, predic, null); } + /** + * Create a new filtered block reader with a given filter that executes + * a specific action when a block fails. + * + * @param src + * The place to read blocks from. + * @param predic + * The predicate to use to pass blocks. + * @param failAct + * The action to take when a block fails. + */ public FilteredBlockReader(BlockReader src, Predicate<Block> predic, Consumer<Block> failAct) { source = src; pred = predic; diff --git a/base/src/main/java/bjc/utils/ioutils/blocks/FlatMappedBlockReader.java b/base/src/main/java/bjc/utils/ioutils/blocks/FlatMappedBlockReader.java index 16d2e44..bab463e 100644 --- a/base/src/main/java/bjc/utils/ioutils/blocks/FlatMappedBlockReader.java +++ b/base/src/main/java/bjc/utils/ioutils/blocks/FlatMappedBlockReader.java @@ -22,8 +22,8 @@ public class FlatMappedBlockReader implements BlockReader { /* * The current block, and any blocks pending from the last source block. */ - private Iterator<Block> pending; - private Block current; + private Iterator<Block> pending; + private Block current; /* * The operator to open blocks with. @@ -35,8 +35,16 @@ public class FlatMappedBlockReader implements BlockReader { */ private int blockNo; + /** + * Create a new flat-mapping block reader. + * + * @param source + * The source to read blocks from + * @param trans + * The transform to use. + */ public FlatMappedBlockReader(BlockReader source, Function<Block, List<Block>> trans) { - reader = source; + reader = source; transform = trans; blockNo = 0; @@ -52,7 +60,7 @@ public class FlatMappedBlockReader implements BlockReader { return current; } - @Override + @Override public boolean nextBlock() { /* * Attempt to get a new pending list if the one we have isn't @@ -67,7 +75,7 @@ public class FlatMappedBlockReader implements BlockReader { /* * Advance the iterator. */ - current = pending.next(); + current = pending.next(); blockNo += 1; return true; diff --git a/base/src/main/java/bjc/utils/ioutils/blocks/MappedBlockReader.java b/base/src/main/java/bjc/utils/ioutils/blocks/MappedBlockReader.java index 12fa848..72da16d 100644 --- a/base/src/main/java/bjc/utils/ioutils/blocks/MappedBlockReader.java +++ b/base/src/main/java/bjc/utils/ioutils/blocks/MappedBlockReader.java @@ -4,6 +4,12 @@ import java.io.IOException; import java.util.function.UnaryOperator; +/** + * A block reader that applies a transform to each block. + * + * @author EVE + * + */ public class MappedBlockReader implements BlockReader { private BlockReader reader; @@ -13,8 +19,16 @@ public class MappedBlockReader implements BlockReader { private int blockNo; + /** + * Create a new mapped block reader. + * + * @param source + * The source for blocks + * @param trans + * The transform to apply. + */ public MappedBlockReader(BlockReader source, UnaryOperator<Block> trans) { - reader = source; + reader = source; transform = trans; blockNo = 0; @@ -30,10 +44,10 @@ public class MappedBlockReader implements BlockReader { return current; } - @Override + @Override public boolean nextBlock() { if(hasNextBlock()) { - current = transform.apply(reader.next()); + current = transform.apply(reader.next()); blockNo += 1; return true; diff --git a/base/src/main/java/bjc/utils/ioutils/blocks/PushbackBlockReader.java b/base/src/main/java/bjc/utils/ioutils/blocks/PushbackBlockReader.java index 0cc9dea..924df39 100644 --- a/base/src/main/java/bjc/utils/ioutils/blocks/PushbackBlockReader.java +++ b/base/src/main/java/bjc/utils/ioutils/blocks/PushbackBlockReader.java @@ -27,7 +27,7 @@ public class PushbackBlockReader implements BlockReader { * Create a new pushback block reader. * * @param src - * The block reader to use when no blocks are queued. + * The block reader to use when no blocks are queued. */ public PushbackBlockReader(final BlockReader src) { source = src; @@ -50,22 +50,22 @@ public class PushbackBlockReader implements BlockReader { /* * Drain pushed-back blocks first. */ - if (!waiting.isEmpty()) { + if(!waiting.isEmpty()) { curBlock = waiting.pop(); blockNo += 1; return true; - } else { - final boolean succ = source.nextBlock(); - curBlock = source.getBlock(); + } - if (succ) { - blockNo += 1; - } + final boolean succ = source.nextBlock(); + curBlock = source.getBlock(); - return succ; + if(succ) { + blockNo += 1; } + + return succ; } @Override @@ -82,7 +82,7 @@ public class PushbackBlockReader implements BlockReader { * Insert a block at the back of the queue of pending blocks. * * @param blk - * The block to put at the back. + * The block to put at the back. */ public void addBlock(final Block blk) { waiting.add(blk); @@ -92,7 +92,7 @@ public class PushbackBlockReader implements BlockReader { * Insert a block at the front of the queue of pending blocks. * * @param blk - * The block to put at the front. + * The block to put at the front. */ public void pushBlock(final Block blk) { waiting.push(blk); diff --git a/base/src/main/java/bjc/utils/math/CardinalState.java b/base/src/main/java/bjc/utils/math/CardinalState.java new file mode 100644 index 0000000..f42fb85 --- /dev/null +++ b/base/src/main/java/bjc/utils/math/CardinalState.java @@ -0,0 +1,74 @@ +package bjc.utils.math; + +import java.util.Map; +import java.util.Map.Entry; +import java.util.function.BiFunction; +import java.util.function.LongPredicate; + +/* + * @TODO 2/12/18 Ben Culkin :AdditionalCardinals + * + * Add some built-in implementations for various things. + */ +/** + * Customizations for number cardinalization. + * + * @author EVE + * + */ +public class CardinalState { + /** + * Alias type for converting numbers to cardinals. + * + * @author EVE + * + */ + @FunctionalInterface + public interface Cardinalizer extends BiFunction<Long, CardinalState, String> { + + } + + /** + * Custom cardinals for numbers. + */ + public final Map<Long, String> customNumbers; + + /** + * Custom functions to apply to certain scales. + */ + public final Map<LongPredicate, Cardinalizer> customScales; + + /** + * Create a new set of cardinalization customizations. + * + * @param customNumbers + * The custom numbers to use. + * @param customScales + * The custom scales to use. + */ + public CardinalState(Map<Long, String> customNumbers, Map<LongPredicate, Cardinalizer> customScales) { + this.customNumbers = customNumbers; + this.customScales = customScales; + } + + /** + * Handle a custom cardinal number + * + * @param number + * The number to handle + * @return The number as a cardinal, or null if we don't handle it. + */ + public String handleCustom(long number) { + if(customNumbers.containsKey(number)) { + return customNumbers.get(number); + } + + for(Entry<LongPredicate, Cardinalizer> ent : customScales.entrySet()) { + if(ent.getKey().test(number)) { + return ent.getValue().apply(number, this); + } + } + + return null; + } +}
\ No newline at end of file diff --git a/base/src/main/java/bjc/utils/math/NumberUtils.java b/base/src/main/java/bjc/utils/math/NumberUtils.java index ed58bb8..53d6017 100644 --- a/base/src/main/java/bjc/utils/math/NumberUtils.java +++ b/base/src/main/java/bjc/utils/math/NumberUtils.java @@ -1,16 +1,29 @@ package bjc.utils.math; -import java.util.Map; -import java.util.function.BiFunction; -import java.util.function.LongPredicate; - -import static java.util.Map.Entry; - +/** + * A variety of functions for doing useful stuff with numbers. + * + * @author EVE + * + */ public class NumberUtils { /* - * @TODO Use U+305 for large roman numerals, as well as excels 'concise' + * @TODO 2/12/18 Ben Culkin :RomanExpansion + * + * Use U+305 for large roman numerals, as well as excels 'concise' * numerals (as implemented by roman()). */ + + /** + * Convert a number into a roman numeral. + * + * @param number + * The number to convert. + * @param classic + * Whether to use classic roman numerals (use IIII instead of IV, + * and such). + * @return The number as a roman numeral. + */ public static String toRoman(long number, boolean classic) { StringBuilder work = new StringBuilder(); @@ -119,40 +132,32 @@ public class NumberUtils { return work.toString(); } + /** + * Convert a number into a cardinal number. + * + * @param number + * The number to convert + * @return The number as a cardinal. + */ public static String toCardinal(long number) { return toCardinal(number, null); } - private static String[] cardinals = new String[] { "zero", "one", "two", "three", "four", "five", "six", - "seven", "eight", "nine", "ten", "eleven", "twelve", "thirteen", "fourteen", "fifteen", - "sixteen", "seventeen", "eighteen", "nineteen", "twenty", }; - - public static class CardinalState { - public final Map<Long, String> customNumbers; - public final Map<LongPredicate, BiFunction<Long, CardinalState, String>> customScales; - - public CardinalState(Map<Long, String> customNumbers, - Map<LongPredicate, BiFunction<Long, CardinalState, String>> customScales) { - this.customNumbers = customNumbers; - this.customScales = customScales; - } - - public String handleCustom(long number) { - if(customNumbers.containsKey(number)) { - return customNumbers.get(number); - } - - for(Entry<LongPredicate, BiFunction<Long, CardinalState, String>> ent : customScales - .entrySet()) { - if(ent.getKey().test(number)) { - return ent.getValue().apply(number, this); - } - } - - return null; - } - } - + private static String[] cardinals = new String[] { + "zero", "one", "two", "three", "four", "five", "six", "seven", "eight", "nine", "ten", "eleven", + "twelve", "thirteen", "fourteen", "fifteen", "sixteen", "seventeen", "eighteen", "nineteen", + "twenty", + }; + + /** + * Convert a number into a cardinal number. + * + * @param number + * The number to convert to a cardinal. + * @param custom + * The customizations to use. + * @return The number as a cardinal. + */ public static String toCardinal(long number, CardinalState custom) { if(custom != null) { String res = custom.handleCustom(number); @@ -189,14 +194,14 @@ public class NumberUtils { } } - long numTens = (long) (number / 10); + long numTens = number / 10; long numOnes = number % 10; return toCardinal(numTens, custom) + "-" + toCardinal(numOnes, custom); } if(number < 1000) { - long numHundreds = (long) (number / 100); + long numHundreds = number / 100; long rest = number % 100; return toCardinal(numHundreds, custom) + " hundred and " + toCardinal(rest, custom); @@ -204,7 +209,7 @@ public class NumberUtils { long MILLION = (long) (Math.pow(10, 6)); if(number < MILLION) { - long numThousands = (long) (number / 1000); + long numThousands = number / 1000; long rest = number % 1000; return toCardinal(numThousands, custom) + " thousand, " + toCardinal(rest, custom); @@ -212,7 +217,7 @@ public class NumberUtils { long BILLION = (long) (Math.pow(10, 9)); if(number < BILLION) { - long numMillions = (long) (number / MILLION); + long numMillions = number / MILLION; long rest = number % MILLION; return toCardinal(numMillions, custom) + " million, " + toCardinal(rest, custom); @@ -220,7 +225,7 @@ public class NumberUtils { long TRILLION = (long) (Math.pow(10, 12)); if(number < TRILLION) { - long numBillions = (long) (number / BILLION); + long numBillions = number / BILLION; long rest = number % BILLION; return toCardinal(numBillions, custom) + " billion, " + toCardinal(rest, custom); @@ -230,6 +235,13 @@ public class NumberUtils { "Numbers greater than or equal to 1 trillion are not supported yet."); } + /** + * Convert a number into an ordinal. + * + * @param number + * The number to convert to an ordinal. + * @return The number as an ordinal. + */ public static String toOrdinal(long number) { if(number < 0) { return "minus " + toOrdinal(number); @@ -314,7 +326,7 @@ public class NumberUtils { } long procNum = number % 100; - long tens = (long) (procNum / 10); + long tens = procNum / 10; long ones = procNum % 10; if(tens == 1) { @@ -356,6 +368,25 @@ public class NumberUtils { } } + /** + * Convert a number into a commafied string. + * + * @param val + * The number to convert. + * @param mincols + * The minimum number of columns to use. + * @param padchar + * The padding char to use. + * @param commaInterval + * The interval to place commas at. + * @param commaChar + * The character to use as a comma + * @param signed + * Whether or not to always display a sign + * @param radix + * The radix to use + * @return The number as a commafied string. + */ public static String toCommaString(long val, int mincols, char padchar, int commaInterval, char commaChar, boolean signed, int radix) { if(radix > radixChars.length) { @@ -383,7 +414,7 @@ public class NumberUtils { int radDigit = (int) (currVal % radix); work.append(radixChars[radDigit]); - currVal = (long) (currVal / radix); + currVal = currVal / radix; if(commaInterval != 0 && valCounter % commaInterval == 0) work.append(commaChar); } @@ -405,6 +436,21 @@ public class NumberUtils { return work.toString(); } + /** + * Convert a number to a normal commafied string. + * + * @param val + * The value to convert. + * @param mincols + * The minimum number of columns. + * @param padchar + * The padding char to use. + * @param signed + * Whether or not to display the sign. + * @param radix + * The radix to use. + * @return The number as a normal commafied string. + */ public static String toNormalString(long val, int mincols, char padchar, boolean signed, int radix) { return toCommaString(val, mincols, padchar, 0, ',', signed, radix); } diff --git a/base/src/main/java/bjc/utils/parserutils/defines/IteratedDefine.java b/base/src/main/java/bjc/utils/parserutils/defines/IteratedDefine.java index 552b471..83ef8a8 100644 --- a/base/src/main/java/bjc/utils/parserutils/defines/IteratedDefine.java +++ b/base/src/main/java/bjc/utils/parserutils/defines/IteratedDefine.java @@ -8,6 +8,12 @@ import java.util.regex.Pattern; import bjc.utils.data.CircularIterator; +/** + * A define that has a set of replacements to use. + * + * @author EVE + * + */ public class IteratedDefine implements UnaryOperator<String> { private Pattern patt; @@ -17,12 +23,12 @@ public class IteratedDefine implements UnaryOperator<String> { * Create a new iterated define. * * @param pattern - * The pattern to use for matching. + * The pattern to use for matching. * @param circular - * Whether or not to loop through the list of replacers, or just - * repeat the last one. + * Whether or not to loop through the list of replacers, or just + * repeat the last one. * @param replacers - * The set of replacers to use. + * The set of replacers to use. */ public IteratedDefine(Pattern pattern, boolean circular, String... replacers) { patt = pattern; @@ -32,7 +38,7 @@ public class IteratedDefine implements UnaryOperator<String> { @Override public String apply(String ln) { - Matcher mat = patt.matcher(ln); + Matcher mat = patt.matcher(ln); StringBuffer sb = new StringBuffer(); while(mat.find()) { |
