summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorbculkin2442 <bjculkin@mix.wvu.edu>2019-06-24 20:38:31 -0400
committerbculkin2442 <bjculkin@mix.wvu.edu>2019-06-24 20:38:31 -0400
commit39dad5c09e73a464c26086741d12dd62e5ebcc08 (patch)
tree50a3b85d23d72b6d37d6c11830b94774d0b02fef
parent63ec2f8e2d1af65f03a74c57ae2bc6c9cdafa19f (diff)
Added ReplSet - a container for List<ReplPair>
At the moment, all ReplSet is is a convenient way to wrap around the interface of ReplPair in a convenient way. It will probably have some extra functionality in the future, though I am not sure of the details as of yet.
-rw-r--r--src/main/java/bjc/everge/ReplSet.java96
1 files changed, 96 insertions, 0 deletions
diff --git a/src/main/java/bjc/everge/ReplSet.java b/src/main/java/bjc/everge/ReplSet.java
new file mode 100644
index 0000000..a477456
--- /dev/null
+++ b/src/main/java/bjc/everge/ReplSet.java
@@ -0,0 +1,96 @@
+package bjc.everge;
+
+import java.io.FileInputStream;
+import java.io.IOException;
+import java.io.InputStream;
+
+import java.util.ArrayList;
+import java.util.List;
+import java.util.Scanner;
+
+/**
+ * A set of ReplPairs, kept together for easy use
+ *
+ * @author Ben Culkin
+ */
+public class ReplSet {
+ // The list of pairs
+ private List<ReplPair> parList;
+
+ /**
+ * Create a new blank set of pairs.
+ */
+ public ReplSet() {
+ parList = new ArrayList<>();
+ }
+
+ /**
+ * Create a new set of pairs using an existing list of pairs.
+ *
+ * Changes to the list of pairs will carry across to the ReplSet, so be careful about that.
+ *
+ * @param lst
+ * The list of pairs to use.
+ */
+ public ReplSet(List<ReplPair> lst) {
+ parList = lst;
+ }
+
+ public static ReplSet fromFile(String fName) throws IOException {
+ ReplSet rs = new ReplSet();
+
+ try (FileInputStream fis = new FileInputStream(fName); Scanner scn = new Scanner(fis)) {
+ rs.parList = ReplPair.readList(scn);
+ }
+
+ return rs;
+ }
+
+ /**
+ * Adds more pairs to the ReplSet.
+ *
+ * @param pars
+ * The pairs to add to the ReplSet.
+ */
+ public void addPairs(List<ReplPair> pars) {
+ for (ReplPair par : pars) {
+ parList.add(par);
+ }
+
+ // Resort the pairs into priority order
+ parList.sort(null);
+ }
+
+ /**
+ * Adds more pairs to the ReplSet.
+ *
+ * @param pars
+ * The pairs to add to the ReplSet.
+ */
+ public void addPairs(ReplPair... pars) {
+ for (ReplPair par : pars) {
+ parList.add(par);
+ }
+
+ // Resort the pairs into priority order
+ parList.sort(null);
+ }
+
+ /**
+ * Apply the ReplSet to a string.
+ *
+ * @param val
+ * The string to apply the ReplSet to.
+ *
+ * @return The result of applying the ReplSet.
+ */
+ public String apply(String val) {
+ String ret = val;
+
+ for (ReplPair par : parList) {
+ ret = par.apply(ret);
+ }
+
+ return ret;
+ }
+}