From 82951e37e10b282d9a7c89f4662990b64949c943 Mon Sep 17 00:00:00 2001 From: bculkin2442 Date: Mon, 29 Feb 2016 10:41:17 -0500 Subject: General code cleanup --- .../src/main/java/bjc/utils/data/IHolder.java | 6 +- BJC-Utils2/src/main/java/bjc/utils/data/IPair.java | 15 +++- .../src/main/java/bjc/utils/data/IPrecedent.java | 4 +- .../java/bjc/utils/dice/DiceExpressionType.java | 2 +- .../java/bjc/utils/funcdata/FunctionalList.java | 34 ++++---- .../bjc/utils/funcdata/bst/BinarySearchTree.java | 39 ++++----- .../utils/funcdata/bst/DirectedWalkFunction.java | 25 +++--- .../java/bjc/utils/funcdata/bst/ITreePart.java | 94 +++++++++++++++------- .../main/java/bjc/utils/funcdata/bst/TreeNode.java | 25 +++--- .../src/main/java/bjc/utils/gen/RandomGrammar.java | 34 +++++--- .../main/java/bjc/utils/gen/WeightedGrammar.java | 89 +++++++++----------- .../main/java/bjc/utils/graph/AdjacencyMap.java | 1 + .../java/bjc/utils/gui/FileNotChosenException.java | 14 ++++ .../java/bjc/utils/gui/ListParameterPanel.java | 7 +- .../src/main/java/bjc/utils/gui/SimpleDialogs.java | 3 +- .../java/bjc/utils/gui/awt/SimpleFileDialog.java | 32 ++++---- .../utils/parserutils/RuleBasedConfigReader.java | 28 +++---- .../java/bjc/utils/parserutils/ShuntingYard.java | 25 +++--- .../utils/parserutils/UnknownPragmaException.java | 19 ++++- 19 files changed, 288 insertions(+), 208 deletions(-) (limited to 'BJC-Utils2/src/main/java/bjc/utils') diff --git a/BJC-Utils2/src/main/java/bjc/utils/data/IHolder.java b/BJC-Utils2/src/main/java/bjc/utils/data/IHolder.java index aa7d5bd..b6e752b 100644 --- a/BJC-Utils2/src/main/java/bjc/utils/data/IHolder.java +++ b/BJC-Utils2/src/main/java/bjc/utils/data/IHolder.java @@ -21,7 +21,7 @@ public interface IHolder { * The transformation to apply * @return A holder with the transformed value */ - IHolder map(Function f); + public IHolder map(Function f); /** * Apply the given transformation to the held value. Returns the holder @@ -31,7 +31,7 @@ public interface IHolder { * The transform to apply to the value * @return The holder */ - IHolder transform(Function f); + public IHolder transform(Function f); /** * Returns a raw mapped value, not contained in a GenHolder @@ -40,6 +40,6 @@ public interface IHolder { * The function to use for mapping the value * @return The mapped value outside of a GenHolder */ - E unwrap(Function f); + public E unwrap(Function f); } \ No newline at end of file diff --git a/BJC-Utils2/src/main/java/bjc/utils/data/IPair.java b/BJC-Utils2/src/main/java/bjc/utils/data/IPair.java index 9da7d22..fcf2b05 100644 --- a/BJC-Utils2/src/main/java/bjc/utils/data/IPair.java +++ b/BJC-Utils2/src/main/java/bjc/utils/data/IPair.java @@ -16,7 +16,8 @@ public interface IPair { * The function to apply to the right value. * @return A new pair containing the two modified values. */ - IPair apply(Function lf, Function rf); + public IPair apply(Function lf, + Function rf); /** * Collapse this pair to a single value. Does not change the internal @@ -26,8 +27,14 @@ public interface IPair { * The function to use to collapse the pair. * @return The collapsed value. */ - E merge(BiFunction bf); - - void doWith(BiConsumer bc); + public E merge(BiFunction bf); + /** + * Execute an action with the values of this pair. Has no effect on the + * internal contents + * + * @param bc + * The action to execute on the values + */ + public void doWith(BiConsumer bc); } \ No newline at end of file diff --git a/BJC-Utils2/src/main/java/bjc/utils/data/IPrecedent.java b/BJC-Utils2/src/main/java/bjc/utils/data/IPrecedent.java index 98258ef..1e00a59 100644 --- a/BJC-Utils2/src/main/java/bjc/utils/data/IPrecedent.java +++ b/BJC-Utils2/src/main/java/bjc/utils/data/IPrecedent.java @@ -12,7 +12,7 @@ public interface IPrecedent { * * @return The precedence of the attached object */ - int getPrecedence(); + public int getPrecedence(); /** * Create a new object with set precedence @@ -21,7 +21,7 @@ public interface IPrecedent { * The precedence of the object to handle * @return A new object with set precedence */ - static IPrecedent newSimplePrecedent(int prec) { + public static IPrecedent newSimplePrecedent(int prec) { return () -> prec; } } \ No newline at end of file diff --git a/BJC-Utils2/src/main/java/bjc/utils/dice/DiceExpressionType.java b/BJC-Utils2/src/main/java/bjc/utils/dice/DiceExpressionType.java index 6493dd5..150ed51 100644 --- a/BJC-Utils2/src/main/java/bjc/utils/dice/DiceExpressionType.java +++ b/BJC-Utils2/src/main/java/bjc/utils/dice/DiceExpressionType.java @@ -1,7 +1,7 @@ package bjc.utils.dice; /* - * Enumeration for dice expression operators + * Enumeration for basic dice expression operators */ public enum DiceExpressionType { ADD, DIVIDE, MULTIPLY, SUBTRACT diff --git a/BJC-Utils2/src/main/java/bjc/utils/funcdata/FunctionalList.java b/BJC-Utils2/src/main/java/bjc/utils/funcdata/FunctionalList.java index 3fd05da..d2f0988 100644 --- a/BJC-Utils2/src/main/java/bjc/utils/funcdata/FunctionalList.java +++ b/BJC-Utils2/src/main/java/bjc/utils/funcdata/FunctionalList.java @@ -129,6 +129,7 @@ public class FunctionalList implements Cloneable { return true; } } + return false; } @@ -203,11 +204,7 @@ public class FunctionalList implements Cloneable { public FunctionalList flatMap(Function> f) { FunctionalList fl = new FunctionalList<>(this.wrap.size()); - forEach(e -> { - f.apply(e).forEach(e2 -> { - fl.add(e2); - }); - }); + forEach(e -> f.apply(e).forEach(fl::add)); return fl; } @@ -230,11 +227,13 @@ public class FunctionalList implements Cloneable { * index. */ public void forEachIndexed(BiConsumer c) { - int i = 0; + GenHolder i = new GenHolder<>(0); - for (E e : wrap) { - c.accept(i++, e); - } + wrap.forEach((val) -> { + c.accept(i.unwrap(vl -> vl), val); + + i.transform((vl) -> vl + 1); + }); } /** @@ -267,7 +266,7 @@ public class FunctionalList implements Cloneable { public FunctionalList getMatching(Predicate matchPred) { FunctionalList fl = new FunctionalList<>(); - this.forEach((elem) -> { + wrap.forEach((elem) -> { if (matchPred.test(elem)) { fl.add(elem); } @@ -369,6 +368,10 @@ public class FunctionalList implements Cloneable { return wrap.removeIf(remPred); } + public void removeMatching(E obj) { + removeIf((ele) -> ele.equals(obj)); + } + /** * Perform a binary search for the specified key using the provided * means of comparing elements. Since this IS a binary search, the list @@ -423,9 +426,12 @@ public class FunctionalList implements Cloneable { return sb.toString(); } - public void removeMatching(E obj) { - removeIf((ele) -> { - return ele.equals(obj); - }); + /** + * Retrieve the size of the wrapped list + * + * @return The size of the wrapped list + */ + public int getSize() { + return wrap.size(); } } diff --git a/BJC-Utils2/src/main/java/bjc/utils/funcdata/bst/BinarySearchTree.java b/BJC-Utils2/src/main/java/bjc/utils/funcdata/bst/BinarySearchTree.java index 7bf0007..7665797 100644 --- a/BJC-Utils2/src/main/java/bjc/utils/funcdata/bst/BinarySearchTree.java +++ b/BJC-Utils2/src/main/java/bjc/utils/funcdata/bst/BinarySearchTree.java @@ -5,6 +5,7 @@ import java.util.Comparator; import java.util.List; import java.util.function.Predicate; +import bjc.utils.funcdata.FunctionalList; import bjc.utils.funcdata.bst.ITreePart.TreeLinearizationMethod; /** @@ -49,6 +50,7 @@ public class BinarySearchTree { */ public void addNode(T dat) { nCount++; + if (root == null) { root = new TreeNode(dat, null, null); } else { @@ -61,42 +63,33 @@ public class BinarySearchTree { * time, but also O(N) space. */ public void balance() { - ArrayList elms = new ArrayList<>(nCount); + FunctionalList elms = new FunctionalList<>(); root.forEach(TreeLinearizationMethod.INORDER, e -> elms.add(e)); root = null; - int piv = elms.size() / 2; + int piv = elms.getSize() / 2; int adj = 0; - while ((piv - adj) >= 0 && (piv + adj) < elms.size()) { + while ((piv - adj) >= 0 && (piv + adj) < elms.getSize()) { if (root == null) { - root = new TreeNode(elms.get(piv), null, null); + root = new TreeNode(elms.getByIndex(piv), null, null); } else { - root.add(elms.get(piv + adj), comp); - root.add(elms.get(piv - adj), comp); + root.add(elms.getByIndex(piv + adj), comp); + root.add(elms.getByIndex(piv - adj), comp); } adj++; } if ((piv - adj) >= 0) { - root.add(elms.get(piv - adj), comp); - } else if ((piv + adj) < elms.size()) { - root.add(elms.get(piv + adj), comp); + root.add(elms.getByIndex(piv - adj), comp); + } else if ((piv + adj) < elms.getSize()) { + root.add(elms.getByIndex(piv + adj), comp); } } - /** - * Get the root of the tree. - * - * @return The root of the tree. - */ - public ITreePart getRoot() { - return root; - } - /** * Soft-delete a node from the tree. Soft-deleted nodes stay in the * tree until trim()/balance() is invoked, and are not included in @@ -106,9 +99,19 @@ public class BinarySearchTree { */ public void deleteNode(T dat) { nCount--; + root.delete(dat, comp); } + /** + * Get the root of the tree. + * + * @return The root of the tree. + */ + public ITreePart getRoot() { + return root; + } + /** * Check if a node is in the tree * diff --git a/BJC-Utils2/src/main/java/bjc/utils/funcdata/bst/DirectedWalkFunction.java b/BJC-Utils2/src/main/java/bjc/utils/funcdata/bst/DirectedWalkFunction.java index 18e80be..12c87b3 100644 --- a/BJC-Utils2/src/main/java/bjc/utils/funcdata/bst/DirectedWalkFunction.java +++ b/BJC-Utils2/src/main/java/bjc/utils/funcdata/bst/DirectedWalkFunction.java @@ -2,9 +2,11 @@ package bjc.utils.funcdata.bst; /** * Represents a function for doing a directed walk of a binary tree. + * * @author ben * * @param + * The type of element stored in the walked tree */ @FunctionalInterface public interface DirectedWalkFunction { @@ -15,28 +17,31 @@ public interface DirectedWalkFunction { * */ public enum DirectedWalkResult { - /** - * Specifies that the function has succesfully completed - * - */ - SUCCESS, /** * Specifies that the function has failed. */ FAILURE, - /** + /** * Specifies that the function wants to move left in the tree next. */ - LEFT, + LEFT, /** - * Specifies that the function wants to move right in the tree next. + * Specifies that the function wants to move right in the tree + * next. */ - RIGHT + RIGHT, + /** + * Specifies that the function has succesfully completed + * + */ + SUCCESS } /** * Perform a directed walk on a node of a tree. - * @param data The data stored in the node currently being visited + * + * @param data + * The data stored in the node currently being visited * @return The way the function wants the walk to go next. */ public DirectedWalkResult walk(T data); diff --git a/BJC-Utils2/src/main/java/bjc/utils/funcdata/bst/ITreePart.java b/BJC-Utils2/src/main/java/bjc/utils/funcdata/bst/ITreePart.java index 0c8f12e..dfcfedd 100644 --- a/BJC-Utils2/src/main/java/bjc/utils/funcdata/bst/ITreePart.java +++ b/BJC-Utils2/src/main/java/bjc/utils/funcdata/bst/ITreePart.java @@ -7,75 +7,109 @@ import java.util.function.Predicate; /** * A interface for the fundamental things that want to be part of a tree. + * * @author ben * - * @param The data contained in this part of the tree. + * @param + * The data contained in this part of the tree. */ public interface ITreePart { /** * Represents the ways to linearize a tree for traversal. + * * @author ben * */ public enum TreeLinearizationMethod { /** - * Visit the left side of this tree part, the tree part itself, and then the right part. + * Visit the left side of this tree part, the tree part itself, and + * then the right part. */ - INORDER, + INORDER, /** - * Visit the left side of this tree part, the right side, and then the tree part itself. + * Visit the left side of this tree part, the right side, and then + * the tree part itself. */ - POSTORDER, + POSTORDER, /** - * Visit the tree part itself, then the left side of tthis tree part and then the right part. + * Visit the tree part itself, then the left side of tthis tree + * part and then the right part. */ PREORDER } - + /** * Add a element below this tree part somewhere. - * @param dat The element to add below this tree part - * @param comp The thing to use for comparing values to find where to insert the tree part. + * + * @param dat + * The element to add below this tree part + * @param comp + * The thing to use for comparing values to find where to + * insert the tree part. */ - void add(T dat, Comparator comp); + public void add(T dat, Comparator comp); + /** - * Collapses this tree part into a single value. - * Does not change the underlying tree. - * @param f The function to use to transform data into mapped form. - * @param bf The function to use to collapse data in mapped form into a single value. + * Collapses this tree part into a single value. Does not change the + * underlying tree. + * + * @param f + * The function to use to transform data into mapped form. + * @param bf + * The function to use to collapse data in mapped form into + * a single value. * @return A single value from collapsing the tree. */ - E collapse(Function f, BiFunction bf); + public E collapse(Function f, BiFunction bf); + /** * Check if this tre part or below it contains the specified data item - * @param data The data item to look for. - * @param cmp The comparator to use to search for the data item - * @return Whether or not the given item is contained in this tree part or its children. + * + * @param data + * The data item to look for. + * @param cmp + * The comparator to use to search for the data item + * @return Whether or not the given item is contained in this tree part + * or its children. */ - boolean contains(T data, Comparator cmp); + public boolean contains(T data, Comparator cmp); + /** * Get the data associated with this tree part. + * * @return The data associated with this tree part. */ - T data(); + public T data(); + /** * Remove the given node from this tree part and any of its children. - * @param dat The data item to remove. - * @param cmp The comparator to use to search for the data item. + * + * @param dat + * The data item to remove. + * @param cmp + * The comparator to use to search for the data item. */ - void delete(T dat, Comparator cmp); + public void delete(T dat, Comparator cmp); + /** * Execute a directed walk through the tree. - * @param ds The function to use to direct the walk through the tree. + * + * @param ds + * The function to use to direct the walk through the tree. * @return Whether the directed walk finished successfully. */ - boolean directedWalk(DirectedWalkFunction ds); + public boolean directedWalk(DirectedWalkFunction ds); + /** - * Execute a provided function for each element of tree it succesfully completes for - * @param tlm The way to linearize the tree for executing - * @param c The function to apply to each element, where it returning false - * terminates traversal early + * Execute a provided function for each element of tree it succesfully + * completes for + * + * @param tlm + * The way to linearize the tree for executing + * @param c + * The function to apply to each element, where it returning + * false terminates traversal early * @return Whether the traversal finished succesfully */ - boolean forEach(TreeLinearizationMethod tlm, Predicate c); + public boolean forEach(TreeLinearizationMethod tlm, Predicate c); } diff --git a/BJC-Utils2/src/main/java/bjc/utils/funcdata/bst/TreeNode.java b/BJC-Utils2/src/main/java/bjc/utils/funcdata/bst/TreeNode.java index 40cc53f..e8c6c8b 100644 --- a/BJC-Utils2/src/main/java/bjc/utils/funcdata/bst/TreeNode.java +++ b/BJC-Utils2/src/main/java/bjc/utils/funcdata/bst/TreeNode.java @@ -111,20 +111,17 @@ public class TreeNode extends TreeLeaf { @Override public void delete(T dat, Comparator cmp) { - directedWalk(new DirectedWalkFunction() { - @Override - public DirectedWalkResult walk(T ds) { - switch (cmp.compare(data, dat)) { - case -1: - return left == null ? FAILURE : LEFT; - case 0: - deleted = true; - return FAILURE; - case 1: - return right == null ? FAILURE : RIGHT; - default: - return DirectedWalkResult.FAILURE; - } + directedWalk(ds -> { + switch (cmp.compare(data, dat)) { + case -1: + return left == null ? FAILURE : LEFT; + case 0: + deleted = true; + return FAILURE; + case 1: + return right == null ? FAILURE : RIGHT; + default: + return FAILURE; } }); } diff --git a/BJC-Utils2/src/main/java/bjc/utils/gen/RandomGrammar.java b/BJC-Utils2/src/main/java/bjc/utils/gen/RandomGrammar.java index 0245b17..339b3f4 100644 --- a/BJC-Utils2/src/main/java/bjc/utils/gen/RandomGrammar.java +++ b/BJC-Utils2/src/main/java/bjc/utils/gen/RandomGrammar.java @@ -6,9 +6,11 @@ import bjc.utils.funcdata.FunctionalList; /** * A weighted grammar where all the rules have a equal chance of occuring. + * * @author ben * - * @param The type of grammar elements to use. + * @param + * The type of grammar elements to use. */ public class RandomGrammar extends WeightedGrammar { @@ -21,11 +23,14 @@ public class RandomGrammar extends WeightedGrammar { /** * Add cases to a specified rule. - * @param rule The name of the rule to add cases to. - * @param cases The cases to add for this rule. + * + * @param rule + * The name of the rule to add cases to. + * @param cases + * The cases to add for this rule. */ @SafeVarargs - public final void addCases(E rule, FunctionalList... cases) { + public final void addCases(E rule, FunctionalList... cases) { for (FunctionalList cse : cases) { super.addCase(rule, 1, cse); } @@ -33,13 +38,16 @@ public class RandomGrammar extends WeightedGrammar { /** * Create a rule with the specified name and cases. - * @param rule The name of the rule to add. - * @param cases The cases to add for this rule. + * + * @param rule + * The name of the rule to add. + * @param cases + * The cases to add for this rule. */ @SafeVarargs public final void makeRule(E rule, FunctionalList... cases) { super.addRule(rule); - + for (FunctionalList cse : cases) { super.addCase(rule, 1, cse); } @@ -47,13 +55,15 @@ public class RandomGrammar extends WeightedGrammar { /** * Create a rule with the specified name and cases. - * @param rule The name of the rule to add. - * @param cases The cases to add for this rule. + * + * @param rule + * The name of the rule to add. + * @param cases + * The cases to add for this rule. */ - public void makeRule(E rule, - FunctionalList> cases) { + public void makeRule(E rule, FunctionalList> cases) { super.addRule(rule); - + cases.forEach(cse -> super.addCase(rule, 1, cse)); } } diff --git a/BJC-Utils2/src/main/java/bjc/utils/gen/WeightedGrammar.java b/BJC-Utils2/src/main/java/bjc/utils/gen/WeightedGrammar.java index 7c3ea6a..623c212 100644 --- a/BJC-Utils2/src/main/java/bjc/utils/gen/WeightedGrammar.java +++ b/BJC-Utils2/src/main/java/bjc/utils/gen/WeightedGrammar.java @@ -53,6 +53,7 @@ public class WeightedGrammar { */ public WeightedGrammar(Random src) { this(); + sr = src; } @@ -67,9 +68,7 @@ public class WeightedGrammar { * The case being added. */ public void addCase(E rule, int prob, FunctionalList cse) { - WeightedRandom> rn = rules.get(rule); - - rn.addProb(prob, cse); + rules.get(rule).addProb(prob, cse); } /** @@ -231,32 +230,12 @@ public class WeightedGrammar { } /** - * Create a series of alternatives for a rule by prefixing them with a - * given token + * Check if this grammar has an initial rule * - * @param addProb - * The amount to adjust the probability by - * @param rName - * The name of the rule to prefix - * @param prefixToken - * The token to prefix to the rule + * @return Whether or not this grammar has an initial rule */ - public void prefixRule(E rName, E prefixToken, int addProb) { - WeightedRandom> rule = rules.get(rName); - - FunctionalList>> newResults = new FunctionalList<>(); - - rule.getValues().forEach((par) -> { - FunctionalList nl = par - .merge((left, right) -> right.clone()); - nl.prepend(prefixToken); - - newResults.add(new Pair<>( - par.merge((left, right) -> left) + addProb, nl)); - }); - - newResults.forEach((par) -> par - .doWith((left, right) -> addCase(rName, left, right))); + public boolean hasInitRule() { + return initRule != null && !initRule.equalsIgnoreCase(""); } /** @@ -292,17 +271,41 @@ public class WeightedGrammar { nls.add(nl); } - nls.forEach((ls) -> { - newResults.add(new Pair<>( - par.merge((left, right) -> left) + addProb, ls)); - }); + nls.forEach((ls) -> newResults.add(new Pair<>( + par.merge((left, right) -> left) + addProb, ls))); }); - newResults.forEach((par) -> { - par.doWith((left, right) -> { - addCase(rName, left, right); - }); + newResults.forEach((par) -> par + .doWith((left, right) -> addCase(rName, left, right))); + } + + /** + * Create a series of alternatives for a rule by prefixing them with a + * given token + * + * @param addProb + * The amount to adjust the probability by + * @param rName + * The name of the rule to prefix + * @param prefixToken + * The token to prefix to the rule + */ + public void prefixRule(E rName, E prefixToken, int addProb) { + WeightedRandom> rule = rules.get(rName); + + FunctionalList>> newResults = new FunctionalList<>(); + + rule.getValues().forEach((par) -> { + FunctionalList nl = par + .merge((left, right) -> right.clone()); + nl.prepend(prefixToken); + + newResults.add(new Pair<>( + par.merge((left, right) -> left) + addProb, nl)); }); + + newResults.forEach((par) -> par + .doWith((left, right) -> addCase(rName, left, right))); } /** @@ -376,19 +379,7 @@ public class WeightedGrammar { par.merge((left, right) -> left) + addProb, nl)); }); - newResults.forEach((par) -> { - par.doWith((left, right) -> { - addCase(rName, left, right); - }); - }); - } - - /** - * Check if this grammar has an initial rule - * - * @return Whether or not this grammar has an initial rule - */ - public boolean hasInitRule() { - return initRule != null && !initRule.equalsIgnoreCase(""); + newResults.forEach((par) -> par + .doWith((left, right) -> addCase(rName, left, right))); } } diff --git a/BJC-Utils2/src/main/java/bjc/utils/graph/AdjacencyMap.java b/BJC-Utils2/src/main/java/bjc/utils/graph/AdjacencyMap.java index d3a420e..6420d8e 100644 --- a/BJC-Utils2/src/main/java/bjc/utils/graph/AdjacencyMap.java +++ b/BJC-Utils2/src/main/java/bjc/utils/graph/AdjacencyMap.java @@ -148,6 +148,7 @@ public class AdjacencyMap { .forEach(tgt -> ps.printf("%d ", tgt.getValue())); ps.println(); }); + ps.close(); } } diff --git a/BJC-Utils2/src/main/java/bjc/utils/gui/FileNotChosenException.java b/BJC-Utils2/src/main/java/bjc/utils/gui/FileNotChosenException.java index b972596..2876b1d 100644 --- a/BJC-Utils2/src/main/java/bjc/utils/gui/FileNotChosenException.java +++ b/BJC-Utils2/src/main/java/bjc/utils/gui/FileNotChosenException.java @@ -4,10 +4,24 @@ import java.io.IOException; /** * Represents the user failing to choose a file. + * * @author ben * */ public class FileNotChosenException extends IOException { private static final long serialVersionUID = -8753348705210831096L; + + public FileNotChosenException() { + super(); + } + /** + * Create a new exception with the given cause + * + * @param cause + * The cause of why the exception was thrown + */ + public FileNotChosenException(String cause) { + super(cause); + } } diff --git a/BJC-Utils2/src/main/java/bjc/utils/gui/ListParameterPanel.java b/BJC-Utils2/src/main/java/bjc/utils/gui/ListParameterPanel.java index 3cdfd47..6272b5f 100644 --- a/BJC-Utils2/src/main/java/bjc/utils/gui/ListParameterPanel.java +++ b/BJC-Utils2/src/main/java/bjc/utils/gui/ListParameterPanel.java @@ -72,15 +72,14 @@ public class ListParameterPanel extends JPanel { buttonPanel.setLayout(new HLayout(3)); JButton addParam = new JButton("Add..."); + JButton editParam = new JButton("Edit..."); + JButton removeParam = new JButton("Remove..."); + addParam.addActionListener( (ev) -> ((DefaultListModel) list.getModel()) .addElement(addAct.get())); - - JButton editParam = new JButton("Edit..."); editParam.addActionListener( (ev) -> editAct.accept(list.getSelectedValue())); - - JButton removeParam = new JButton("Remove..."); removeParam.addActionListener((ev) -> removeAct .accept(((DefaultListModel) list.getModel()) .remove(list.getSelectedIndex()))); diff --git a/BJC-Utils2/src/main/java/bjc/utils/gui/SimpleDialogs.java b/BJC-Utils2/src/main/java/bjc/utils/gui/SimpleDialogs.java index ea48977..0acbd65 100644 --- a/BJC-Utils2/src/main/java/bjc/utils/gui/SimpleDialogs.java +++ b/BJC-Utils2/src/main/java/bjc/utils/gui/SimpleDialogs.java @@ -78,8 +78,9 @@ public class SimpleDialogs { JPanel buttonPane = new JPanel(); JButton okButton = new JButton("Ok"); - okButton.addActionListener(e -> jd.dispose()); JButton cancelButton = new JButton("Cancel"); + + okButton.addActionListener(e -> jd.dispose()); cancelButton.addActionListener(e -> jd.dispose()); buttonPane.add(cancelButton); diff --git a/BJC-Utils2/src/main/java/bjc/utils/gui/awt/SimpleFileDialog.java b/BJC-Utils2/src/main/java/bjc/utils/gui/awt/SimpleFileDialog.java index 2e13949..c12119f 100644 --- a/BJC-Utils2/src/main/java/bjc/utils/gui/awt/SimpleFileDialog.java +++ b/BJC-Utils2/src/main/java/bjc/utils/gui/awt/SimpleFileDialog.java @@ -16,6 +16,19 @@ import bjc.utils.gui.SimpleDialogs; * */ public class SimpleFileDialog { + /** + * Prompt the user to pick a file to open + * + * @param par + * The parent of the file picker + * @param title + * The title of the file picker + * @return The file the user picked + */ + public static File getOpenFile(Frame par, String title) { + return getOpenFile(par, title, (String[]) null); + } + /** * Prompt the user to pick a file to open * @@ -48,7 +61,7 @@ public class SimpleFileDialog { } /** - * Prompt the user to pick a file to open + * Prompt the user to pick a file to save * * @param par * The parent of the file picker @@ -56,8 +69,8 @@ public class SimpleFileDialog { * The title of the file picker * @return The file the user picked */ - public static File getOpenFile(Frame par, String title) { - return getOpenFile(par, title, (String[]) null); + public static File getSaveFile(Frame par, String title) { + return getSaveFile(par, title, (String[]) null); } /** @@ -90,17 +103,4 @@ public class SimpleFileDialog { return fd.getFiles()[0]; } - - /** - * Prompt the user to pick a file to save - * - * @param par - * The parent of the file picker - * @param title - * The title of the file picker - * @return The file the user picked - */ - public static File getSaveFile(Frame par, String title) { - return getSaveFile(par, title, (String[]) null); - } } diff --git a/BJC-Utils2/src/main/java/bjc/utils/parserutils/RuleBasedConfigReader.java b/BJC-Utils2/src/main/java/bjc/utils/parserutils/RuleBasedConfigReader.java index 1190281..d07d725 100644 --- a/BJC-Utils2/src/main/java/bjc/utils/parserutils/RuleBasedConfigReader.java +++ b/BJC-Utils2/src/main/java/bjc/utils/parserutils/RuleBasedConfigReader.java @@ -20,12 +20,12 @@ import bjc.utils.funcdata.FunctionalStringTokenizer; * type of the state object to use */ public class RuleBasedConfigReader { - private Map> pragmas; - private BiConsumer> startRule; private BiConsumer continueRule; private Consumer endRule; + private Map> pragmas; + /** * Create a new rule-based config reader * @@ -43,10 +43,15 @@ public class RuleBasedConfigReader { this.startRule = startRule; this.continueRule = continueRule; this.endRule = endRule; - + this.pragmas = new HashMap<>(); } + public void addPragma(String pragName, + BiConsumer pragAct) { + pragmas.put(pragName, pragAct); + } + public E fromStream(InputStream is, E initState) { Scanner scn = new Scanner(is); @@ -67,7 +72,7 @@ public class RuleBasedConfigReader { String nxtToken = stk.nextToken(); if (nxtToken.equals("#")) { - + // Do nothing, this is a comment } else if (nxtToken.equals("pragma")) { String tk = stk.nextToken(); @@ -87,16 +92,6 @@ public class RuleBasedConfigReader { return stat; } - public void addPragma(String pragName, - BiConsumer pragAct) { - pragmas.put(pragName, pragAct); - } - - public void setStartRule( - BiConsumer> startRule) { - this.startRule = startRule; - } - public void setContinueRule( BiConsumer continueRule) { this.continueRule = continueRule; @@ -105,4 +100,9 @@ public class RuleBasedConfigReader { public void setEndRule(Consumer endRule) { this.endRule = endRule; } + + public void setStartRule( + BiConsumer> startRule) { + this.startRule = startRule; + } } diff --git a/BJC-Utils2/src/main/java/bjc/utils/parserutils/ShuntingYard.java b/BJC-Utils2/src/main/java/bjc/utils/parserutils/ShuntingYard.java index ace636e..8a036ce 100644 --- a/BJC-Utils2/src/main/java/bjc/utils/parserutils/ShuntingYard.java +++ b/BJC-Utils2/src/main/java/bjc/utils/parserutils/ShuntingYard.java @@ -19,7 +19,7 @@ import bjc.utils.funcdata.FunctionalList; */ public class ShuntingYard { - private static enum Operator implements IPrecedent { + public static enum Operator implements IPrecedent { ADD(1), DIVIDE(4), MULTIPLY(3), SUBTRACT(2); private final int precedence; @@ -39,9 +39,6 @@ public class ShuntingYard { } } - static { - } - /** * Holds all the shuntable operations */ @@ -59,6 +56,16 @@ public class ShuntingYard { ops.put("/", Operator.DIVIDE); } + /** + * Add an operator to the list of shuntable operators + * + * @param tok + * The token representing the operator + */ + public void addOp(String tok, int i) { + this.addOp(tok, IPrecedent.newSimplePrecedent(i)); + } + /** * Add an operator to the list of shuntable operators * @@ -127,14 +134,4 @@ public class ShuntingYard { public void removeOp(String tok) { ops.remove(tok); } - - /** - * Add an operator to the list of shuntable operators - * - * @param tok - * The token representing the operator - */ - public void addOp(String tok, int i) { - this.addOp(tok, IPrecedent.newSimplePrecedent(i)); - } } \ No newline at end of file diff --git a/BJC-Utils2/src/main/java/bjc/utils/parserutils/UnknownPragmaException.java b/BJC-Utils2/src/main/java/bjc/utils/parserutils/UnknownPragmaException.java index fc97a44..88f38d4 100644 --- a/BJC-Utils2/src/main/java/bjc/utils/parserutils/UnknownPragmaException.java +++ b/BJC-Utils2/src/main/java/bjc/utils/parserutils/UnknownPragmaException.java @@ -2,11 +2,26 @@ package bjc.utils.parserutils; import java.util.InputMismatchException; +/** + * Represents a error from encountering a unknown pragma + * + * @author ben + * + */ public class UnknownPragmaException extends InputMismatchException { + /** + * Version id for serialization + */ + private static final long serialVersionUID = -4277573484926638662L; + + /** + * Create a new exception with the given cause + * + * @param m + * The cause for throwing this exception + */ public UnknownPragmaException(String m) { super(m); } - private static final long serialVersionUID = -4277573484926638662L; - } -- cgit v1.2.3