summaryrefslogtreecommitdiff
path: root/BJC-Utils2/src/main/java/bjc
diff options
context:
space:
mode:
authorbjculkin <bjculkin@mix.wvu.edu>2017-04-06 16:02:40 -0400
committerbjculkin <bjculkin@mix.wvu.edu>2017-04-06 16:02:40 -0400
commit0be891100df83544c89651c815105832e3e11eb9 (patch)
treed9ef2b10e6cf39c77911a54be5cb62f782054b50 /BJC-Utils2/src/main/java/bjc
parent0ff9ed78aadb32f85ee3e37f5574d7ca2e12cdf5 (diff)
Start moving things to properties
This cuts down on the amount of regex escapes
Diffstat (limited to 'BJC-Utils2/src/main/java/bjc')
-rw-r--r--BJC-Utils2/src/main/java/bjc/utils/PropertyDB.java128
1 files changed, 128 insertions, 0 deletions
diff --git a/BJC-Utils2/src/main/java/bjc/utils/PropertyDB.java b/BJC-Utils2/src/main/java/bjc/utils/PropertyDB.java
new file mode 100644
index 0000000..20be2fc
--- /dev/null
+++ b/BJC-Utils2/src/main/java/bjc/utils/PropertyDB.java
@@ -0,0 +1,128 @@
+package bjc.utils;
+
+import bjc.utils.funcutils.LambdaLock;
+import bjc.utils.ioutils.SimpleProperties;
+
+import java.util.HashMap;
+import java.util.Map;
+import java.util.NoSuchElementException;
+import java.util.regex.Pattern;
+
+/**
+ * Database for storage of properties from external files.
+ *
+ * @author EVE
+ *
+ */
+public class PropertyDB {
+ private static SimpleProperties regexes;
+ private static Map<String, Pattern> compiledRegexes;
+
+ private static SimpleProperties formats;
+
+ /*
+ * The lock to use to ensure a read can't happen during a reload
+ */
+ private static LambdaLock loadLock = new LambdaLock();
+
+ static {
+ reloadProperties();
+ }
+
+ /**
+ * Reload all the properties from their files.
+ *
+ * NOTE: Any attempts to read from the property DB while properties are
+ * being loaded will block, to prevent reads from partial states.
+ */
+ public static void reloadProperties() {
+ loadLock.write(() -> {
+ regexes = new SimpleProperties();
+ regexes.loadFrom(PropertyDB.class.getResourceAsStream("/regexes.sprop"), false);
+
+ compiledRegexes = new HashMap<>();
+
+ formats = new SimpleProperties();
+ formats.loadFrom(PropertyDB.class.getResourceAsStream("/formats.sprop"), false);
+ });
+ }
+
+ /**
+ * Retrieve a persisted regular expression.
+ *
+ * @param key
+ * 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)) {
+ String msg = String.format("No regular expression named '%s' found", key);
+
+ throw new NoSuchElementException(msg);
+ }
+
+ return regexes.get(key);
+ });
+ }
+
+ /**
+ * Retrieve a persisted regular expression, compiled into a regular
+ * expression.
+ *
+ * @param key
+ * 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)) {
+ String msg = String.format("No regular expression named '%s' found", key);
+
+ throw new NoSuchElementException(msg);
+ }
+
+ return compiledRegexes.computeIfAbsent(key, strang -> {
+ return Pattern.compile(regexes.get(strang));
+ });
+ });
+ }
+
+ /**
+ * Retrieve a persisted format string.
+ *
+ * @param key
+ * 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)) {
+ String msg = String.format("No format string named '%s' found", key);
+
+ throw new NoSuchElementException(msg);
+ }
+
+ return formats.get(key);
+ });
+ }
+
+ /**
+ * Retrieve a persisted format string, and apply it to a set of
+ * arguments.
+ *
+ * @param key
+ * The name of the format string.
+ *
+ * @param objects
+ * The parameters to the format string.
+ *
+ * @return The format string with that name.
+ */
+ public static String applyFormat(String key, Object... objects) {
+ return String.format(getFormat(key), objects);
+ }
+} \ No newline at end of file