diff options
| author | bculkin2442 <bjculkin@mix.wvu.edu> | 2016-02-29 10:41:17 -0500 |
|---|---|---|
| committer | bculkin2442 <bjculkin@mix.wvu.edu> | 2016-02-29 10:41:17 -0500 |
| commit | 82951e37e10b282d9a7c89f4662990b64949c943 (patch) | |
| tree | f84770bf755c4d187ef46e137082248c2709fed9 | |
| parent | 68faea64a4b1ef23acba209ad502e4458eb16290 (diff) | |
General code cleanup
19 files changed, 288 insertions, 208 deletions
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<T> { * The transformation to apply * @return A holder with the transformed value */ - <NewT> IHolder<NewT> map(Function<T, NewT> f); + public <NewT> IHolder<NewT> map(Function<T, NewT> f); /** * Apply the given transformation to the held value. Returns the holder @@ -31,7 +31,7 @@ public interface IHolder<T> { * The transform to apply to the value * @return The holder */ - IHolder<T> transform(Function<T, T> f); + public IHolder<T> transform(Function<T, T> f); /** * Returns a raw mapped value, not contained in a GenHolder @@ -40,6 +40,6 @@ public interface IHolder<T> { * The function to use for mapping the value * @return The mapped value outside of a GenHolder */ - <E> E unwrap(Function<T, E> f); + public <E> E unwrap(Function<T, E> 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<L, R> { * The function to apply to the right value. * @return A new pair containing the two modified values. */ - <L2, R2> IPair<L2, R2> apply(Function<L, L2> lf, Function<R, R2> rf); + public <L2, R2> IPair<L2, R2> apply(Function<L, L2> lf, + Function<R, R2> rf); /** * Collapse this pair to a single value. Does not change the internal @@ -26,8 +27,14 @@ public interface IPair<L, R> { * The function to use to collapse the pair. * @return The collapsed value. */ - <E> E merge(BiFunction<L, R, E> bf); - - void doWith(BiConsumer<L, R> bc); + public <E> E merge(BiFunction<L, R, E> 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<L, R> 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<E> implements Cloneable { return true; } } + return false; } @@ -203,11 +204,7 @@ public class FunctionalList<E> implements Cloneable { public <T> FunctionalList<T> flatMap(Function<E, List<T>> f) { FunctionalList<T> 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<E> implements Cloneable { * index. */ public void forEachIndexed(BiConsumer<Integer, E> c) { - int i = 0; + GenHolder<Integer> 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<E> implements Cloneable { public FunctionalList<E> getMatching(Predicate<E> matchPred) { FunctionalList<E> fl = new FunctionalList<>(); - this.forEach((elem) -> { + wrap.forEach((elem) -> { if (matchPred.test(elem)) { fl.add(elem); } @@ -369,6 +368,10 @@ public class FunctionalList<E> 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<E> 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<T> { */ public void addNode(T dat) { nCount++; + if (root == null) { root = new TreeNode<T>(dat, null, null); } else { @@ -61,43 +63,34 @@ public class BinarySearchTree<T> { * time, but also O(N) space. */ public void balance() { - ArrayList<T> elms = new ArrayList<>(nCount); + FunctionalList<T> 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<T>(elms.get(piv), null, null); + root = new TreeNode<T>(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<T> 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 * traversals/finds. @@ -106,10 +99,20 @@ public class BinarySearchTree<T> { */ public void deleteNode(T dat) { nCount--; + root.delete(dat, comp); } /** + * Get the root of the tree. + * + * @return The root of the tree. + */ + public ITreePart<T> getRoot() { + return root; + } + + /** * Check if a node is in the tree * * @param dat 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 <T> + * The type of element stored in the walked tree */ @FunctionalInterface public interface DirectedWalkFunction<T> { @@ -16,27 +18,30 @@ public interface DirectedWalkFunction<T> { */ 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 <T> The data contained in this part of the tree. + * @param <T> + * The data contained in this part of the tree. */ public interface ITreePart<T> { /** * 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<T> comp); + public void add(T dat, Comparator<T> 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> E collapse(Function<T, E> f, BiFunction<E, E, E> bf); + public <E> E collapse(Function<T, E> f, BiFunction<E, E, E> 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<T> cmp); + public boolean contains(T data, Comparator<T> 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<T> cmp); + public void delete(T dat, Comparator<T> 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<T> ds); + public boolean directedWalk(DirectedWalkFunction<T> 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<T> c); + public boolean forEach(TreeLinearizationMethod tlm, Predicate<T> 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<T> extends TreeLeaf<T> { @Override public void delete(T dat, Comparator<T> cmp) { - directedWalk(new DirectedWalkFunction<T>() { - @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 <E> The type of grammar elements to use. + * @param <E> + * The type of grammar elements to use. */ public class RandomGrammar<E> extends WeightedGrammar<E> { @@ -21,11 +23,14 @@ public class RandomGrammar<E> extends WeightedGrammar<E> { /** * 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<E>... cases) { + public final void addCases(E rule, FunctionalList<E>... cases) { for (FunctionalList<E> cse : cases) { super.addCase(rule, 1, cse); } @@ -33,13 +38,16 @@ public class RandomGrammar<E> extends WeightedGrammar<E> { /** * 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<E>... cases) { super.addRule(rule); - + for (FunctionalList<E> cse : cases) { super.addCase(rule, 1, cse); } @@ -47,13 +55,15 @@ public class RandomGrammar<E> extends WeightedGrammar<E> { /** * 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<FunctionalList<E>> cases) { + public void makeRule(E rule, FunctionalList<FunctionalList<E>> 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<E> { */ public WeightedGrammar(Random src) { this(); + sr = src; } @@ -67,9 +68,7 @@ public class WeightedGrammar<E> { * The case being added. */ public void addCase(E rule, int prob, FunctionalList<E> cse) { - WeightedRandom<FunctionalList<E>> rn = rules.get(rule); - - rn.addProb(prob, cse); + rules.get(rule).addProb(prob, cse); } /** @@ -231,32 +230,12 @@ public class WeightedGrammar<E> { } /** - * 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<FunctionalList<E>> rule = rules.get(rName); - - FunctionalList<Pair<Integer, FunctionalList<E>>> newResults = new FunctionalList<>(); - - rule.getValues().forEach((par) -> { - FunctionalList<E> 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<E> { 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<FunctionalList<E>> rule = rules.get(rName); + + FunctionalList<Pair<Integer, FunctionalList<E>>> newResults = new FunctionalList<>(); + + rule.getValues().forEach((par) -> { + FunctionalList<E> 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<E> { 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<T> { .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<E> 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<E>) 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<E>) 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 @@ -23,6 +23,19 @@ public class SimpleFileDialog { * 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 + * + * @param par + * The parent of the file picker + * @param title + * The title of the file picker * @param extensions * The extensions to accept as valid * @return The file the user picked @@ -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<E> { - private Map<String, BiConsumer<FunctionalStringTokenizer, E>> pragmas; - private BiConsumer<FunctionalStringTokenizer, Pair<String, E>> startRule; private BiConsumer<FunctionalStringTokenizer, E> continueRule; private Consumer<E> endRule; + private Map<String, BiConsumer<FunctionalStringTokenizer, E>> pragmas; + /** * Create a new rule-based config reader * @@ -43,10 +43,15 @@ public class RuleBasedConfigReader<E> { this.startRule = startRule; this.continueRule = continueRule; this.endRule = endRule; - + this.pragmas = new HashMap<>(); } + public void addPragma(String pragName, + BiConsumer<FunctionalStringTokenizer, E> pragAct) { + pragmas.put(pragName, pragAct); + } + public E fromStream(InputStream is, E initState) { Scanner scn = new Scanner(is); @@ -67,7 +72,7 @@ public class RuleBasedConfigReader<E> { 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<E> { return stat; } - public void addPragma(String pragName, - BiConsumer<FunctionalStringTokenizer, E> pragAct) { - pragmas.put(pragName, pragAct); - } - - public void setStartRule( - BiConsumer<FunctionalStringTokenizer, Pair<String, E>> startRule) { - this.startRule = startRule; - } - public void setContinueRule( BiConsumer<FunctionalStringTokenizer, E> continueRule) { this.continueRule = continueRule; @@ -105,4 +100,9 @@ public class RuleBasedConfigReader<E> { public void setEndRule(Consumer<E> endRule) { this.endRule = endRule; } + + public void setStartRule( + BiConsumer<FunctionalStringTokenizer, Pair<String, E>> 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<E> { - 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<E> { } } - static { - } - /** * Holds all the shuntable operations */ @@ -64,6 +61,16 @@ public class ShuntingYard<E> { * * @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 + * + * @param tok + * The token representing the operator * @param prec * The precedence of the operator */ @@ -127,14 +134,4 @@ public class ShuntingYard<E> { 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; - } |
