summaryrefslogtreecommitdiff
path: root/BJC-Utils2
diff options
context:
space:
mode:
authorstudent <student@192.168.1.186>2017-04-07 11:34:00 -0400
committerstudent <student@192.168.1.186>2017-04-07 11:34:00 -0400
commit7692fa077a84972231948354d3f0de99f27a9ad7 (patch)
treea7539aff7732339f7655c35f2c2c07723618127b /BJC-Utils2
parent63d88eb8db1f7a6d5924ec2a8b7f462373d5ac9a (diff)
Fix property bugs
Diffstat (limited to 'BJC-Utils2')
-rw-r--r--BJC-Utils2/src/main/java/bjc/utils/PropertyDB.java30
-rw-r--r--BJC-Utils2/src/main/java/bjc/utils/funcutils/StringUtils.java72
-rw-r--r--BJC-Utils2/src/main/java/bjc/utils/ioutils/SimpleProperties.java33
-rw-r--r--BJC-Utils2/src/main/java/bjc/utils/parserutils/TokenUtils.java2
4 files changed, 76 insertions, 61 deletions
diff --git a/BJC-Utils2/src/main/java/bjc/utils/PropertyDB.java b/BJC-Utils2/src/main/java/bjc/utils/PropertyDB.java
index 20be2fc..3f2c078 100644
--- a/BJC-Utils2/src/main/java/bjc/utils/PropertyDB.java
+++ b/BJC-Utils2/src/main/java/bjc/utils/PropertyDB.java
@@ -15,8 +15,8 @@ import java.util.regex.Pattern;
*
*/
public class PropertyDB {
- private static SimpleProperties regexes;
- private static Map<String, Pattern> compiledRegexes;
+ private static SimpleProperties regexes;
+ private static Map<String, Pattern> compiledRegexes;
private static SimpleProperties formats;
@@ -37,13 +37,20 @@ public class PropertyDB {
*/
public static void reloadProperties() {
loadLock.write(() -> {
+ System.out.println("Reading regex properties:");
regexes = new SimpleProperties();
regexes.loadFrom(PropertyDB.class.getResourceAsStream("/regexes.sprop"), false);
+ System.out.println();
compiledRegexes = new HashMap<>();
+ System.out.println("Reading format properties:");
formats = new SimpleProperties();
formats.loadFrom(PropertyDB.class.getResourceAsStream("/formats.sprop"), false);
+ System.out.println();
+
+ if (regexes.equals(formats))
+ System.out.println("WAT");
});
}
@@ -51,13 +58,13 @@ public class PropertyDB {
* Retrieve a persisted regular expression.
*
* @param key
- * The name of the regular expression.
+ * The name of the regular expression.
*
* @return The regular expression with that name.
*/
public static String getRegex(String key) {
return loadLock.read(() -> {
- if(!regexes.containsKey(key)) {
+ if (!regexes.containsKey(key)) {
String msg = String.format("No regular expression named '%s' found", key);
throw new NoSuchElementException(msg);
@@ -72,13 +79,13 @@ public class PropertyDB {
* expression.
*
* @param key
- * The name of the regular expression.
+ * The name of the regular expression.
*
* @return The regular expression with that name.
*/
public static Pattern getCompiledRegex(String key) {
return loadLock.read(() -> {
- if(!regexes.containsKey(key)) {
+ if (!regexes.containsKey(key)) {
String msg = String.format("No regular expression named '%s' found", key);
throw new NoSuchElementException(msg);
@@ -94,13 +101,13 @@ public class PropertyDB {
* Retrieve a persisted format string.
*
* @param key
- * The name of the format string.
+ * The name of the format string.
*
* @return The format string with that name.
*/
public static String getFormat(String key) {
return loadLock.read(() -> {
- if(!formats.containsKey(key)) {
+ if (!formats.containsKey(key)) {
String msg = String.format("No format string named '%s' found", key);
throw new NoSuchElementException(msg);
@@ -111,14 +118,13 @@ public class PropertyDB {
}
/**
- * Retrieve a persisted format string, and apply it to a set of
- * arguments.
+ * Retrieve a persisted format string, and apply it to a set of arguments.
*
* @param key
- * The name of the format string.
+ * The name of the format string.
*
* @param objects
- * The parameters to the format string.
+ * The parameters to the format string.
*
* @return The format string with that name.
*/
diff --git a/BJC-Utils2/src/main/java/bjc/utils/funcutils/StringUtils.java b/BJC-Utils2/src/main/java/bjc/utils/funcutils/StringUtils.java
index 540fb39..d5eaadf 100644
--- a/BJC-Utils2/src/main/java/bjc/utils/funcutils/StringUtils.java
+++ b/BJC-Utils2/src/main/java/bjc/utils/funcutils/StringUtils.java
@@ -16,53 +16,51 @@ public class StringUtils {
* expression
*
* @param input
- * The string to check
- * @param regex
- * 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
+ * The string to check
+ * @param rRegex
+ * 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(String input, String regex) {
- if(input == null)
+ public static boolean containsOnly(String input, String rRegex) {
+ if (input == null)
throw new NullPointerException("Input must not be null");
- else if(regex == 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.
*
* First, we match the beginning of the string. Then, we start a
- * non-capturing group whose contents are the passed in regex.
- * That group is then matched one or more times and the pattern
- * matches to the end of the string
+ * non-capturing group whose contents are the passed in regex. That
+ * group is then matched one or more times and the pattern matches to
+ * the end of the string
*/
- return input.matches("\\A(?:" + regex + ")+\\Z");
+ return input.matches("\\A(?:" + rRegex + ")+\\Z");
}
/**
* 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(StringBuilder builder, int levels) {
- for(int i = 0; i < levels; i++) {
+ for (int i = 0; i < levels; i++) {
builder.append("\t");
}
}
/**
- * Print out a deque with a special case for easily showing a deque is
- * empty
+ * Print out a deque with a special case for easily showing a deque is empty
*
* @param <ContainedType>
- * The type in the deque
+ * The type in the deque
* @param queue
- * The deque to print
- * @return A string version of the deque, with allowance for an empty
- * deque
+ * The deque to print
+ * @return A string version of the deque, with allowance for an empty deque
*/
public static <ContainedType> String printDeque(Deque<ContainedType> queue) {
return queue.isEmpty() ? "(none)" : queue.toString();
@@ -72,17 +70,17 @@ 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(Object[] objects, String join, String comma) {
- if(objects == null) {
+ if (objects == null) {
throw new NullPointerException("Sequence must not be null");
}
@@ -91,7 +89,7 @@ public class StringUtils {
String joiner = join;
String coma = comma;
- switch(objects.length) {
+ switch (objects.length) {
case 0:
/*
* Empty list.
@@ -115,7 +113,7 @@ public class StringUtils {
/*
* 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 + " ");
}
@@ -137,10 +135,10 @@ 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.
*/
@@ -152,14 +150,14 @@ 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(Object[] objects, boolean and) {
- if(and) {
+ if (and) {
return toEnglishList(objects, "and");
} else {
return toEnglishList(objects, "or");
@@ -170,7 +168,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.
*/
@@ -179,7 +177,7 @@ public class StringUtils {
it.setText(value);
int count = 0;
- while(it.next() != BreakIterator.DONE) {
+ while (it.next() != BreakIterator.DONE) {
count++;
}
diff --git a/BJC-Utils2/src/main/java/bjc/utils/ioutils/SimpleProperties.java b/BJC-Utils2/src/main/java/bjc/utils/ioutils/SimpleProperties.java
index e3b0122..a43b16a 100644
--- a/BJC-Utils2/src/main/java/bjc/utils/ioutils/SimpleProperties.java
+++ b/BJC-Utils2/src/main/java/bjc/utils/ioutils/SimpleProperties.java
@@ -15,7 +15,7 @@ import java.util.Set;
*
*/
public class SimpleProperties implements Map<String, String> {
- private static Map<String, String> props;
+ private Map<String, String> props;
/**
* Create a new set of simple properties.
@@ -32,33 +32,37 @@ public class SimpleProperties implements Map<String, String> {
* All leading/trailing spaces from the name & body are removed.
*
* @param is
- * The stream to read from.
+ * The stream to read from.
*
* @param allowDuplicates
- * Whether or not duplicate keys should be allowed.
+ * Whether or not duplicate keys should be allowed.
*/
public void loadFrom(InputStream is, boolean allowDuplicates) {
- try(Scanner scn = new Scanner(is)) {
- while(scn.hasNextLine()) {
+ try (Scanner scn = new Scanner(is)) {
+ while (scn.hasNextLine()) {
String ln = scn.nextLine().trim();
/*
* Skip blank lines/comments
*/
- if(ln.equals("")) continue;
- if(ln.equals("#")) continue;
+ if (ln.equals(""))
+ continue;
+ if (ln.startsWith("#"))
+ continue;
int sepIdx = ln.indexOf(' ');
- if(sepIdx == -1) {
- throw new NoSuchElementException(
- "Properties must be a name, a space, then the body");
+ if (sepIdx == -1) {
+ String fmt = "Properties must be a name, a space, then the body.\n\tOffending line is '%s'";
+ String msg = String.format(fmt, ln);
+
+ throw new NoSuchElementException(msg);
}
String name = ln.substring(0, sepIdx).trim();
String body = ln.substring(sepIdx).trim();
- if(!allowDuplicates && containsKey(name)) {
+ if (!allowDuplicates && containsKey(name)) {
String msg = String.format("Duplicate key '%s'", name);
throw new IllegalStateException(msg);
@@ -67,6 +71,13 @@ public class SimpleProperties implements Map<String, String> {
put(name, body);
}
}
+
+ System.out.println("Read properties:");
+ for (Entry<String, String> entry : entrySet()) {
+ System.out.printf("\t'%s'\t'%s'\n", entry.getKey(), entry.getValue());
+ }
+ System.out.println();
+
}
@Override
diff --git a/BJC-Utils2/src/main/java/bjc/utils/parserutils/TokenUtils.java b/BJC-Utils2/src/main/java/bjc/utils/parserutils/TokenUtils.java
index 52eba1d..1d31522 100644
--- a/BJC-Utils2/src/main/java/bjc/utils/parserutils/TokenUtils.java
+++ b/BJC-Utils2/src/main/java/bjc/utils/parserutils/TokenUtils.java
@@ -32,7 +32,7 @@ public class TokenUtils {
private static Pattern escapePatt = Pattern.compile(rEscapeString);
- private static String rDoubleQuoteString = applyFormat("doubleQuotes", getRegex("nonEscape"),
+ private static String rDoubleQuoteString = applyFormat("doubleQuotes", getRegex("nonStringEscape"),
rPossibleEscapeString);
private static Pattern doubleQuotePatt = Pattern.compile(rDoubleQuoteString);