diff options
Diffstat (limited to 'base/src/main/java/bjc')
4 files changed, 77 insertions, 20 deletions
diff --git a/base/src/main/java/bjc/utils/funcutils/IteratorUtils.java b/base/src/main/java/bjc/utils/funcutils/IteratorUtils.java index d691bab..732f1e9 100644 --- a/base/src/main/java/bjc/utils/funcutils/IteratorUtils.java +++ b/base/src/main/java/bjc/utils/funcutils/IteratorUtils.java @@ -3,7 +3,7 @@ package bjc.utils.funcutils; import java.util.*; import java.util.function.*; -import bjc.utils.data.*; +import bjc.utils.data.ArrayIterator; /** * Utility methods for dealing with iterators. diff --git a/base/src/main/java/bjc/utils/ioutils/ReportWriter.java b/base/src/main/java/bjc/utils/ioutils/ReportWriter.java index 4eb5aef..bc9b046 100644 --- a/base/src/main/java/bjc/utils/ioutils/ReportWriter.java +++ b/base/src/main/java/bjc/utils/ioutils/ReportWriter.java @@ -1,6 +1,7 @@ package bjc.utils.ioutils; import java.io.IOException; +import java.io.StringWriter; import java.io.Writer; import bjc.utils.esodata.DefaultList; @@ -413,6 +414,9 @@ public class ReportWriter extends Writer { return rw; } + public ReportWriter() { + this(new StringWriter()); + } /** * Create a new ReportWriter. * @param write The place to write to. diff --git a/base/src/main/java/bjc/utils/ioutils/SimpleProperties.java b/base/src/main/java/bjc/utils/ioutils/SimpleProperties.java index 4f41c50..35dc47c 100644 --- a/base/src/main/java/bjc/utils/ioutils/SimpleProperties.java +++ b/base/src/main/java/bjc/utils/ioutils/SimpleProperties.java @@ -1,10 +1,12 @@ package bjc.utils.ioutils; import java.io.InputStream; +import java.io.InputStreamReader; +import java.io.PrintStream; +import java.io.Reader; import java.util.Collection; import java.util.HashMap; import java.util.Map; -import java.util.NoSuchElementException; import java.util.Scanner; import java.util.Set; @@ -15,6 +17,33 @@ import java.util.Set; * */ public class SimpleProperties implements Map<String, String> { + /** + * Exception thrown when there is a duplicate key, when they are forbidden. + * + * @author 15405 + * + */ + public static class DuplicateKeys extends RuntimeException { + private static final long serialVersionUID = -5521190136366024804L; + + /** + * Create a new duplicate key exception. + * + * @param keyName + * The name of the key that has been duplicated. + */ + public DuplicateKeys(String keyName) { + super(String.format("Duplicate value encountered for key '%s'", keyName)); + } + } + + public static class InvalidLineFormat extends RuntimeException { + private static final long serialVersionUID = 5332131472090792841L; + + public InvalidLineFormat(String lne) { + super(String.format("Line '%s' is improperly formatted.\n\tExpected format is a string key, followed by a single space, followed by the value", "")); + } + } private final Map<String, String> props; /** @@ -23,22 +52,40 @@ public class SimpleProperties implements Map<String, String> { public SimpleProperties() { props = new HashMap<>(); } - + /** - * Load properties from the provided input stream. + * Load properties from an input stream. + * + * Delegates to {@link SimpleProperties#loadFrom(Reader, boolean)} to allow + * you to pass a input stream instead of a reader. + * + * @param is + * The stream to read from. + * @param allowDuplicates + * Whether or not duplicate keys should be allowed. + */ + public void loadFrom(final InputStream is, final boolean allowDuplicates) { + loadFrom(new InputStreamReader(is), allowDuplicates); + } + + /** + * Load properties from the provided input reader. * * The format is the name, a space, then the body. * * All leading/trailing spaces from the name & body are removed. * * @param is - * The stream to read from. + * The reader to read from. * * @param allowDuplicates - * Whether or not duplicate keys should be allowed. + * Whether or not duplicate keys should be allowed. If they are, + * the end-value of the property will be what the last one was. + * + * @throws DuplicateKeys If duplicate keys have been found when they are prohibited. */ - public void loadFrom(final InputStream is, final boolean allowDuplicates) { - try(Scanner scn = new Scanner(is)) { + public void loadFrom(final Reader rdr, final boolean allowDuplicates) { + try(Scanner scn = new Scanner(rdr)) { while(scn.hasNextLine()) { final String ln = scn.nextLine().trim(); @@ -55,10 +102,7 @@ public class SimpleProperties implements Map<String, String> { * Complain about improperly formatted lines. */ if(sepIdx == -1) { - final String fmt = "Properties must be a name, a space, then the body.\n\tOffending line is '%s'"; - final String msg = String.format(fmt, ln); - - throw new NoSuchElementException(msg); + throw new InvalidLineFormat(ln); } final String name = ln.substring(0, sepIdx).trim(); @@ -68,9 +112,7 @@ public class SimpleProperties implements Map<String, String> { * Complain about duplicates, if that is wanted. */ if(!allowDuplicates && containsKey(name)) { - final String msg = String.format("Duplicate key '%s'", name); - - throw new IllegalStateException(msg); + throw new DuplicateKeys(name); } put(name, body); @@ -80,15 +122,26 @@ public class SimpleProperties implements Map<String, String> { /** * Output the set of read properties. + * + * Uses System.out, since one isn't specified. */ public void outputProperties() { - System.out.println("Read properties:"); + outputProperties(System.out); + } + + /** + * Output the set of read properties. + * + * @param strim The stream to output properties to. + */ + public void outputProperties(PrintStream strim) { + strim.println("Read properties:"); for(final Entry<String, String> entry : entrySet()) { - System.out.printf("\t'%s'\t'%s'\n", entry.getKey(), entry.getValue()); + strim.printf("\t'%s'\t'%s'\n", entry.getKey(), entry.getValue()); } - System.out.println(); + strim.println(); } @Override diff --git a/base/src/main/java/bjc/utils/misc/PropertyDB.java b/base/src/main/java/bjc/utils/misc/PropertyDB.java index c9434ef..09e1999 100644 --- a/base/src/main/java/bjc/utils/misc/PropertyDB.java +++ b/base/src/main/java/bjc/utils/misc/PropertyDB.java @@ -54,7 +54,7 @@ public class PropertyDB { regexes = new SimpleProperties(); regexes.loadFrom(PropertyDB.class.getResourceAsStream("/regexes.sprop"), false); if(LOGLOAD) { - regexes.outputProperties(); + regexes.outputProperties(System.out); System.out.println(); } compiledRegexes = new HashMap<>(); @@ -67,7 +67,7 @@ public class PropertyDB { formats = new SimpleProperties(); formats.loadFrom(PropertyDB.class.getResourceAsStream("/formats.sprop"), false); if(LOGLOAD) { - formats.outputProperties(); + formats.outputProperties(System.out); System.out.println(); } }); |
