summaryrefslogtreecommitdiff
path: root/base/src/main/java/bjc/utils/ioutils/RuleBasedReaderPragmas.java
diff options
context:
space:
mode:
authorBenjamin J. Culkin <bjculkin@mix.wvu.edu>2017-10-08 22:39:59 -0300
committerBenjamin J. Culkin <bjculkin@mix.wvu.edu>2017-10-08 22:39:59 -0300
commitc82e3b3b2de0633317ec8fc85925e91422820597 (patch)
tree96567416ce23c5ce85601f9cedc3a94bb1c55cba /base/src/main/java/bjc/utils/ioutils/RuleBasedReaderPragmas.java
parentb3ac1c8690c3e14c879913e5dcc03a5f5e14876e (diff)
Start splitting into maven modules
Diffstat (limited to 'base/src/main/java/bjc/utils/ioutils/RuleBasedReaderPragmas.java')
-rw-r--r--base/src/main/java/bjc/utils/ioutils/RuleBasedReaderPragmas.java100
1 files changed, 100 insertions, 0 deletions
diff --git a/base/src/main/java/bjc/utils/ioutils/RuleBasedReaderPragmas.java b/base/src/main/java/bjc/utils/ioutils/RuleBasedReaderPragmas.java
new file mode 100644
index 0000000..e26a7ee
--- /dev/null
+++ b/base/src/main/java/bjc/utils/ioutils/RuleBasedReaderPragmas.java
@@ -0,0 +1,100 @@
+package bjc.utils.ioutils;
+
+import java.util.function.BiConsumer;
+
+import bjc.utils.exceptions.PragmaFormatException;
+import bjc.utils.funcdata.FunctionalStringTokenizer;
+import bjc.utils.funcutils.ListUtils;
+
+/**
+ * Contains factory methods for common pragma types
+ *
+ * @author ben
+ *
+ */
+public class RuleBasedReaderPragmas {
+
+ /**
+ * Creates a pragma that takes a single integer argument
+ *
+ * @param <StateType>
+ * The type of state that goes along with this pragma
+ * @param name
+ * The name of this pragma, for error message purpose
+ * @param consumer
+ * The function to invoke with the parsed integer
+ * @return A pragma that functions as described above.
+ */
+ public static <StateType> BiConsumer<FunctionalStringTokenizer, StateType> buildInteger(final String name,
+ final BiConsumer<Integer, StateType> consumer) {
+ return (tokenizer, state) -> {
+ /*
+ * Check our input is correct
+ */
+ if (!tokenizer.hasMoreTokens()) {
+ String fmt = "Pragma %s requires one integer argument";
+
+ throw new PragmaFormatException(String.format(fmt, name));
+ }
+
+ /*
+ * Read the argument
+ */
+ final String token = tokenizer.nextToken();
+
+ try {
+ /*
+ * Run the pragma
+ */
+ consumer.accept(Integer.parseInt(token), state);
+ } catch (final NumberFormatException nfex) {
+ /*
+ * Tell the user their argument isn't correct
+ */
+ String fmt = "Argument %s to %s pragma isn't a valid integer, and this pragma requires an integer argument.";
+
+ final PragmaFormatException pfex = new PragmaFormatException(String.format(fmt, token, name));
+
+ pfex.initCause(nfex);
+
+ throw pfex;
+ }
+ };
+ }
+
+ /**
+ * Creates a pragma that takes any number of arguments and collapses
+ * them all into a single string
+ *
+ * @param <StateType>
+ * The type of state that goes along with this pragma
+ * @param name
+ * The name of this pragma, for error message purpose
+ * @param consumer
+ * The function to invoke with the parsed string
+ * @return A pragma that functions as described above.
+ */
+ public static <StateType> BiConsumer<FunctionalStringTokenizer, StateType> buildStringCollapser(
+ final String name, final BiConsumer<String, StateType> consumer) {
+ return (tokenizer, state) -> {
+ /*
+ * Check our input
+ */
+ if (!tokenizer.hasMoreTokens()) {
+ String fmt = "Pragma %s requires one or more string arguments.";
+
+ throw new PragmaFormatException(String.format(fmt, name));
+ }
+
+ /*
+ * Build our argument
+ */
+ final String collapsed = ListUtils.collapseTokens(tokenizer.toList());
+
+ /*
+ * Run the pragma
+ */
+ consumer.accept(collapsed, state);
+ };
+ }
+}