diff options
Diffstat (limited to 'base/src/main/java/bjc/utils/ioutils')
8 files changed, 452 insertions, 96 deletions
diff --git a/base/src/main/java/bjc/utils/ioutils/LevelSplitter.java b/base/src/main/java/bjc/utils/ioutils/LevelSplitter.java index c2467ae..93bc424 100644 --- a/base/src/main/java/bjc/utils/ioutils/LevelSplitter.java +++ b/base/src/main/java/bjc/utils/ioutils/LevelSplitter.java @@ -5,29 +5,24 @@ import java.util.List; import java.util.regex.Matcher; import java.util.regex.Pattern; -/** - * Splits a string on a delimiter, respecting grouping delimiters. +/** Splits a string on a delimiter, respecting grouping delimiters. * * By default, grouping delimiters are (), [], {}, and <>, as well as single and * double quoted strings. * - * @author bjculkin - * - */ + * @author bjculkin */ public class LevelSplitter { - /** - * Defaultly configured level splitter. - */ + /** Default configured level splitter. */ public final static LevelSplitter def = new LevelSplitter(); - /** - * Check if a string contains any one of a specified number of things, + /** Should empty strings be ignored? */ + public boolean ignoreEmpty = false; + + /** Check if a string contains any one of a specified number of things, * respecting groups. * - * @param haystack - * The string to look in. - * @param needles - * The strings to look for. + * @param haystack The string to look in. + * @param needles The strings to look for. * @return Whether or not any of the strings were contained outside of groups. */ public boolean levelContains(String haystack, String... needles) { @@ -88,13 +83,11 @@ public class LevelSplitter { return false; } - /** - * Split a string, respecting groups. + /** Split a string, respecting groups. * - * @param phrase - * The string to split. - * @param splits - * The strings to split on. + * @param phrase The string to split. + * @param splits The strings to split on. + * * @return A list of split strings. If keepDelims is true, it also includes the * delimiters in between the split strings. */ @@ -102,15 +95,12 @@ public class LevelSplitter { return levelSplit(phrase, false, splits); } - /** - * Split a string, respecting groups. + /** Split a string, respecting groups. * - * @param phrase - * The string to split. - * @param keepDelims - * Whether or not to include the delimiters in the results. - * @param splits - * The strings to split on. + * @param phrase The string to split. + * @param keepDelims Whether or not to include the delimiters in the results. + * @param splits The strings to split on. + * * @return A list of split strings. If keepDelims is true, it also includes the * delimiters in between the split strings. */ @@ -140,8 +130,7 @@ public class LevelSplitter { if (work.regionMatches(i, split, 0, split.length())) { strangs.add(work.substring(0, i)); - if (keepDelims) - strangs.add(split); + if (keepDelims) strangs.add(split); work = work.substring(i + split.length()); i = 0; diff --git a/base/src/main/java/bjc/utils/ioutils/LineReader.java b/base/src/main/java/bjc/utils/ioutils/LineReader.java deleted file mode 100644 index 2ac2797..0000000 --- a/base/src/main/java/bjc/utils/ioutils/LineReader.java +++ /dev/null @@ -1,18 +0,0 @@ -package bjc.utils.ioutils; - -/** - * A line reader - * - * @author bjculkin - * - */ -public class LineReader implements AutoCloseable { - //private Scanner scn; - - @Override - public void close() { - //scn.close(); - } - - // @TODO Implement me - ben, 1/6/20 -} diff --git a/base/src/main/java/bjc/utils/ioutils/LogStream.java b/base/src/main/java/bjc/utils/ioutils/LogStream.java new file mode 100644 index 0000000..f5271dc --- /dev/null +++ b/base/src/main/java/bjc/utils/ioutils/LogStream.java @@ -0,0 +1,341 @@ +package bjc.utils.ioutils; + +import java.io.*; + +/** + * Simple class used for logging with various levels. + * + * @author Ben Culkin + */ +public class LogStream { + /** + * Log level for printing nothing. + */ + public static final int NOTHING = -1; + + /** + * Log level for printing only fatal errors. + */ + public static final int FATAL = 0; + + /** + * Log level for printing all errors. + */ + public static final int ERROR = 1; + + /** + * Log level for printing warnings. + */ + public static final int WARN = 2; + + /** + * Log level for printing info messages. + */ + public static final int INFO = 3; + + /** + * Log level for printing debug messages. + */ + public static final int DEBUG = 4; + + /** + * Log level for printing trace messages. + */ + public static final int TRACE = 5; + + private int verbosity; + + private PrintStream output; + + /** + * Create a new log stream. + * + * Defaults to printing only fatal errors. + * + * @param out + * The output stream to place things into. + */ + public LogStream(PrintStream out) { + output = out; + verbosity = FATAL; + } + + /** + * Create a new log stream. + * + * @param out + * The output stream to place things into. + * @param level + * The verbosity level. Use the constants in this class for the + * values. + */ + public LogStream(PrintStream out, int level) { + output = out; + verbosity = level; + } + + /** + * Create a new log stream. + * + * Defaults to printing only fatal errors. + * + * @param out + * The output stream to place things into. + */ + public LogStream(OutputStream out) { + output = new PrintStream(out); + verbosity = FATAL; + } + + /** + * Create a new log stream. + * + * @param out + * The output stream to place things into. + * @param level + * The verbosity level. Use the constants in this class for the + * values. + */ + public LogStream(OutputStream out, int level) { + output = new PrintStream(out); + verbosity = level; + } + + /** + * Get the verbosity of the stream. + * + * @return The verbosity of the stream. + */ + public int verbosity() { + return verbosity; + } + + /** + * Set the verbosity of the stream. + * + * @param verb + * The verbosity of the stream. + */ + public void verbosity(int verb) { + verbosity = verb; + } + + /** + * Increment the verbosity of the stream. + */ + public void louder() { + louder(1); + } + + /** + * Increase the verbosity of the stream by an amount. + * + * @param amt + * The amount to increase the verbosity by. + */ + public void louder(int amt) { + verbosity += amt; + } + + /** + * Decrement the verbosity of the stream. + */ + public void quieter() { + quieter(1); + } + + /** + * Decrease the verbosity of the stream by an amount. + * + * @param amt + * The amount to decrease the verbosity by. + */ + public void quieter(int amt) { + verbosity -= amt; + } + + /** + * Print a message that will always be visible. + * + * @param msg + * The message to print. + */ + public void print(String msg) { + output.print(msg); + } + + /** + * Print a formatted message that will always be visible. + * + * @param msg + * The format string for the message to print. + * + * @param args + * The arguments to the format string. + */ + public void printf(String msg, Object... args) { + output.printf(msg, args); + } + + /** + * Print a message at a given verbosity level. + * + * @param lvl + * The verbosity level. + * @param msg + * The message to print. + */ + public void message(int lvl, String msg) { + if (verbosity >= lvl) { + output.print(msg); + } + } + + /** + * Print a formatted message at a given verbosity level. + * + * @param lvl + * The verbosity level. + * @param msg + * The message to print. + * @param args + * The arguments to the message. + */ + public void messagef(int lvl, String msg, Object... args) { + if (verbosity >= lvl) { + output.printf(msg, args); + } + } + + /** + * Emit a fatal error message. + * + * @param msg + * The message to emit. + */ + public void fatal(String msg) { + message(FATAL, msg); + } + + /** + * Emit a formatted fatal error message. + * + * @param msg + * The message to emit. + * @param args + * The arguments to the message. + */ + public void fatalf(String msg, Object... args) { + messagef(FATAL, msg, args); + } + + /** + * Emit a normal error message. + * + * @param msg + * The message to emit. + */ + public void error(String msg) { + message(ERROR, msg); + } + + /** + * Emit a formatted normal error message. + * + * @param msg + * The message to emit. + * @param args + * The arguments to the message. + */ + public void errorf(String msg, Object... args) { + messagef(ERROR, msg, args); + } + + /** + * Emit a warning message. + * + * @param msg + * The message to emit. + */ + public void warn(String msg) { + message(WARN, msg); + } + + /** + * Emit a formatted warning message. + * + * @param msg + * The message to emit. + * @param args + * The arguments to the message. + */ + public void warnf(String msg, Object... args) { + messagef(WARN, msg, args); + } + + /** + * Emit an info message. + * + * @param msg + * The message to emit. + */ + public void info(String msg) { + message(INFO, msg); + } + + /** + * Emit a formatted info message. + * + * @param msg + * The message to emit. + * @param args + * The arguments to the message. + */ + public void infof(String msg, Object... args) { + messagef(INFO, msg, args); + } + + /** + * Emit a debug message. + * + * @param msg + * The message to emit. + */ + public void debug(String msg) { + message(DEBUG, msg); + } + + /** + * Emit a formatted debug message. + * + * @param msg + * The message to emit. + * @param args + * The arguments to the message. + */ + public void debugf(String msg, Object... args) { + messagef(DEBUG, msg, args); + } + + /** + * Emit a tracing message. + * + * @param msg + * The message to emit. + */ + public void trace(String msg) { + message(TRACE, msg); + } + + /** + * Emit a formatted tracing message. + * + * @param msg + * The message to emit. + * @param args + * The arguments to the message. + */ + public void tracef(String msg, Object... args) { + messagef(TRACE, msg, args); + } +} diff --git a/base/src/main/java/bjc/utils/ioutils/RegexStringEditor.java b/base/src/main/java/bjc/utils/ioutils/RegexStringEditor.java index 3dad724..e7d8c54 100644 --- a/base/src/main/java/bjc/utils/ioutils/RegexStringEditor.java +++ b/base/src/main/java/bjc/utils/ioutils/RegexStringEditor.java @@ -8,7 +8,7 @@ import java.util.regex.Pattern; import bjc.data.Toggle; import bjc.data.ValueToggle; import bjc.funcdata.FunctionalList; -import bjc.funcdata.IList; +import bjc.funcdata.ListEx; import bjc.functypes.ID; /** @@ -84,7 +84,7 @@ public class RegexStringEditor { /* * Get all of the occurances. */ - final IList<String> occurances = listOccurances(input, rPatt); + final ListEx<String> occurances = listOccurances(input, rPatt); /* * Execute the correct action on every occurance. @@ -118,13 +118,13 @@ public class RegexStringEditor { * * @return The string, with both actions applied. */ - public static IList<String> mapOccurances(final String input, final Pattern rPatt, + public static ListEx<String> mapOccurances(final String input, final Pattern rPatt, final UnaryOperator<String> betweenAction, final UnaryOperator<String> onAction) { /* * Get all of the occurances. */ - final IList<String> occurances = listOccurances(input, rPatt); + final ListEx<String> occurances = listOccurances(input, rPatt); /* * Execute the correct action on every occurance. @@ -146,8 +146,8 @@ public class RegexStringEditor { * @return The string, as a list of match/non-match segments, starting/ending * with a non-match segment. */ - public static IList<String> listOccurances(final String input, final Pattern rPatt) { - final IList<String> res = new FunctionalList<>(); + public static ListEx<String> listOccurances(final String input, final Pattern rPatt) { + final ListEx<String> res = new FunctionalList<>(); /* * Create the matcher and work buffer. diff --git a/base/src/main/java/bjc/utils/ioutils/ReportWriter.java b/base/src/main/java/bjc/utils/ioutils/ReportWriter.java index 3815f25..a04102f 100644 --- a/base/src/main/java/bjc/utils/ioutils/ReportWriter.java +++ b/base/src/main/java/bjc/utils/ioutils/ReportWriter.java @@ -26,8 +26,7 @@ public class ReportWriter extends Writer { // Indent string w/ tabs replaced with spaces public String indentStrSpacedTabs; - public IndentVal() { - } + public IndentVal() {} } // Writer to print to @@ -265,9 +264,7 @@ public class ReportWriter extends Writer { // that case; whether we should continue calling the function, // or just adjust the counts and pretend that nothing // significant happened - while (pageLine > linesPerPage) { - writePage(); - } + while (pageLine > linesPerPage) writePage(); } /** @@ -328,6 +325,11 @@ public class ReportWriter extends Writer { refreshIndents(lvl); } + // @FIXME Ben Culkin 11/13/2020 IntArgument + // Overloading an argument like this is bogus. This should be restructured, + // probably as three different functions; one to refresh a general level, + // one to refresh every level, and one to refresh the default indentation + // Parameter is the level of indents to refresh. // // Pass a index to refresh that level @@ -345,9 +347,7 @@ public class ReportWriter extends Writer { if (lvl == -2) { refreshIndent(defIVal); } else if (lvl == -1) { - for (IndentVal ival : iVals) { - refreshIndent(ival); - } + for (IndentVal ival : iVals) refreshIndent(ival); refreshIndent(defIVal); } else { @@ -364,10 +364,9 @@ public class ReportWriter extends Writer { StringBuilder conv = new StringBuilder(); for (int i = 0; i < indentLength; i++) { char c = vl.indentStr.charAt(i); + if (c == '\t') { - for (int j = 0; j < tabEqv; j++) { - conv.append(' '); - } + for (int j = 0; j < tabEqv; j++) conv.append(' '); vl.indentStrPos += tabEqv; } else { @@ -534,17 +533,14 @@ public class ReportWriter extends Writer { // If we're printing CRLF pairs, make sure that we don't // print incomplete pairs. if (i < lineSpacing - 1) { - if (c == '\n' && lastChar == '\r') - contained.write('\r'); + if (c == '\n' && lastChar == '\r') contained.write('\r'); } } // @NOTE 9/17/18 // // Not sure if this should be here, or before the above loop - if (pageLine > linesPerPage || c == '\f') { - writePage(); - } + if (pageLine > linesPerPage || c == '\f') writePage(); linePos = 0; indentPos = 0; @@ -553,8 +549,7 @@ public class ReportWriter extends Writer { @Override public void write(char[] cbuf, int off, int len) throws IOException { // Skip empty writes - if (len == 0) - return; + if (len == 0) return; // Last character was a new line, print the indent string if (lastCharWasNL) { @@ -568,7 +563,10 @@ public class ReportWriter extends Writer { char c = cbuf[idx]; - if ((c == '\n' && lastChar != '\r') || c == '\n' || c == '\r' || c == '\f') { + if ((c == '\n' && lastChar != '\r') + || c == '\n' + || c == '\r' + || c == '\f') { writeNL(c); } else { if (lastCharWasNL) { @@ -580,9 +578,7 @@ public class ReportWriter extends Writer { if (c == '\t') { linePos += tabEqv; - for (int j = 0; j < tabEqv; j++) { - contained.write(' '); - } + for (int j = 0; j < tabEqv; j++) contained.write(' '); } else { linePos += 1; contained.write(c); @@ -597,12 +593,10 @@ public class ReportWriter extends Writer { for (int j = 0; j < indentLevel; j++) { IndentVal ival = iVals.get(j); - if (printTabsAsSpaces) - contained.write(ival.indentStrSpacedTabs); - else - contained.write(ival.indentStr); + if (printTabsAsSpaces) contained.write(ival.indentStrSpacedTabs); + else contained.write(ival.indentStr); - linePos += ival.indentStrPos; + linePos += ival.indentStrPos; indentPos += ival.indentStrPos; } } @@ -626,4 +620,15 @@ public class ReportWriter extends Writer { public String toString() { return contained.toString(); } + + /** + * Write out a formatted string. + * + * @param format The format string. + * @param items The arguments to the format string. + * @throws IOException Thrown if something goes wrong. + */ + public void writef(String format, Object... items) throws IOException { + write(String.format(format, items)); + } } diff --git a/base/src/main/java/bjc/utils/ioutils/RuleBasedConfigReader.java b/base/src/main/java/bjc/utils/ioutils/RuleBasedConfigReader.java index 2ce2591..12534bd 100644 --- a/base/src/main/java/bjc/utils/ioutils/RuleBasedConfigReader.java +++ b/base/src/main/java/bjc/utils/ioutils/RuleBasedConfigReader.java @@ -6,14 +6,14 @@ import java.util.Scanner; import java.util.function.BiConsumer; import java.util.function.Consumer; -import bjc.data.IHolder; -import bjc.data.IPair; -import bjc.data.Identity; +import bjc.data.Holder; import bjc.data.Pair; +import bjc.data.Identity; +import bjc.data.SimplePair; import bjc.utils.exceptions.UnknownPragma; import bjc.funcdata.FunctionalMap; import bjc.funcdata.FunctionalStringTokenizer; -import bjc.funcdata.IMap; +import bjc.funcdata.MapEx; /** * This class parses a rules based config file, and uses it to drive a provided @@ -31,7 +31,7 @@ public class RuleBasedConfigReader<E> { * * Takes the tokenizer, and a pair of the read token and application state */ - private BiConsumer<FunctionalStringTokenizer, IPair<String, E>> start; + private BiConsumer<FunctionalStringTokenizer, Pair<String, E>> start; /* * Function to use when continuing a rule. @@ -52,7 +52,7 @@ public class RuleBasedConfigReader<E> { * * Pragma actions are functions taking a tokenizer and application state */ - private final IMap<String, BiConsumer<FunctionalStringTokenizer, E>> pragmas; + private final MapEx<String, BiConsumer<FunctionalStringTokenizer, E>> pragmas; /** * Create a new rule-based config reader @@ -65,7 +65,7 @@ public class RuleBasedConfigReader<E> { * The action to fire when ending a rule */ public RuleBasedConfigReader( - final BiConsumer<FunctionalStringTokenizer, IPair<String, E>> start, + final BiConsumer<FunctionalStringTokenizer, Pair<String, E>> start, final BiConsumer<FunctionalStringTokenizer, E> continueRule, final Consumer<E> end) { this.start = start; @@ -161,7 +161,7 @@ public class RuleBasedConfigReader<E> { /* * This is true when a rule's open */ - final IHolder<Boolean> isRuleOpen = new Identity<>(false); + final Holder<Boolean> isRuleOpen = new Identity<>(false); /* * Do something for every line of the file @@ -237,7 +237,7 @@ public class RuleBasedConfigReader<E> { * The action to execute on starting of a rule */ public void setStartRule( - final BiConsumer<FunctionalStringTokenizer, IPair<String, E>> start) { + final BiConsumer<FunctionalStringTokenizer, Pair<String, E>> start) { if (start == null) throw new NullPointerException("Action on rule start must be non-null"); @@ -270,7 +270,7 @@ public class RuleBasedConfigReader<E> { /* * Handle pragmas */ - pragmas.getOrDefault(token, (tokenzer, stat) -> { + pragmas.get(token).orElse((tokenzer, stat) -> { throw new UnknownPragma("Unknown pragma " + token); }).accept(tokenizer, state); } else { @@ -284,7 +284,7 @@ public class RuleBasedConfigReader<E> { /* * Start a rule */ - start.accept(tokenizer, new Pair<>(nextToken, state)); + start.accept(tokenizer, new SimplePair<>(nextToken, state)); isRuleOpen = true; } diff --git a/base/src/main/java/bjc/utils/ioutils/SimpleProperties.java b/base/src/main/java/bjc/utils/ioutils/SimpleProperties.java index 754ed45..d380866 100644 --- a/base/src/main/java/bjc/utils/ioutils/SimpleProperties.java +++ b/base/src/main/java/bjc/utils/ioutils/SimpleProperties.java @@ -170,17 +170,20 @@ public class SimpleProperties implements Map<String, String> { return props.isEmpty(); } - @Override + @SuppressWarnings("unlikely-arg-type") + @Override public boolean containsKey(final Object key) { return props.containsKey(key); } - @Override + @SuppressWarnings("unlikely-arg-type") + @Override public boolean containsValue(final Object value) { return props.containsValue(value); } - @Override + @SuppressWarnings("unlikely-arg-type") + @Override public String get(final Object key) { return props.get(key); } @@ -190,7 +193,8 @@ public class SimpleProperties implements Map<String, String> { return props.put(key, value); } - @Override + @SuppressWarnings("unlikely-arg-type") + @Override public String remove(final Object key) { return props.remove(key); } diff --git a/base/src/main/java/bjc/utils/ioutils/TextAreaOutputStream.java b/base/src/main/java/bjc/utils/ioutils/TextAreaOutputStream.java new file mode 100644 index 0000000..10f5104 --- /dev/null +++ b/base/src/main/java/bjc/utils/ioutils/TextAreaOutputStream.java @@ -0,0 +1,35 @@ +package bjc.utils.ioutils; + +import java.io.IOException; +import java.io.OutputStream; + +import javax.swing.JTextArea; + +/** + * An output stream that prints to a JTextArea + * + * @author epr + * @author Levente S\u00e1ntha (lsantha@users.sourceforge.net) + */ +public class TextAreaOutputStream extends OutputStream { + private final JTextArea textArea; + + /** + * Create a new output stream attached to a textarea + * + * @param console + * The textarea to write to + */ + public TextAreaOutputStream(final JTextArea console) { + this.textArea = console; + } + + @Override + public void write(final int b) throws IOException { + textArea.append("" + (char) b); + + if (b == '\n') { + textArea.repaint(); + } + } +} |
