summaryrefslogtreecommitdiff
path: root/BJC-Utils2/src/main/java/bjc/utils/ioutils
diff options
context:
space:
mode:
Diffstat (limited to 'BJC-Utils2/src/main/java/bjc/utils/ioutils')
-rw-r--r--BJC-Utils2/src/main/java/bjc/utils/ioutils/RegexStringEditor.java75
-rw-r--r--BJC-Utils2/src/main/java/bjc/utils/ioutils/SimpleProperties.java28
2 files changed, 81 insertions, 22 deletions
diff --git a/BJC-Utils2/src/main/java/bjc/utils/ioutils/RegexStringEditor.java b/BJC-Utils2/src/main/java/bjc/utils/ioutils/RegexStringEditor.java
index 2825346..4f66a99 100644
--- a/BJC-Utils2/src/main/java/bjc/utils/ioutils/RegexStringEditor.java
+++ b/BJC-Utils2/src/main/java/bjc/utils/ioutils/RegexStringEditor.java
@@ -1,7 +1,12 @@
package bjc.utils.ioutils;
+import bjc.utils.data.Toggle;
+import bjc.utils.data.ValueToggle;
+import bjc.utils.funcdata.FunctionalList;
+import bjc.utils.funcdata.IList;
import bjc.utils.functypes.ID;
+import java.util.function.BiFunction;
import java.util.function.UnaryOperator;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
@@ -13,6 +18,8 @@ import java.util.regex.Pattern;
*
*/
public class RegexStringEditor {
+ private static final UnaryOperator<String> SID = ID.id();
+
/**
* Replace every occurrence of the pattern with the result of applying
* the action to the string matched by the pattern.
@@ -29,7 +36,7 @@ public class RegexStringEditor {
* @return The string, with matches replaced with the action.
*/
public static String onOccurances(String input, Pattern patt, UnaryOperator<String> action) {
- return occurances(input, patt, ID.id(), action);
+ return reduceOccurances(input, patt, SID, action);
}
/**
@@ -49,7 +56,7 @@ public class RegexStringEditor {
* the action.
*/
public static String betweenOccurances(String input, Pattern patt, UnaryOperator<String> action) {
- return occurances(input, patt, action, ID.id());
+ return reduceOccurances(input, patt, action, SID);
}
/**
@@ -58,7 +65,7 @@ public class RegexStringEditor {
* @param input
* The input string.
*
- * @param patt
+ * @param rPatt
* The pattern to match against the string.
*
* @param betweenAction
@@ -69,11 +76,59 @@ public class RegexStringEditor {
*
* @return The string, with both actions applied.
*/
- public static String occurances(String input, Pattern patt, UnaryOperator<String> betweenAction,
+ public static String reduceOccurances(String input, Pattern rPatt, UnaryOperator<String> betweenAction,
UnaryOperator<String> onAction) {
- Matcher matcher = patt.matcher(input);
+ IList<String> occurances = listOccurances(input, rPatt);
+
+ Toggle<UnaryOperator<String>> actions = new ValueToggle<>(onAction, betweenAction);
+ BiFunction<String, StringBuilder, StringBuilder> reducer = (strang, state) -> {
+ return state.append(actions.get().apply(strang));
+ };
+
+ return occurances.reduceAux(new StringBuilder(), reducer, StringBuilder::toString);
+ }
+
+ /**
+ * Execute actions between and on matches of a regular expression.
+ *
+ * @param input
+ * The input string.
+ *
+ * @param rPatt
+ * The pattern to match against the string.
+ *
+ * @param betweenAction
+ * The function to execute between matches of the string.
+ *
+ * @param onAction
+ * The function to execute on matches of the string.
+ *
+ * @return The string, with both actions applied.
+ */
+ public static IList<String> mapOccurances(String input, Pattern rPatt, UnaryOperator<String> betweenAction,
+ UnaryOperator<String> onAction) {
+ IList<String> occurances = listOccurances(input, rPatt);
+ Toggle<UnaryOperator<String>> actions = new ValueToggle<>(onAction, betweenAction);
+
+ return occurances.map(strang -> actions.get().apply(strang));
+ }
+
+ /**
+ * Separate a string into match/non-match segments.
+ *
+ * @param input
+ * The string to separate.
+ *
+ * @param rPatt
+ * The pattern to use for separation.
+ *
+ * @return The string, as a list of match/non-match segments,
+ * starting/ending with a non-match segment.
+ */
+ public static IList<String> listOccurances(String input, Pattern rPatt) {
+ IList<String> res = new FunctionalList<>();
- StringBuffer res = new StringBuffer();
+ Matcher matcher = rPatt.matcher(input);
StringBuffer work = new StringBuffer();
@@ -82,17 +137,17 @@ public class RegexStringEditor {
matcher.appendReplacement(work, "");
- res.append(betweenAction.apply(matcher.toString()));
- res.append(onAction.apply(match));
+ res.add(work.toString());
+ res.add(match);
work = new StringBuffer();
}
matcher.appendTail(work);
- res.append(betweenAction.apply(work.toString()));
+ res.add(work.toString());
- return res.toString();
+ return res;
}
/**
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 a43b16a..531e8b8 100644
--- a/BJC-Utils2/src/main/java/bjc/utils/ioutils/SimpleProperties.java
+++ b/BJC-Utils2/src/main/java/bjc/utils/ioutils/SimpleProperties.java
@@ -32,27 +32,25 @@ 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.startsWith("#"))
- continue;
+ if(ln.equals("")) continue;
+ if(ln.startsWith("#")) continue;
int sepIdx = ln.indexOf(' ');
- if (sepIdx == -1) {
+ 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);
@@ -62,7 +60,7 @@ public class SimpleProperties implements Map<String, String> {
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);
@@ -71,13 +69,19 @@ public class SimpleProperties implements Map<String, String> {
put(name, body);
}
}
+ }
+ /**
+ * Output the set of read properties.
+ */
+ public void outputProperties() {
System.out.println("Read properties:");
- for (Entry<String, String> entry : entrySet()) {
+
+ for(Entry<String, String> entry : entrySet()) {
System.out.printf("\t'%s'\t'%s'\n", entry.getKey(), entry.getValue());
}
- System.out.println();
+ System.out.println();
}
@Override