diff options
Diffstat (limited to 'base/src/main/java/bjc/utils')
6 files changed, 387 insertions, 213 deletions
diff --git a/base/src/main/java/bjc/utils/cli/objects/BlockReaderCLI.java b/base/src/main/java/bjc/utils/cli/objects/BlockReaderCLI.java index 64b56fc..bf8fc11 100644 --- a/base/src/main/java/bjc/utils/cli/objects/BlockReaderCLI.java +++ b/base/src/main/java/bjc/utils/cli/objects/BlockReaderCLI.java @@ -23,6 +23,47 @@ import static bjc.utils.cli.objects.Command.CommandStatus.*; * @author Ben Culkin */ public class BlockReaderCLI { + /** + * The state of the block reader. + * + * @author Ben Culkin + */ + public static final class BlockReaderState { + /** + * All of the configured block readers. + */ + public final Map<String, BlockReader> readers; + /** + * All of the configured I/O sources. + */ + public final Map<String, Reader> sources; + + /** + * Create a new set of state for the block reader. + * + * @param readers + * The set of configured block readers. + * + * @param sources + * The set of configured I/O sources. + */ + public BlockReaderState(Map<String, BlockReader> readers, Map<String, Reader> sources) { + this.readers = readers; + this.sources = sources; + } + + /** + * Create a new set of state for the block reader. + * + * @param sources + * The set of configured I/O sources. + */ + public BlockReaderState(Map<String, Reader> sources) { + this.readers = new HashMap<>(); + this.sources = sources; + } + } + /* Logger. */ private final Logger LOGGER = Logger.getLogger(BlockReaderCLI.class.getName()); @@ -33,17 +74,17 @@ public class BlockReaderCLI { * Create a new CLI for configuring BlockReaders. * * @param srcs - * The container of initial I/O sources. + * The container of initial I/O sources. */ public BlockReaderCLI(Map<String, Reader> srcs) { - stat = new BlockReaderState(new HashMap<>(), srcs); + stat = new BlockReaderState(srcs); } /** * Create a new CLI for configuring BlockReaders. * * @param state - * The state object to use. + * The state object to use. */ public BlockReaderCLI(BlockReaderState state) { stat = state; @@ -54,7 +95,7 @@ public class BlockReaderCLI { * Run the command line interface * * @param args - * Ignored CLI args. + * Ignored CLI args. */ public static void main(String[] args) { /* Create/configure I/O sources. */ @@ -72,60 +113,53 @@ public class BlockReaderCLI { * Run the CLI on an input source. * * @param input - * The place to read input from. + * The place to read input from. * - * @param ioSource - * The name of the place to read input from. + * @param srcName + * The name of the place to read input from. * * @param interactive - * Whether or not the source is interactive + * Whether or not the source is interactive */ - public void run(Scanner input, String ioSource, boolean interactive) { + public void run(Scanner input, String srcName, boolean interactive) { int lno = 0; - /* Print a prompt. */ - if(interactive) { - System.out.printf("reader-conf(%d)>", lno); - } + do { + /* Print a prompt. */ + if (interactive) { + System.out.printf("reader-conf(%d)>", lno); + } - while(input.hasNextLine()) { /* Read a line. */ String ln = input.nextLine(); lno += 1; /* Parse the command. */ - Command com = Command.fromString(ln, lno, ioSource); + Command com = Command.fromString(ln, lno, srcName); /* Ignore blank commands. */ - if(com == null) continue; + if (com == null) continue; /* Handle a command. */ - CommandStatus stt = handleCommand(com, interactive); + CommandStatus sts = handleCommand(com, interactive); /* Exit if we finished or encountered a fatal error. */ - if(stt == FINISH || stt == ERROR) { - return; - } - - /* Print a prompt. */ - if(interactive) { - System.out.printf("reader-conf(%d)>", lno); - } - - } + if (sts == FINISH || sts == ERROR) { return; } + } while (input.hasNextLine()); } /** * Handle a command. * * @param com - * The command to handle + * The command to handle * * @param interactive - * Whether the current input source is interactive or not. + * Whether the current input source is interactive or + * not. * @return The status of the executed command. */ public CommandStatus handleCommand(Command com, boolean interactive) { /* Handle each command. */ - switch(com.nameCommand) { + switch (com.name) { case "def-filtered": return defFiltered(com); case "def-layered": @@ -142,57 +176,48 @@ public class BlockReaderCLI { case "end": case "exit": case "quit": - if(interactive) System.out.printf("Exiting reader-conf, %d readers configured in %d commands\n", - stat.readers.size(), com.lineNo); + if (interactive) + System.out.printf("Exiting reader-conf, %d readers configured in %d commands\n", + stat.readers.size(), com.lno); return FINISH; default: - LOGGER.severe(com.error("Unknown command '%s'\n", com.nameCommand)); + LOGGER.severe(com.error("Unknown command '%s'\n", com.name)); return FAIL; } } private CommandStatus defFiltered(Command com) { - String remn = com.remnCommand; - /* * Get the block name. */ - /* - * @TODO 02/17/18 Ben Culkin :StringHandling - * This slicing up strings by indexed characters should be abstracted into some - * sort of class. - */ - int idx = remn.indexOf(' '); - if(idx == -1) { + + String blockName = com.trimTo(' '); + if (blockName == null) { LOGGER.severe(com.error("No name argument for def-filtered.\n")); return FAIL; } - String blockName = remn.substring(0, idx).trim(); - remn = remn.substring(idx).trim(); /* * Check there isn't a reader already bound to this name. */ - if(stat.readers.containsKey(blockName)) { + if (stat.readers.containsKey(blockName)) { LOGGER.warning(com.warn("Shadowing existing reader named %s\n", blockName)); } /* * Get the reader name. */ - /* :StringHandling */ - idx = remn.indexOf(' '); - if(idx == -1) { + + String readerName = com.trimTo(' '); + if (readerName == null) { LOGGER.severe(com.error("No reader-name argument for def-filtered.\n")); return FAIL; } - String readerName = remn.substring(0, idx).trim(); - remn = remn.substring(idx).trim(); /* * Check there is a reader bound to that name. */ - if(!stat.readers.containsKey(readerName)) { + if (!stat.readers.containsKey(readerName)) { LOGGER.severe(com.error("No source named %s\n", readerName)); return FAIL; } @@ -200,12 +225,12 @@ public class BlockReaderCLI { /* * Get the pattern. */ - if(remn.equals("")) { + if (com.remn.equals("")) { LOGGER.severe(com.error("No filter argument for def-filtered\n")); return FAIL; } - String filter = remn; + String filter = com.remn; try { Pattern pat = Pattern.compile(filter); @@ -219,7 +244,7 @@ public class BlockReaderCLI { BlockReader reader = new FilteredBlockReader(stat.readers.get(readerName), pred); stat.readers.put(blockName, reader); - } catch(PatternSyntaxException psex) { + } catch (PatternSyntaxException psex) { LOGGER.severe(com.error("Invalid regular expression '%s' for filter. (%s)\n", filter, psex.getMessage())); return FAIL; @@ -229,22 +254,22 @@ public class BlockReaderCLI { } private CommandStatus defPushback(Command com) { - String[] parts = com.remnCommand.split(" "); + String[] parts = com.remn.split(" "); - if(parts.length != 2) { + if (parts.length != 2) { LOGGER.severe(com.error( "Incorrect number of arguments to def-pushback. Requires a block name and a reader name\n")); return FAIL; } String blockName = parts[0]; - if(stat.readers.containsKey(blockName)) { + if (stat.readers.containsKey(blockName)) { LOGGER.warning(com.warn("Shadowing existing reader %s\n", blockName)); return FAIL; } String readerName = parts[1]; - if(!stat.readers.containsKey(readerName)) { + if (!stat.readers.containsKey(readerName)) { LOGGER.severe(com.error("No reader named %s\n", readerName)); return FAIL; } @@ -256,9 +281,9 @@ public class BlockReaderCLI { } private CommandStatus defToggled(Command com) { - String[] parts = com.remnCommand.split(" "); + String[] parts = com.remn.split(" "); - if(parts.length != 3) { + if (parts.length != 3) { LOGGER.severe(com.error( "Incorrect number of arguments to def-toggled. Requires a block name and two reader names\n")); return FAIL; @@ -268,19 +293,19 @@ public class BlockReaderCLI { * Get the block name. */ String blockName = parts[0]; - if(stat.readers.containsKey(blockName)) { + if (stat.readers.containsKey(blockName)) { LOGGER.warning(com.warn("Shadowing existing reader named %s\n", blockName)); } /* * Make sure the component readers exist. */ - if(!stat.readers.containsKey(parts[1])) { + if (!stat.readers.containsKey(parts[1])) { LOGGER.severe(com.error("No reader named %s\n", parts[1])); return FAIL; } - if(!stat.readers.containsKey(parts[2])) { + if (!stat.readers.containsKey(parts[2])) { LOGGER.severe(com.error("No reader named %s\n", parts[2])); return FAIL; } @@ -292,9 +317,9 @@ public class BlockReaderCLI { } private CommandStatus defLayered(Command com) { - String[] parts = com.remnCommand.split(" "); + String[] parts = com.remn.split(" "); - if(parts.length != 3) { + if (parts.length != 3) { LOGGER.severe(com.error( "Incorrect number of arguments to def-layered. Requires a block name and two reader names\n")); return FAIL; @@ -304,19 +329,19 @@ public class BlockReaderCLI { * Get the block name. */ String blockName = parts[0]; - if(stat.readers.containsKey(blockName)) { + if (stat.readers.containsKey(blockName)) { LOGGER.warning(com.warn("Shadowing existing reader named %s\n", blockName)); } /* * Make sure the component readers exist. */ - if(!stat.readers.containsKey(parts[1])) { + if (!stat.readers.containsKey(parts[1])) { LOGGER.severe(com.error("No reader named %s\n", parts[1])); return FAIL; } - if(!stat.readers.containsKey(parts[2])) { + if (!stat.readers.containsKey(parts[2])) { LOGGER.severe(com.error("No reader named %s\n", parts[2])); return FAIL; } @@ -328,9 +353,9 @@ public class BlockReaderCLI { } private CommandStatus defSerial(Command com) { - String[] parts = com.remnCommand.split(" "); + String[] parts = com.remn.split(" "); - if(parts.length < 2) { + if (parts.length < 2) { LOGGER.severe(com.error( "Not enough arguments to def-serial. Requires at least a block name and at least one reader name\n")); return FAIL; @@ -343,7 +368,7 @@ public class BlockReaderCLI { /* * Check there isn't a reader already bound to this name. */ - if(stat.readers.containsKey(blockName)) { + if (stat.readers.containsKey(blockName)) { LOGGER.warning(com.warn("Shadowing existing reader named %s\n", blockName)); } @@ -351,13 +376,13 @@ public class BlockReaderCLI { * Get all of the component readers. */ BlockReader[] readerArr = new BlockReader[parts.length - 1]; - for(int i = 1; i < parts.length; i++) { + for (int i = 1; i < parts.length; i++) { String readerName = parts[i]; /* * Check there is a reader bound to that name. */ - if(!stat.readers.containsKey(readerName)) { + if (!stat.readers.containsKey(readerName)) { LOGGER.severe(com.error("No reader named %s\n", readerName)); return FAIL; } @@ -373,14 +398,14 @@ public class BlockReaderCLI { } private CommandStatus defSimple(Command com) { - String remn = com.remnCommand; + String remn = com.remn; /* * Get the block name. */ /* :StringHandling */ int idx = remn.indexOf(' '); - if(idx == -1) { + if (idx == -1) { LOGGER.severe(com.error("No name argument for def-simple.\n")); return FAIL; } @@ -390,7 +415,7 @@ public class BlockReaderCLI { /* * Check there isn't a reader already bound to this name. */ - if(stat.readers.containsKey(blockName)) { + if (stat.readers.containsKey(blockName)) { LOGGER.warning(com.warn("Shadowing existing reader named %s\n", blockName)); } @@ -399,7 +424,7 @@ public class BlockReaderCLI { */ /* :StringHandling */ idx = remn.indexOf(' '); - if(idx == -1) { + if (idx == -1) { LOGGER.severe(com.error("No source-name argument for def-simple.\n")); return FAIL; } @@ -409,7 +434,7 @@ public class BlockReaderCLI { /* * Check there is a source bound to that name. */ - if(!stat.sources.containsKey(sourceName)) { + if (!stat.sources.containsKey(sourceName)) { LOGGER.severe(com.error("No source named %s\n", sourceName)); return FAIL; } @@ -417,7 +442,7 @@ public class BlockReaderCLI { /* * Get the pattern. */ - if(remn.equals("")) { + if (remn.equals("")) { LOGGER.severe(com.error("No delimiter argument for def-simple\n")); return FAIL; } @@ -429,7 +454,7 @@ public class BlockReaderCLI { BlockReader reader = new SimpleBlockReader(delim, stat.sources.get(sourceName)); stat.readers.put(blockName, reader); - } catch(PatternSyntaxException psex) { + } catch (PatternSyntaxException psex) { LOGGER.severe(com.error("Invalid regular expression '%s' for delimiter. (%s)\n", delim, psex.getMessage())); return FAIL; diff --git a/base/src/main/java/bjc/utils/cli/objects/BlockReaderState.java b/base/src/main/java/bjc/utils/cli/objects/BlockReaderState.java deleted file mode 100644 index c528e6a..0000000 --- a/base/src/main/java/bjc/utils/cli/objects/BlockReaderState.java +++ /dev/null @@ -1,36 +0,0 @@ -package bjc.utils.cli.objects; - -import bjc.utils.ioutils.blocks.BlockReader; - -import java.io.Reader; -import java.util.Map; - -/** - * The state of the block reader. - * - * @author Ben Culkin - */ -public class BlockReaderState { - /** - * All of the configured block readers. - */ - public final Map<String, BlockReader> readers; - /** - * All of the configured I/O sources. - */ - public final Map<String, Reader> sources; - - /** - * Create a new set of state for the block reader. - * - * @param readers - * The set of configured block readers. - * - * @param sources - * The set of configured I/O sources. - */ - public BlockReaderState(Map<String, BlockReader> readers, Map<String, Reader> sources) { - this.readers = readers; - this.sources = sources; - } -}
\ No newline at end of file diff --git a/base/src/main/java/bjc/utils/cli/objects/Command.java b/base/src/main/java/bjc/utils/cli/objects/Command.java index a48eef7..e31839c 100644 --- a/base/src/main/java/bjc/utils/cli/objects/Command.java +++ b/base/src/main/java/bjc/utils/cli/objects/Command.java @@ -1,5 +1,8 @@ package bjc.utils.cli.objects; +import java.util.regex.Matcher; +import java.util.regex.Pattern; + /** * A single-line command read from the user. * @@ -13,7 +16,7 @@ public class Command { */ public static enum CommandStatus { /** - * The command succeded. + * The command succeeded. */ SUCCESS, /** @@ -33,52 +36,236 @@ public class Command { /** * The line number of this command. */ - public final int lineNo; + public final int lno; /** * The full text of this command. */ - public final String fullCommand; + public final String full; /** * The text of this command without its name. */ - public final String remnCommand; + public String remn; /** * The name of this command. */ - public final String nameCommand; + public final String name; /** * The name of the I/O source this command was read from. */ - public final String ioSource; + public final String src; /** * Create a new command. * * @param ln - * The string to get the command from. + * The string to get the command from. * * @param lno - * The number of the line the command came from. + * The number of the line the command came from. * * @param ioSrc - * The name of where the I/O came from. + * The name of where the I/O came from. */ public Command(String ln, int lno, String ioSrc) { - /* :StringHandling */ int idx = ln.indexOf(' '); - if(idx == -1) idx = ln.length(); + if (idx == -1) idx = ln.length(); /* Grab command parts. */ - fullCommand = ln; - nameCommand = ln.substring(0, idx).trim(); - remnCommand = ln.substring(idx).trim(); + full = ln; + name = ln.substring(0, idx).trim(); + remn = ln.substring(idx).trim(); + + this.lno = lno; + + src = ioSrc; + } + + /** + * Removes up until the first occurrence of a particular string for the + * remaining command, and returns the removed string. + * + * By default, both the substring and the remaining text are trimmed + * (leading/trailing spaces removed). + * + * @param delm + * The delimiter to stop substringing at. + * + * @return The substring, or null if there is no occurrence of the + * delimiter. + */ + public String trimTo(String delm) { + return trimTo(delm, true); + } + + /** + * Removes up until the first occurrence of a particular string for the + * remaining command, and returns the removed string. + * + * @param delm + * The delimiter to stop substringing at. + * @param doTrim + * Whether or not to trim the substring and remaining + * command (Remove leading/trailing spaces). + * + * @return The substring, or null if there is no occurrence of the + * delimiter. + */ + public String trimTo(String delm, boolean doTrim) { + int idx = remn.indexOf(delm); + if (idx == -1) return null; + + String tmp = remn.substring(0, idx); + remn = remn.substring(idx); + + if (doTrim) { + tmp = tmp.trim(); + remn = remn.trim(); + } + + return tmp; + } + + /** + * Removes up until the first occurrence of a particular regex for the + * remaining command, and returns the removed string. + * + * By default, both the substring and the remaining text are trimmed + * (leading/trailing spaces removed). + * + * @param rDelm + * The regex to stop substringing at. + * + * @return The first capturing group (the entire match otherwise), or + * null if there is no occurrence of the delimiter. + */ + public String trimToRX(String rDelm) { + return trimToRX(Pattern.compile(rDelm), true); + } + + /** + * Removes up until the first occurrence of a particular regex for the + * remaining command, and returns the removed string. + * + * By default, both the substring and the remaining text are trimmed + * (leading/trailing spaces removed). + * + * @param delm + * The regex to stop substringing at. + * + * @return The first capturing group (the entire match otherwise), or + * null if there is no occurrence of the delimiter. + */ + public String trimToRX(Pattern delm) { + return trimToRX(delm, true); + } + + /** + * Removes up until the first occurrence of a particular regex for the + * remaining command, and returns the removed string. + * + * @param rDelm + * The regex to stop substringing at. + * @param doTrim + * Whether or not to trim the substring and remaining + * command (Remove leading/trailing spaces). + * + * @return The first capturing group (the entire match otherwise), or + * null if there is no occurrence of the delimiter. + */ + public String trimToRX(String rDelm, boolean doTrim) { + return trimToRX(Pattern.compile(rDelm), doTrim); + } + + /** + * Removes up until the first occurrence of a particular regex for the + * remaining command, and returns the removed string. + * + * @param delm + * The regex to stop substringing at. + * @param doTrim + * Whether or not to trim the substring and remaining + * command (Remove leading/trailing spaces). + * + * @return The first capturing group (the entire match otherwise), or + * null if there is no occurrence of the delimiter. + */ + public String trimToRX(Pattern delm, boolean doTrim) { + Matcher mat = delm.matcher(remn); + if (!mat.find()) return null; + + String tmp; + + if (mat.groupCount() > 0) { + tmp = mat.group(1); + remn = remn.substring(mat.end()); + } else { + tmp = mat.group(); + remn = remn.substring(mat.end()); + } + + if (doTrim) { + tmp = tmp.trim(); + remn = remn.trim(); + } + + return tmp; + } + + /** + * Removes up until the first occurrence of a particular string for the + * remaining command, and returns the removed string. + * + * By default, both the substring and the remaining text are trimmed + * (leading/trailing spaces removed). + * + * @param delm + * The delimiter to stop substringing at. + * + * @return The substring, or null if there is no occurrence of the + * delimiter. + */ + public String trimTo(char delm) { + return trimTo(delm, true); + } + + /** + * Removes up until the first occurrence of a particular string for the + * remaining command, and returns the removed string. + * + * @param delm + * The delimiter to stop substringing at. + * @param doTrim + * Whether or not to trim the substring and remaining + * command (Remove leading/trailing spaces). + * + * @return The substring, or null if there is no occurrence of the + * delimiter. + */ + public String trimTo(char delm, boolean doTrim) { + int idx = remn.indexOf(delm); + if (idx == -1) return null; - lineNo = lno; + String tmp = remn.substring(0, idx); + remn = remn.substring(idx); - ioSource = ioSrc; + if (doTrim) { + tmp = tmp.trim(); + remn = remn.trim(); + } + + return tmp; + } + + /** + * Check if this command has text after its name. + * + * @return Whether or not this command has text after its name. + */ + public boolean hasRemaining() { + return !remn.equals(""); } /** @@ -89,27 +276,27 @@ public class Command { * through the line. * * @param ln - * The string to get the command from. + * The string to get the command from. * * @param lno - * The line number of the command. + * The line number of the command. * - * @param ioSource - * The name of where the I/O came from. + * @param srcName + * The name of where the I/O came from. * @return The parsed command */ - public static Command fromString(String ln, int lno, String ioSource) { + public static Command fromString(String ln, int lno, String srcName) { /* Ignore blank lines and comments. */ - if(ln.equals("")) return null; - if(ln.startsWith("#")) return null; + if (ln.equals("")) return null; + if (ln.startsWith("#")) return null; /* Trim off comments part-way through the line. */ int idxHash = ln.indexOf('#'); - if(idxHash != -1) { + if (idxHash != -1) { ln = ln.substring(0, idxHash).trim(); } - return new Command(ln, lno, ioSource); + return new Command(ln, lno, srcName); } /** @@ -117,49 +304,49 @@ public class Command { * command. * * @param info - * The informational message. + * The informational message. * * @param parms - * The parameters for the informational message. + * The parameters for the informational message. * @return The information message. */ public String info(String info, Object... parms) { String msg = String.format(info, parms); - return String.format("INFO (%s:%d): %s", ioSource, lineNo, msg); + return String.format("INFO (%s:%d): %s", src, lno, msg); } /** * Warn about something in relation to this command. * * @param warning - * The warning message. + * The warning message. * * @param parms - * The parameters for the warning message. + * The parameters for the warning message. * * @return The formatted warning. */ public String warn(String warning, Object... parms) { String msg = String.format(warning, parms); - return String.format("WARNING (%s:%d): %s", ioSource, lineNo, msg); + return String.format("WARNING (%s:%d): %s", src, lno, msg); } /** * Give an error about something in relation to this command. * * @param err - * The error message. + * The error message. * * @param parms - * The parameters for the error message. + * The parameters for the error message. * * @return The formatted error */ public String error(String err, Object... parms) { String msg = String.format(err, parms); - return String.format("ERROR (%s:%d): %s", ioSource, lineNo, msg); + return String.format("ERROR (%s:%d): %s", src, lno, msg); } } diff --git a/base/src/main/java/bjc/utils/cli/objects/DefineCLI.java b/base/src/main/java/bjc/utils/cli/objects/DefineCLI.java index 108f90b..c2a5a1a 100644 --- a/base/src/main/java/bjc/utils/cli/objects/DefineCLI.java +++ b/base/src/main/java/bjc/utils/cli/objects/DefineCLI.java @@ -112,7 +112,7 @@ public class DefineCLI { * @return The status of the executed command. */ public CommandStatus handleCommand(Command com, boolean interactive) { - switch(com.nameCommand) { + switch(com.name) { case "def-string": return defString(com); case "def-format": @@ -120,13 +120,13 @@ public class DefineCLI { case "bind-format": return bindFormat(com); default: - LOGGER.severe(com.error("Unknown command %s\n", com.nameCommand)); + LOGGER.severe(com.error("Unknown command %s\n", com.name)); return FAIL; } } private CommandStatus defString(Command com) { - List<String> arguments = StringUtils.processArguments(com.remnCommand); + List<String> arguments = StringUtils.processArguments(com.remn); if(arguments.size() < 1) { LOGGER.severe(com.error( @@ -154,7 +154,7 @@ public class DefineCLI { } private CommandStatus defFormat(Command com) { - List<String> arguments = StringUtils.processArguments(com.remnCommand); + List<String> arguments = StringUtils.processArguments(com.remn); if(arguments.size() < 1) { LOGGER.severe(com.error( @@ -182,7 +182,7 @@ public class DefineCLI { } private CommandStatus bindFormat(Command com) { - List<String> strings = StringUtils.processArguments(com.remnCommand); + List<String> strings = StringUtils.processArguments(com.remn); if(strings.size() < 2) { LOGGER.severe(com.error( diff --git a/base/src/main/java/bjc/utils/funcutils/StringUtils.java b/base/src/main/java/bjc/utils/funcutils/StringUtils.java index 7a4ee20..5d191bc 100644 --- a/base/src/main/java/bjc/utils/funcutils/StringUtils.java +++ b/base/src/main/java/bjc/utils/funcutils/StringUtils.java @@ -22,18 +22,19 @@ 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. + * 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(final String input, final String rRegex) { - if(input == null) + 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. @@ -50,13 +51,13 @@ 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(final StringBuilder builder, final int levels) { - for(int i = 0; i < levels; i++) { + for (int i = 0; i < levels; i++) { builder.append("\t"); } } @@ -66,10 +67,10 @@ public class StringUtils { * empty. * * @param <ContainedType> - * The type in the deque. + * The type in the deque. * * @param queue - * The deque to print. + * The deque to print. * * @return A string version of the deque, with allowance for an empty * deque. @@ -82,26 +83,26 @@ 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. * * @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(final Object[] objects, final String join, final String comma) { - if(objects == null) throw new NullPointerException("Sequence must not be null"); + if (objects == null) throw new NullPointerException("Sequence must not be null"); final StringBuilder sb = new StringBuilder(); final String joiner = join; final String coma = comma; - switch(objects.length) { + switch (objects.length) { case 0: /* Empty list. */ break; @@ -117,7 +118,7 @@ public class StringUtils { break; default: /* Three or more items. */ - for(int i = 0; i < objects.length - 1; i++) { + for (int i = 0; i < objects.length - 1; i++) { sb.append(objects[i].toString()); sb.append(coma + " "); } @@ -140,11 +141,11 @@ 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. */ @@ -156,17 +157,15 @@ 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 and - * Whether to use 'and' or 'or'. + * Whether to use 'and' or 'or'. * * @return The sequence as an English list. */ public static String toEnglishList(final Object[] objects, final boolean and) { - if(and) { - return toEnglishList(objects, "and"); - } + if (and) { return toEnglishList(objects, "and"); } return toEnglishList(objects, "or"); } @@ -175,7 +174,7 @@ public class StringUtils { * 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. */ @@ -184,7 +183,7 @@ public class StringUtils { it.setText(value); int count = 0; - while(it.next() != BreakIterator.DONE) { + while (it.next() != BreakIterator.DONE) { count++; } @@ -195,17 +194,17 @@ public class StringUtils { * Count the number of times a pattern matches in a given string. * * @param value - * The string to count occurances in. + * The string to count occurances in. * * @param pattern - * The pattern to count occurances of. + * The pattern to count occurances of. * @return The number of times the pattern matches. */ public static int countMatches(final String value, final String pattern) { Matcher mat = Pattern.compile(pattern).matcher(value); int num = 0; - while(mat.find()) + while (mat.find()) num += 1; return num; @@ -215,16 +214,36 @@ public class StringUtils { * Get a substring until a specified string. * * @param strang - * The string to substring. + * The string to substring. * @param vx - * The place to substring until. + * The place to substring until. + * * @return The specified substring. */ public static String substringTo(String strang, String vx) { + return substringTo(strang, vx, true); + } + + /** + * Get a substring until a specified string. + * + * @param strang + * The string to substring. + * @param vx + * The place to substring until. + * @param allowFail + * Whether or not to allow failure. + * + * @return The specified substring, or null if the specified place to + * substring to was not found, and we were not allowed to fail. + */ + public static String substringTo(String strang, String vx, boolean allowFail) { int idx = strang.indexOf(vx); - if(idx == -1) { - return strang; + if (idx == -1) { + if (allowFail) return strang; + + return null; } return strang.substring(0, strang.indexOf(vx)); @@ -235,7 +254,7 @@ public class StringUtils { * string literals. * * @param com - * The command to split from + * The command to split from * @return The split arguments. */ public static List<String> processArguments(String com) { @@ -243,11 +262,11 @@ public class StringUtils { BooleanToggle togg = new BooleanToggle(); - for(String strang : TokenUtils.removeDQuotedStrings(com)) { - if(togg.get()) { + for (String strang : TokenUtils.removeDQuotedStrings(com)) { + if (togg.get()) { strings.add(TokenUtils.descapeString(strang)); } else { - for(String strung : strang.split("\\s+")) { + for (String strung : strang.split("\\s+")) { strings.add(strung); } } diff --git a/base/src/main/java/bjc/utils/parserutils/ShuntingYard.java b/base/src/main/java/bjc/utils/parserutils/ShuntingYard.java index a3fa2b9..c3733b9 100644 --- a/base/src/main/java/bjc/utils/parserutils/ShuntingYard.java +++ b/base/src/main/java/bjc/utils/parserutils/ShuntingYard.java @@ -58,27 +58,6 @@ public class ShuntingYard<TokenType> { } /* - * Function that shunts tokens. - */ - private final class TokenShunter implements Consumer<String> { - private final IList<TokenType> output; - private final Deque<String> stack; - private final Function<String, TokenType> transformer; - - public TokenShunter(final IList<TokenType> outpt, final Deque<String> stack, - final Function<String, TokenType> transformer) { - this.output = outpt; - this.stack = stack; - this.transformer = transformer; - } - - @Override - public void accept(final String token) { - - } - } - - /* * Holds all the shuntable operations. */ private IMap<String, IPrecedent> operators; |
