diff options
19 files changed, 335 insertions, 448 deletions
diff --git a/base/src/main/java/bjc/utils/cli/CommandHelp.java b/base/src/main/java/bjc/utils/cli/CommandHelp.java index d07c9d5..a13a42f 100644 --- a/base/src/main/java/bjc/utils/cli/CommandHelp.java +++ b/base/src/main/java/bjc/utils/cli/CommandHelp.java @@ -1,30 +1,24 @@ package bjc.utils.cli; -/** - * Interface for the help entry for a command +/** Interface for the help entry for a command * - * @author ben - */ + * @author ben */ public interface CommandHelp { - /** - * Get the description of a command. + /** Get the description of a command. * - * @return The description of a command - */ + * @return The description of a command */ String getDescription(); - /** - * Get the summary line for a command. + /** Get the summary line for a command. * * A summary line should consist of a string of the following format * * <pre> - * "<command-name&rt;\t<command-summary&rt;" + * "<command-name>\t<command-summary>" * </pre> * * where anything in angle brackets should be filled in. * - * @return The summary line line for a command - */ + * @return The summary line line for a command */ String getSummary(); } diff --git a/base/src/main/java/bjc/utils/components/ComponentDescriptionFileParser.java b/base/src/main/java/bjc/utils/components/ComponentDescriptionFileParser.java index 87a749e..02e47fe 100644 --- a/base/src/main/java/bjc/utils/components/ComponentDescriptionFileParser.java +++ b/base/src/main/java/bjc/utils/components/ComponentDescriptionFileParser.java @@ -13,10 +13,10 @@ import bjc.utils.ioutils.RuleBasedConfigReader; * The file format is based entirely off of pragma statements, and should have * at least one of each of the following statements * <ul> - * <li>pragma name <component-name&rt;</li> - * <li>pragma author <component-version&rt;</li> - * <li>pragma description <component-description&rt;</li> - * <li>pragma version <component-version&rt;</li> + * <li>pragma name <component-name></li> + * <li>pragma author <component-version></li> + * <li>pragma description <component-description></li> + * <li>pragma version <component-version></li> * </ul> * * @author ben diff --git a/base/src/main/java/bjc/utils/funcutils/LambdaLock.java b/base/src/main/java/bjc/utils/funcutils/LambdaLock.java index c7ba09a..c974c8c 100644 --- a/base/src/main/java/bjc/utils/funcutils/LambdaLock.java +++ b/base/src/main/java/bjc/utils/funcutils/LambdaLock.java @@ -5,12 +5,10 @@ import java.util.concurrent.locks.ReadWriteLock; import java.util.concurrent.locks.ReentrantReadWriteLock; import java.util.function.Supplier; -/** - * A wrapper around a {@link ReadWriteLock} to ensure that the lock is used +/** A wrapper around a {@link ReadWriteLock} to ensure that the lock is used * properly. * - * @author EVE - */ + * @author EVE */ public class LambdaLock { /* The read lock. */ private final Lock readLock; @@ -22,26 +20,21 @@ public class LambdaLock { this(new ReentrantReadWriteLock()); } - /** - * Create a new lambda-enabled lock. + /** Create a new lambda-enabled lock. * - * @param lck - * The lock to wrap. - */ + * @param lck The lock to wrap. */ public LambdaLock(final ReadWriteLock lck) { readLock = lck.readLock(); writeLock = lck.writeLock(); } - /** - * Execute an action with the read lock taken. + /** Execute an action with the read lock taken. * * @param <T> The type returned by the action. * * @param supp The action to call. * - * @return The result of the action. - */ + * @return The result of the action. */ public <T> T read(final Supplier<T> supp) { readLock.lock(); @@ -59,8 +52,7 @@ public class LambdaLock { * * @param supp The action to call. * - * @return The result of the action. - */ + * @return The result of the action. */ public <T> T write(final Supplier<T> supp) { writeLock.lock(); @@ -74,8 +66,7 @@ public class LambdaLock { /** * Execute an action with the read lock taken. * - * @param action - * The action to call. + * @param action The action to call. */ public void read(final Runnable action) { readLock.lock(); diff --git a/base/src/main/java/bjc/utils/funcutils/ListUtils.java b/base/src/main/java/bjc/utils/funcutils/ListUtils.java index ab32ccc..47141c2 100644 --- a/base/src/main/java/bjc/utils/funcutils/ListUtils.java +++ b/base/src/main/java/bjc/utils/funcutils/ListUtils.java @@ -6,42 +6,32 @@ import java.util.function.*; import bjc.funcdata.FunctionalList; import bjc.funcdata.ListEx; -/** - * Utilities for manipulating FunctionalLists and regular Collections lists that +/** Utilities for manipulating FunctionalLists and regular Collections lists that * don't belong in the class itself. * - * @author ben - */ + * @author ben */ public class ListUtils { /* The max amount of times to try to partition things. */ private static final int MAX_NTRIESPART = 50; - /** - * Collapse a string of tokens into a single string without adding any spaces. + /** Collapse a string of tokens into a single string without adding any spaces. * - * @param input - * The list of tokens to collapse. + * @param input The list of tokens to collapse. * - * @return The collapsed string of tokens. - */ + * @return The collapsed string of tokens. */ public static String collapseTokens(final ListEx<String> input) { if (input == null) throw new NullPointerException("Input must not be null"); return collapseTokens(input, ""); } - /** - * Collapse a string of tokens into a single string, adding the desired + /** Collapse a string of tokens into a single string, adding the desired * separator after each token. * - * @param input - * The list of tokens to collapse. - * - * @param seperator - * The separator to use for separating tokens. + * @param input The list of tokens to collapse. + * @param seperator The separator to use for separating tokens. * - * @return The collapsed string of tokens. - */ + * @return The collapsed string of tokens. */ public static String collapseTokens(final ListEx<String> input, final String seperator) { if (input == null) throw new NullPointerException("Input must not be null"); @@ -67,25 +57,17 @@ public class ListUtils { } } - /** - * Select a number of random items from the list without replacement. - * - * @param <E> - * The type of items to select. + /** Select a number of random items from the list without replacement. * - * @param list - * The list to select from. + * @param <E> The type of items to select. * - * @param number - * The number of items to selet. - * - * @param rng - * A function that creates a random number from 0 to the desired + * @param list The list to select from. + * @param number The number of items to selet. + * @param rng A function that creates a random number from 0 to the desired * number. * * @return A new list containing the desired number of items randomly selected - * from the specified list without replacement. - */ + * from the specified list without replacement. */ public static <E> ListEx<E> drawWithoutReplacement(final ListEx<E> list, final int number, final Function<Integer, Integer> rng) { final ListEx<E> selected = new FunctionalList<>(new ArrayList<>(number)); @@ -111,25 +93,17 @@ public class ListUtils { return selected; } - /** - * Select a number of random items from the list, with replacement. - * - * @param <E> - * The type of items to select. + /** Select a number of random items from the list, with replacement. * - * @param list - * The list to select from. + * @param <E> The type of items to select. * - * @param number - * The number of items to select. - * - * @param rng - * A function that creates a random number from 0 to the desired + * @param list The list to select from. + * @param number The number of items to select. + * @param rng A function that creates a random number from 0 to the desired * number. * * @return A new list containing the desired number of items randomly selected - * from the specified list. - */ + * from the specified list. */ public static <E> ListEx<E> drawWithReplacement(final ListEx<E> list, final int number, final Function<Integer, Integer> rng) { final ListEx<E> selected = new FunctionalList<>(new ArrayList<>(number)); @@ -139,25 +113,16 @@ public class ListUtils { return selected; } - /** - * Partition a list into a list of lists, where each element can count for more + /** Partition a list into a list of lists, where each element can count for more * than one element in a partition. * - * @param <E> - * The type of elements in the list to partition. - * - * @param input - * The list to partition. + * @param <E> The type of elements in the list to partition. * - * @param counter - * The function to determine the count for each element - * for. + * @param input The list to partition. + * @param counter The function to determine the count for each element for. + * @param partitionSize The number of elements to put in each partition. * - * @param partitionSize - * The number of elements to put in each partition. - * - * @return A list partitioned according to the above rules. - */ + * @return A list partitioned according to the above rules. */ public static <E> ListEx<ListEx<E>> groupPartition(final ListEx<E> input, final Function<E, Integer> counter, final int partitionSize) { if (input == null) { @@ -200,17 +165,13 @@ public class ListUtils { throw new IllegalArgumentException(msg); } - /** - * Merge the contents of a bunch of lists together into a single list. + /** Merge the contents of a bunch of lists together into a single list. * - * @param <E> - * The type of value in this lists. + * @param <E> The type of value in this lists. * - * @param lists - * The values in the lists to merge. + * @param lists The values in the lists to merge. * - * @return A list containing all the elements of the lists. - */ + * @return A list containing all the elements of the lists. */ @SafeVarargs public static <E> ListEx<E> mergeLists(final ListEx<E>... lists) { final ListEx<E> returned = new FunctionalList<>(); @@ -222,30 +183,19 @@ public class ListUtils { return returned; } - /** - * Pad the provided list out to the desired size. - * - * @param <E> - * The type of elements in the list. + /** Pad the provided list out to the desired size. * - * @param list - * The list to pad out. + * @param <E> The type of elements in the list. * - * @param counter - * The function to count elements with. - * - * @param size - * The desired size of the list. - * - * @param padder - * The function to get elements to pad with. + * @param list The list to pad out. + * @param counter The function to count elements with. + * @param size The desired size of the list. + * @param padder The function to get elements to pad with. * * @return The list, padded to the desired size. * - * @throws IllegalArgumentException - * If the list couldn't be padded to the - * desired size. - */ + * @throws IllegalArgumentException If the list couldn't be padded to the + * desired size. */ public static <E> ListEx<E> padList(final ListEx<E> list, final Function<E, Integer> counter, final int size, final Supplier<E> padder) { @@ -293,13 +243,11 @@ public class ListUtils { return returned; } - /** - * Convert a list of longs into an array of longs. + /** Convert a list of longs into an array of longs. * - * @param list - * The list to convert. - * @return The list as an array. - */ + * @param list The list to convert. + * + * @return The list as an array. */ public static long[] toPrimitive(List<Long> list) { long[] res = new long[list.size()]; @@ -313,16 +261,15 @@ public class ListUtils { return res; } - /** - * Generate all of the permuations of a list. + /** Generate all of the permuations of a list. * * This is a version of Algorith P (Plain Changes) from Knuth (vol 4A, pg 322) * - * @param <T> The type of elements in the list. - * + * @param <T> The type of element in the list. + * * @param list The list to generate permutations from. - * @return The list of permutations of the list. - */ + * + * @return The list of permutations of the list. */ public static <T> List<List<T>> permuteList(List<T> list) { List<List<T>> permutes = new LinkedList<>(); @@ -410,14 +357,11 @@ public class ListUtils { } } -/** - * Implements a single group partitioning pass on a list. +/** Implements a single group partitioning pass on a list. * * @author ben * - * @param <E> - * The type of element in the list being partitioned - */ + * @param <E> The type of element in the list being partitioned */ class GroupPartIteration<E> implements Consumer<E> { /* The list we're returning. */ private final ListEx<ListEx<E>> returnedList; @@ -435,23 +379,14 @@ class GroupPartIteration<E> implements Consumer<E> { /* The function to use to count an item. */ private final Function<E, Integer> elementCounter; - /** - * Create a new group partitioning iteration. - * - * @param returned - * The list containing all of the existing partitions. + /** Create a new group partitioning iteration. * - * @param rejects - * The items that have been rejected from a partition for being + * @param returned The list containing all of the existing partitions. + * @param rejects The items that have been rejected from a partition for being * too large. - * - * @param nPerPart - * The combined value of items that should go into each + * @param nPerPart The combined value of items that should go into each * partition. - * - * @param eleCount - * The function to use to determine the value of an item. - */ + * @param eleCount The function to use to determine the value of an item. */ public GroupPartIteration(final ListEx<ListEx<E>> returned, final ListEx<E> rejects, final int nPerPart, final Function<E, Integer> eleCount) { this.returnedList = returned; @@ -465,8 +400,7 @@ class GroupPartIteration<E> implements Consumer<E> { @Override public void accept(final E value) { - final boolean shouldStartPartition - = numberInCurrentPartition >= numberPerPartition; + final boolean shouldStartPartition = numberInCurrentPartition >= numberPerPartition; if (shouldStartPartition) { returnedList.add(currentPartition); diff --git a/base/src/main/java/bjc/utils/funcutils/SetUtils.java b/base/src/main/java/bjc/utils/funcutils/SetUtils.java index b721d10..799eadc 100644 --- a/base/src/main/java/bjc/utils/funcutils/SetUtils.java +++ b/base/src/main/java/bjc/utils/funcutils/SetUtils.java @@ -5,20 +5,17 @@ import java.util.HashSet; import java.util.List; import java.util.Set; -/** - * Various utility functions dealing with sets. +/** Various utility functions dealing with sets. * - * @author bjculkin - * - */ + * @author bjculkin */ public class SetUtils { - /** - * Create a power-set (set of all subsets) of a given set. + /** Create a power-set (set of all subsets) of a given set. * - * @param <T> The type of elements contained in the set. + * @param <T> The type of element in the set. + * * @param originalSet The set to create a power-set of. - * @return The power-set of the set. - */ + * + * @return The power-set of the set. */ public static <T> Set<Set<T>> powerSet(Set<T> originalSet) { Set<Set<T>> sets = new HashSet<>(); @@ -52,19 +49,17 @@ public class SetUtils { return sets; } - /** - * Utility method for set construction. + /** Utility method for set construction. * - * @param <T> The type of elements in the set. + * @param <T> The type of element in the set. + * * @param elms The elements to stick in the set. - * @return A set containing the specified elements. - */ + * + * @return A set containing the specified elements. */ @SafeVarargs public static <T> Set<T> toSet(T... elms) { Set<T> set = new HashSet<>(); - for (T elm : elms) set.add(elm); - return set; } } diff --git a/base/src/main/java/bjc/utils/funcutils/TestUtils.java b/base/src/main/java/bjc/utils/funcutils/TestUtils.java index bf38cbe..860a565 100644 --- a/base/src/main/java/bjc/utils/funcutils/TestUtils.java +++ b/base/src/main/java/bjc/utils/funcutils/TestUtils.java @@ -5,33 +5,28 @@ import static org.junit.Assert.assertEquals; import java.util.Iterator; import java.util.List; -/** - * Utilities for testing. +/** Utilities for testing. * - * @author bjculkin - * - */ + * @author bjculkin */ public class TestUtils { - /** - * Assert an iterator provides a particular sequence of values. + /** Assert an iterator provides a particular sequence of values. + * + * @param <T> The type of value. * - * @param <T> The type of the values. * @param src The iterator to pull values from. - * @param vals The values to expect from the iterator. - */ + * @param vals The values to expect from the iterator. */ @SafeVarargs public static <T> void assertIteratorEquals(Iterator<T> src, T... vals) { for (T val : vals) assertEquals(val, src.next()); } - /** - * Assert an iterator provides a particular sequence of values. + /** Assert an iterator provides a particular sequence of values. + * + * @param <T> The type of value. * - * @param <T> The type of the values. * @param src The iterator to pull values from. * @param hasMore The expected value of hasNext for the iterator. - * @param vals The values to expect from the iterator. - */ + * @param vals The values to expect from the iterator. */ @SafeVarargs public static <T> void assertIteratorEquals(boolean hasMore, Iterator<T> src, T... vals) { @@ -49,13 +44,12 @@ public class TestUtils { assertEquals(hasMore, src.hasNext()); } - /** - * Assert that a list has a given set of contents. + /** Assert that a list has a given set of contents. + * + * @param <T> The type of value in the list. * - * @param <T> The type of the values. * @param src The list of actual elements. - * @param exps The list of expected elements. - */ + * @param exps The list of expected elements. */ @SafeVarargs public static <T> void assertListEquals(List<T> src, T... exps) { assertEquals(exps.length, src.size()); diff --git a/base/src/main/java/bjc/utils/funcutils/TreeUtils.java b/base/src/main/java/bjc/utils/funcutils/TreeUtils.java index 5a1d9c8..daab8a1 100644 --- a/base/src/main/java/bjc/utils/funcutils/TreeUtils.java +++ b/base/src/main/java/bjc/utils/funcutils/TreeUtils.java @@ -6,20 +6,18 @@ import java.util.function.*; import bjc.data.*; import bjc.funcdata.*; -/** - * Implements various utilities for trees. +/** Implements various utilities for trees. * - * @author Benjamin Culkin - */ + * @author Benjamin Culkin */ public class TreeUtils { - /** - * Convert a tree into a list of outline nodes that match a certain path. + /** Convert a tree into a list of outline nodes that match a certain path. + * + * @param <T> The type contained in the tree. * - * @param <T> The type of the values. * @param tre The tree to outline. * @param leafMarker The path to mark nodes with. - * @return The list of marked paths. - */ + * + * @return The list of marked paths. */ public static <T> ListEx<ListEx<T>> outlineTree(Tree<T> tre, Predicate<T> leafMarker) { ListEx<ListEx<T>> paths = new FunctionalList<>(); @@ -53,15 +51,15 @@ public class TreeUtils { } } - /** - * Performs 'variable substitution' or something along those lines on a tree. + /** Performs 'variable substitution' or something along those lines on a tree. * * @param <ContainedType> The type of element contained in the tree. + * * @param tree The tree to do expansion in. * @param marker The function to mark which nodes should be expanded. * @param expander The function to expand nodes. - * @return A transformed copy of the tree. - */ + * + * @return A transformed copy of the tree. */ public static <ContainedType> Tree<ContainedType> substitute( Tree<ContainedType> tree, Predicate<ContainedType> marker, @@ -75,14 +73,14 @@ public class TreeUtils { return tree; } - /** - * Performs 'variable substitution' or something along those lines on a tree. + /** Performs 'variable substitution' or something along those lines on a tree. * * @param <ContainedType> The type of element contained in the tree. + * * @param tree The tree to do expansion in. * @param environment A map which contains the variables to substitute. - * @return A transformed copy of the tree. - */ + * + * @return A transformed copy of the tree. */ public static <ContainedType> Tree<ContainedType> substitute( Tree<ContainedType> tree, MapEx<ContainedType, Tree<ContainedType>> environment) { diff --git a/base/src/main/java/bjc/utils/ioutils/LevelSplitter.java b/base/src/main/java/bjc/utils/ioutils/LevelSplitter.java index 93bc424..e808bec 100644 --- a/base/src/main/java/bjc/utils/ioutils/LevelSplitter.java +++ b/base/src/main/java/bjc/utils/ioutils/LevelSplitter.java @@ -7,7 +7,7 @@ import java.util.regex.Pattern; /** Splits a string on a delimiter, respecting grouping delimiters. * - * By default, grouping delimiters are (), [], {}, and <>, as well as single and + * By default, grouping delimiters are (), [], {}, and <>, as well as single and * double quoted strings. * * @author bjculkin */ diff --git a/base/src/main/java/bjc/utils/ioutils/blocks/Block.java b/base/src/main/java/bjc/utils/ioutils/blocks/Block.java index b8c611b..313a91b 100644 --- a/base/src/main/java/bjc/utils/ioutils/blocks/Block.java +++ b/base/src/main/java/bjc/utils/ioutils/blocks/Block.java @@ -1,55 +1,38 @@ package bjc.utils.ioutils.blocks; -/** - * Represents a block of text read in from a source. +/** Represents a block of text read in from a source. * - * @author EVE - * - */ + * @author EVE */ public class Block { - /** - * The contents of this block. - */ + /** The contents of this block. */ public final String contents; - /** - * The line of the source this block started on. - */ + /** The line of the source this block started on. */ public final int startLine; - /** - * The line of the source this block ended on. - */ + /** The line of the source this block ended on. */ public final int endLine; - /** - * The number of this block. - */ + /** The number of this block. */ public final int blockNo; - /** - * The line offset number for this block. + /** The line offset number for this block. * * Essentially, this is the absolute number of lines that this block is into * whatever source it came from, where as startLine and endLine are relative to - * the BlockReader this Block is from. - */ + * the BlockReader this Block is from. */ public int lineOffset; - /** - * Create a new block. + /** Create a new block. * - * @param blockNo - * The number of this block. - * @param contents - * The contents of this block. - * @param startLine - * The line this block started on. - * @param endLine - * The line this block ended. - */ - public Block(final int blockNo, final String contents, final int startLine, - final int endLine) { + * @param blockNo The number of this block. + * @param contents The contents of this block. + * @param startLine The line this block started on. + * @param endLine The line this block ended. */ + public Block( + final int blockNo, final String contents, final int startLine, + final int endLine) + { this.contents = contents; this.startLine = startLine; this.endLine = endLine; @@ -62,8 +45,11 @@ public class Block { String fmt = "Block #%d (from lines %d (%d) to %d (%d)), length: %d characters"; - return String.format(fmt, blockNo, startLine + lineOffset, startLine, - endLine + lineOffset, endLine + lineOffset, contents.length()); + return String.format(fmt, + blockNo, + startLine + lineOffset, startLine, + endLine + lineOffset, endLine, + contents.length()); } String fmt = "Block #%d (from lines %d to %d), length: %d characters"; diff --git a/base/src/main/java/bjc/utils/ioutils/blocks/BlockReader.java b/base/src/main/java/bjc/utils/ioutils/blocks/BlockReader.java index f111244..0968050 100644 --- a/base/src/main/java/bjc/utils/ioutils/blocks/BlockReader.java +++ b/base/src/main/java/bjc/utils/ioutils/blocks/BlockReader.java @@ -1,58 +1,50 @@ package bjc.utils.ioutils.blocks; -import java.io.IOException; -import java.util.Iterator; -import java.util.function.Consumer; +import java.io.*; +import java.util.*; +import java.util.function.*; -/** - * A source of blocks of characters, marked with line numbers as to block +// @NOTE Ben Culkin 12/16/2020 :IterableIterator +// Having this class implement both Iterator and Iterable is somewhat suspect. +// I understand why it was done, because that allows easy use of the 'for-each' +// loop semantics, but there is a question of whether it is a good idea to have +// instances of Iterable that will always give the same Iterator without +// explicitly saying that is what they are doing. + +/** A source of blocks of characters, marked with line numbers as to block * start/block end. * - * @author bjculkin - * - */ -public interface BlockReader extends AutoCloseable, Iterator<Block>, Iterable<Block> { - /** - * Check if this reader has an available block. + * @author bjculkin */ +public interface +BlockReader extends AutoCloseable, Iterator<Block>, Iterable<Block> { + /** Check if this reader has an available block. * - * @return Whether or not another block is available. - */ + * @return Whether or not another block is available. */ boolean hasNextBlock(); - /** - * Get the current block. + /** Get the current block. * - * @return The current block, or null if there is no current block. - */ + * @return The current block, or null if there is no current block. */ Block getBlock(); - /** - * Move to the next block. + /** Move to the next block. * - * @return Whether or not the next block was successfully read. - */ + * @return Whether or not the next block was successfully read. */ boolean nextBlock(); - /** - * Retrieve the number of blocks that have been read so far. + /** Retrieve the number of blocks that have been read so far. * - * @return The number of blocks read so far. - */ + * @return The number of blocks read so far. */ int getBlockCount(); @Override void close() throws IOException; - /* - * Methods with default impls. - */ + /* Methods with default impls. */ - /** - * Execute an action for each remaining block. + /** Execute an action for each remaining block. * - * @param action - * The action to execute for each block - */ + * @param action The action to execute for each block. */ default void forEachBlock(final Consumer<Block> action) { while (hasNext()) { action.accept(next()); diff --git a/base/src/main/java/bjc/utils/ioutils/blocks/BlockReaders.java b/base/src/main/java/bjc/utils/ioutils/blocks/BlockReaders.java index 615af85..16d50c0 100644 --- a/base/src/main/java/bjc/utils/ioutils/blocks/BlockReaders.java +++ b/base/src/main/java/bjc/utils/ioutils/blocks/BlockReaders.java @@ -1,81 +1,62 @@ package bjc.utils.ioutils.blocks; -import java.io.Reader; +import java.io.*; -/** - * Utility methods for constructing instances of {@link BlockReader} +/** Utility methods for constructing instances of {@link BlockReader} * - * @author bjculkin - * - */ + * @author bjculkin */ public class BlockReaders { - /** - * Create a new simple block reader that works off a regex. - * - * @param blockDelim - * The regex that separates blocks. + /** Create a new simple block reader that works off a regex. * - * @param source - * The reader to get blocks from. + * @param blockDelim The regex that separates blocks. + * @param source The reader to get blocks from. * - * @return A configured simple reader. - */ - public static SimpleBlockReader simple(final String blockDelim, final Reader source) { + * @return A configured simple reader. */ + public static SimpleBlockReader simple( + final String blockDelim, final Reader source) + { return new SimpleBlockReader(blockDelim, source); } - /** - * Create a new pushback block reader. + /** Create a new pushback block reader. * - * @param src - * The block reader to read blocks from. + * @param src The block reader to read blocks from. * - * @return A configured pushback reader. - */ + * @return A configured pushback reader. */ public static PushbackBlockReader pushback(final BlockReader src) { return new PushbackBlockReader(src); } - /** - * Create a new triggered block reader. + /** Create a new triggered block reader. * - * @param source - * The block reader to read blocks from. + * @param source The block reader to read blocks from. + * @param action The action to execute before reading a block. * - * @param action - * The action to execute before reading a block. - * - * @return A configured triggered block reader. - */ - public static BlockReader trigger(final BlockReader source, final Runnable action) { + * @return A configured triggered block reader. */ + public static BlockReader trigger( + final BlockReader source, final Runnable action) + { return new TriggeredBlockReader(source, action); } - /** - * Create a new layered block reader. - * - * @param primary - * The first source to read blocks from. + /** Create a new layered block reader. * - * @param secondary - * The second source to read blocks from. + * @param primary The first source to read blocks from. + * @param secondary The second source to read blocks from. * - * @return A configured layered block reader. - */ - public static BlockReader layered(final BlockReader primary, - final BlockReader secondary) { + * @return A configured layered block reader. */ + public static BlockReader layered( + final BlockReader primary, final BlockReader secondary) + { return new LayeredBlockReader(primary, secondary); } - /** - * Create a new serial block reader. + /** Create a new serial block reader. * - * @param readers - * The readers to pull from, in the order to pull from them. + * @param readers The readers to pull from, in the order to pull from them. * - * @return A configured serial block reader. - */ + * @return A configured serial block reader. */ public static BlockReader serial(final BlockReader... readers) { return new SerialBlockReader(readers); } -}
\ No newline at end of file +} diff --git a/base/src/main/java/bjc/utils/ioutils/blocks/BoundBlockReader.java b/base/src/main/java/bjc/utils/ioutils/blocks/BoundBlockReader.java index 0bd0991..04a8375 100644 --- a/base/src/main/java/bjc/utils/ioutils/blocks/BoundBlockReader.java +++ b/base/src/main/java/bjc/utils/ioutils/blocks/BoundBlockReader.java @@ -1,30 +1,20 @@ package bjc.utils.ioutils.blocks; -import java.io.IOException; -import java.util.function.BooleanSupplier; -import java.util.function.Supplier; +import java.io.*; +import java.util.function.*; -/** - * A block reader composed from functions. +/** A block reader composed from functions. * - * @author EVE - * - */ + * @author EVE */ public class BoundBlockReader implements BlockReader { - /** - * A function that serves to close an I/O source. - * - * @author EVE + /** A function that serves to close an I/O source. * - */ + * @author EVE */ @FunctionalInterface public interface Closer { - /** - * Close the I/O source this is attached to. + /** Close the I/O source this is attached to. * - * @throws IOException - * If something goes wrong - */ + * @throws IOException If something goes wrong */ public void close() throws IOException; } @@ -36,21 +26,19 @@ public class BoundBlockReader implements BlockReader { private int blockNo; - /** - * Create a new bound block reader. + /** Create a new bound block reader. * - * @param blockChecker - * Predicate for checking if a block is available - * @param blockGetter - * Function to retrieve a block - * @param blockCloser - * Function to close a block source - */ - public BoundBlockReader(BooleanSupplier blockChecker, Supplier<Block> blockGetter, - Closer blockCloser) { + * @param blockChecker Predicate for checking if a block is available + * @param blockGetter Function to retrieve a block + * @param blockCloser Function to close a block source */ + public BoundBlockReader( + BooleanSupplier blockChecker, + Supplier<Block> blockGetter, + Closer blockCloser) + { checker = blockChecker; - getter = blockGetter; - closer = blockCloser; + getter = blockGetter; + closer = blockCloser; blockNo = 0; } @@ -72,9 +60,9 @@ public class BoundBlockReader implements BlockReader { blockNo += 1; return true; + } else { + return false; } - - return false; } @Override diff --git a/base/src/main/java/bjc/utils/ioutils/blocks/SimpleBlockReader.java b/base/src/main/java/bjc/utils/ioutils/blocks/SimpleBlockReader.java index 94405c9..3eb57d0 100644 --- a/base/src/main/java/bjc/utils/ioutils/blocks/SimpleBlockReader.java +++ b/base/src/main/java/bjc/utils/ioutils/blocks/SimpleBlockReader.java @@ -1,49 +1,34 @@ package bjc.utils.ioutils.blocks; -import java.io.IOException; -import java.io.Reader; -import java.util.NoSuchElementException; -import java.util.Scanner; -import java.util.regex.Pattern; +import java.io.*; +import java.util.*; +import java.util.regex.*; -import bjc.utils.funcutils.StringUtils; +import bjc.utils.funcutils.*; -/** - * Simple implementation of {@link BlockReader}. +/** Simple implementation of {@link BlockReader}. * * NOTE: The EOF marker is always treated as a delimiter. You are expected to * handle blocks that may be shorter than you expect. * - * @author EVE - * - */ + * @author EVE */ public class SimpleBlockReader implements BlockReader { - /* - * I/O source for blocks. - */ + /* I/O source for blocks. */ private final Scanner blockReader; - /* - * The current block. - */ + /* The current block. */ private Block currBlock; - /* - * Info about the current block. - */ + /* Info about the current block. */ private int blockNo; private int lineNo; - /** - * Create a new block reader. + /** Create a new block reader. * - * @param blockDelim - * The pattern that separates blocks. Note that the end of + * @param blockDelim The pattern that separates blocks. Note that the end of * file is always considered to end a block. * - * @param source - * The source to read blocks from. - */ + * @param source The source to read blocks from. */ public SimpleBlockReader(final String blockDelim, final Reader source) { blockReader = new Scanner(source); @@ -55,17 +40,13 @@ public class SimpleBlockReader implements BlockReader { lineNo = 1; } - /** - * Create a new block reader. + /** Create a new block reader. * - * @param blockDelim - * The pattern that separates blocks. Note that the end of + * @param blockDelim The pattern that separates blocks. Note that the end of * file is always considered to end a block. * - * @param source - * The source to read blocks from. NOTE: This does modify the - * provided scanner. - */ + * @param source The source to read blocks from. NOTE: This does modify the + * provided scanner. */ public SimpleBlockReader(final String blockDelim, final Scanner source) { blockReader = source; @@ -90,19 +71,19 @@ public class SimpleBlockReader implements BlockReader { @Override public boolean nextBlock() { try { - /* - * Read in a new block, and keep the line numbers sane. - */ + /* Read in a new block, and keep the line numbers sane. */ final String blockContents = blockReader.next(); + final int numLines = StringUtils.countMatches(blockContents, "\\R"); + final int blockStartLine = lineNo; - final int blockEndLine - = lineNo + StringUtils.countMatches(blockContents, "\\R"); + final int blockEndLine = lineNo + numLines; - lineNo = blockEndLine; + lineNo = blockEndLine; blockNo += 1; - currBlock = new Block(blockNo, blockContents, blockStartLine, blockEndLine); + currBlock = new Block( + blockNo, blockContents, blockStartLine, blockEndLine); return true; } catch (final NoSuchElementException nseex) { @@ -124,19 +105,17 @@ public class SimpleBlockReader implements BlockReader { blockReader.close(); } - /** - * Set the delimiter used to separate blocks. + /** Set the delimiter used to separate blocks. * - * @param delim - * The delimiter used to separate blocks. - */ + * @param delim The delimiter used to separate blocks. */ public void setDelimiter(final String delim) { blockReader.useDelimiter(delim); } @Override public String toString() { - return String.format("SimpleBlockReader [currBlock=%s, blockNo=%s]", currBlock, - blockNo); + return String.format( + "SimpleBlockReader [currBlock=%s, blockNo=%s]", + currBlock, blockNo); } } diff --git a/base/src/main/java/bjc/utils/ioutils/blocks/ToggledBlockReader.java b/base/src/main/java/bjc/utils/ioutils/blocks/ToggledBlockReader.java index 12568c8..d1737f5 100644 --- a/base/src/main/java/bjc/utils/ioutils/blocks/ToggledBlockReader.java +++ b/base/src/main/java/bjc/utils/ioutils/blocks/ToggledBlockReader.java @@ -1,15 +1,12 @@ package bjc.utils.ioutils.blocks; -import java.io.IOException; +import java.io.*; -import bjc.data.BooleanToggle; +import bjc.data.*; -/** - * A block reader that toggles between two sources. +/** A block reader that toggles between two sources. * - * @author EVE - * - */ + * @author EVE */ public class ToggledBlockReader implements BlockReader { private BlockReader leftSource; private BlockReader rightSource; @@ -19,16 +16,12 @@ public class ToggledBlockReader implements BlockReader { private int blockNo; - /** - * Create a new toggling block reader. + /** Create a new toggling block reader. * - * @param left - * The first block reader to use. - * @param right - * The second block reader to use. - */ + * @param left The first block reader to use. + * @param right The second block reader to use. */ public ToggledBlockReader(BlockReader left, BlockReader right) { - leftSource = left; + leftSource = left; rightSource = right; blockNo = 0; @@ -38,20 +31,14 @@ public class ToggledBlockReader implements BlockReader { @Override public boolean hasNextBlock() { - if (leftToggle.peek()) { - return leftSource.hasNextBlock(); - } - - return rightSource.hasNextBlock(); + if (leftToggle.peek()) return leftSource.hasNextBlock(); + else return rightSource.hasNextBlock(); } @Override public Block getBlock() { - if (leftToggle.peek()) { - return leftSource.getBlock(); - } - - return rightSource.getBlock(); + if (leftToggle.peek()) return leftSource.getBlock(); + else return rightSource.getBlock(); } @Override @@ -64,8 +51,8 @@ public class ToggledBlockReader implements BlockReader { succ = rightSource.nextBlock(); } - if (succ) - blockNo += 1; + if (succ) blockNo += 1; + return succ; } diff --git a/base/src/main/java/bjc/utils/ioutils/blocks/package-info.java b/base/src/main/java/bjc/utils/ioutils/blocks/package-info.java new file mode 100644 index 0000000..3d05ed4 --- /dev/null +++ b/base/src/main/java/bjc/utils/ioutils/blocks/package-info.java @@ -0,0 +1,22 @@ +/** This package contains a number of classes useful for processing input that + * is structured as a series of 'blocks' or records. + * <p> + * + * The most fundamental unit here is that of {@link Block}. Each {@link + * BlockReader} will yield a sequence of these, which contain a piece of text + * as its contents, as well as the beginning/ending line for that block. + * + * There are a number of different types of {@link BlockReader}, which are + * summarized here. + * </p> + * + * <dl> + * <dt>{@link SimpleBlockReader}</dt> + * <dd> + * The most basic form of BlockReader. This uses a regular expression to + * delimit input reader from a {@link Reader} into a series of blocks. + * Listed first, because this is + * </dd> + * </dl> + * @author Ben Culkin */ +package bjc.utils.ioutils.blocks; diff --git a/base/src/main/java/bjc/utils/parserutils/delims/SequenceDelimiter.java b/base/src/main/java/bjc/utils/parserutils/delims/SequenceDelimiter.java index b3f6dc4..b56af03 100644 --- a/base/src/main/java/bjc/utils/parserutils/delims/SequenceDelimiter.java +++ b/base/src/main/java/bjc/utils/parserutils/delims/SequenceDelimiter.java @@ -45,14 +45,14 @@ public class SequenceDelimiter<T> { * grammar while obeying the defined grouping rules. * * <pre> - * <tree> → (<data> | <subgroup> | <group>)* - * <subgroup> → <tree> <marker> - * <group> → <open> <tree> <close> + * <tree> → (<data> | <subgroup> | <group>)* + * <subgroup> → <tree> <marker> + * <group> → <open> <tree> <close> * - * <data> → STRING - * <open> → STRING - * <close> → STRING - * <marker> → STRING + * <data> → STRING + * <open> → STRING + * <close> → STRING + * <marker> → STRING * </pre> * * @param chars diff --git a/base/src/main/java/bjc/utils/patterns/ComplexPattern.java b/base/src/main/java/bjc/utils/patterns/ComplexPattern.java index c6d72ec..2d9fc71 100644 --- a/base/src/main/java/bjc/utils/patterns/ComplexPattern.java +++ b/base/src/main/java/bjc/utils/patterns/ComplexPattern.java @@ -38,7 +38,7 @@ public interface ComplexPattern<ReturnType, PredType, InputType> { /* Pattern producing functions */ /** - * Create a pattern composed from a predicate & a function. + * Create a pattern composed from a predicate & a function. * * @param <RetType> The type returned by the pattern. * @param <PreType> The type used as intermediate state. @@ -220,4 +220,4 @@ public interface ComplexPattern<ReturnType, PredType, InputType> { } }, (ignored, input) -> action.apply(input)); } -}
\ No newline at end of file +} diff --git a/clformat/example-format-strings.sprop b/clformat/example-format-strings.sprop new file mode 100644 index 0000000..6de136a --- /dev/null +++ b/clformat/example-format-strings.sprop @@ -0,0 +1,6 @@ +# Dump hexadecimal output in some manner +dumpHexOutput ~:{~8,'0X: ~2{~8@{~# ~:;~2,'0X ~]~} ~}~v@{ ~}~2{~8@{~A~} ~}~%~} +# Print a list in proper English style (no Oxford comma) +englishList ~#[ none~; ~A~; ~A and ~A:;~@{~#*[ ~A,~; and ~A~; ~A~]~}~] +# An alternate variant which abbreviates long lists +abbrevList ~#[NONE~;~a~;~a and ~a~:;~a, ~a~]~#[~; and ~a~:;, ~a, etc~] diff --git a/clformat/src/example/java/bjc/utils/ioutils/format/examples/CLFormatCLI.java b/clformat/src/example/java/bjc/utils/ioutils/format/examples/CLFormatCLI.java new file mode 100644 index 0000000..71e6b92 --- /dev/null +++ b/clformat/src/example/java/bjc/utils/ioutils/format/examples/CLFormatCLI.java @@ -0,0 +1,40 @@ +package bjc.utils.ioutils.format.examples; + +import java.io.*; +import java.util.*; + +public class CLFormatCLI { + public static void main(String[] args) { + Scanner scn = new Scanner(System.in); + PrintStream output = System.out; + + runCLI(scn, output); + } + + private static void runCLI(Scanner input, PrintStream output) { + output.println("CLFormat CLI v0.1"); + output.println("Enter a command (m for help)"); + + String inp = input.nextLine().trim(); + + boolean verboseErrors = false; + + while (!inp.equals("q")) { + String[] args = inp.split("\\S+"); + + String command = args[0]; + switch(command) { + default: + if (verboseErrors) { + output.printf("! Error: %s is not a recognizable command\n", inp); + } else { + output.println("! UC"); + } + } + + output.println("Enter a command (m for help)"); + + String inp = input.nextLine().trim(); + } + } +} |
