diff options
Diffstat (limited to 'BJC-Utils2/src/main')
48 files changed, 997 insertions, 402 deletions
diff --git a/BJC-Utils2/src/main/java/bjc/utils/components/ComponentDescription.java b/BJC-Utils2/src/main/java/bjc/utils/components/ComponentDescription.java index 5923f26..3cb16b4 100644 --- a/BJC-Utils2/src/main/java/bjc/utils/components/ComponentDescription.java +++ b/BJC-Utils2/src/main/java/bjc/utils/components/ComponentDescription.java @@ -2,33 +2,39 @@ package bjc.utils.components; /** * Generic implementation of a description for a component + * * @author ben * */ public class ComponentDescription implements IDescribedComponent { /** - * The name of the component - */ - private String name; - /** * The author of the component */ - private String author; + private String author; /** * The description of the component */ - private String description; + private String description; + /** + * The name of the component + */ + private String name; /** * The version of the component */ - private int version; - + private int version; + /** * Create a new component description - * @param name The name of the component - * @param author The author of the component - * @param description The description of the component - * @param version The version of the component + * + * @param name + * The name of the component + * @param author + * The author of the component + * @param description + * The description of the component + * @param version + * The version of the component */ public ComponentDescription(String name, String author, String description, int version) { @@ -39,20 +45,20 @@ public class ComponentDescription implements IDescribedComponent { } @Override - public String getName() { - return name; - } - - @Override public String getAuthor() { return author; } - + @Override public String getDescription() { return description; } - + + @Override + public String getName() { + return name; + } + @Override public int getVersion() { return version; diff --git a/BJC-Utils2/src/main/java/bjc/utils/components/FileComponentRepository.java b/BJC-Utils2/src/main/java/bjc/utils/components/FileComponentRepository.java index 9dd881c..0d25e68 100644 --- a/BJC-Utils2/src/main/java/bjc/utils/components/FileComponentRepository.java +++ b/BJC-Utils2/src/main/java/bjc/utils/components/FileComponentRepository.java @@ -86,5 +86,4 @@ public class FileComponentRepository<E extends IDescribedComponent> public String getSource() { return "Read from directory " + sourcePath + "."; } - } diff --git a/BJC-Utils2/src/main/java/bjc/utils/components/IComponentRepository.java b/BJC-Utils2/src/main/java/bjc/utils/components/IComponentRepository.java index a326a78..43d5919 100644 --- a/BJC-Utils2/src/main/java/bjc/utils/components/IComponentRepository.java +++ b/BJC-Utils2/src/main/java/bjc/utils/components/IComponentRepository.java @@ -9,6 +9,8 @@ import bjc.utils.funcdata.FunctionalList; * * @author ben * + * @param <E> + * The type of components contained in this repository */ public interface IComponentRepository<E extends IDescribedComponent> { /** diff --git a/BJC-Utils2/src/main/java/bjc/utils/components/IDescribedComponent.java b/BJC-Utils2/src/main/java/bjc/utils/components/IDescribedComponent.java index f1d8b1c..15be70d 100644 --- a/BJC-Utils2/src/main/java/bjc/utils/components/IDescribedComponent.java +++ b/BJC-Utils2/src/main/java/bjc/utils/components/IDescribedComponent.java @@ -9,13 +9,15 @@ package bjc.utils.components; */ public interface IDescribedComponent { /** - * Get the name of this component. + * Get the author of this component * - * This is the only thing required of all components + * Providing this is optional, with "Anonymous" as the default author * - * @return The name of the component + * @return The author of the component */ - public String getName(); + public default String getAuthor() { + return "Anonymous"; + } /** * Get the description of this component @@ -30,15 +32,13 @@ public interface IDescribedComponent { } /** - * Get the author of this component + * Get the name of this component. * - * Providing this is optional, with "Anonymous" as the default author + * This is the only thing required of all components * - * @return The author of the component + * @return The name of the component */ - public default String getAuthor() { - return "Anonymous"; - } + public String getName(); /** * Get the version of this component diff --git a/BJC-Utils2/src/main/java/bjc/utils/data/GenHolder.java b/BJC-Utils2/src/main/java/bjc/utils/data/GenHolder.java index fba801b..5cf35ae 100644 --- a/BJC-Utils2/src/main/java/bjc/utils/data/GenHolder.java +++ b/BJC-Utils2/src/main/java/bjc/utils/data/GenHolder.java @@ -32,7 +32,7 @@ public class GenHolder<T> implements IHolder<T> { * Creates a new holder, with its state initialized to the provided * value * - * @param held + * @param hld * The state to initialize this holder to. */ public GenHolder(T hld) { @@ -42,11 +42,21 @@ public class GenHolder<T> implements IHolder<T> { /* * (non-Javadoc) * + * @see bjc.utils.data.IHolder#doWith(java.util.function.Consumer) + */ + @Override + public void doWith(Consumer<T> f) { + f.accept(held); + } + + /* + * (non-Javadoc) + * * @see bjc.utils.data.IHolder#map(java.util.function.Function) */ @Override public <NewT> IHolder<NewT> map(Function<T, NewT> f) { - return new GenHolder<NewT>(f.apply(held)); + return new GenHolder<>(f.apply(held)); } /* @@ -70,13 +80,4 @@ public class GenHolder<T> implements IHolder<T> { public <E> E unwrap(Function<T, E> f) { return f.apply(held); } - - /* - * (non-Javadoc) - * - * @see bjc.utils.data.IHolder#doWith(java.util.function.Consumer) - */ - public void doWith(Consumer<T> f) { - f.accept(held); - } } 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 ddcb2f6..3675842 100644 --- a/BJC-Utils2/src/main/java/bjc/utils/data/IHolder.java +++ b/BJC-Utils2/src/main/java/bjc/utils/data/IHolder.java @@ -15,8 +15,19 @@ import java.util.function.Function; public interface IHolder<T> { /** + * Call a provided function with the value being held + * + * @param f + * The function to call + */ + public void doWith(Consumer<T> f); + + /** * Return the result of applying the given transformation to the held - * value Doesn't change the held value + * value. Doesn't change the held value. + * + * @param <NewT> + * The new type of the held value * * @param f * The transformation to apply @@ -37,17 +48,12 @@ public interface IHolder<T> { /** * Returns a raw mapped value, not contained in a GenHolder * + * @param <E> + * The type of the value that is the end result + * * @param f * The function to use for mapping the value * @return The mapped value outside of a GenHolder */ public <E> E unwrap(Function<T, E> f); - - /** - * Call a provided function with the value being held - * - * @param f - * The function to call - */ - public void doWith(Consumer<T> 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 fcf2b05..9923099 100644 --- a/BJC-Utils2/src/main/java/bjc/utils/data/IPair.java +++ b/BJC-Utils2/src/main/java/bjc/utils/data/IPair.java @@ -4,12 +4,27 @@ import java.util.function.BiConsumer; import java.util.function.BiFunction; import java.util.function.Function; +/** + * An interface representing a pair of values + * + * @author ben + * + * @param <L> + * The type stored in the left side of the pair + * @param <R> + * The type stored in the right side of the pair + */ public interface IPair<L, R> { /** * Create a new pair by applying the given functions to the left/right. * Does not change the internal contents of this pair. * + * @param <L2> + * The new left type of the pair + * @param <R2> + * The new right type of the pair + * * @param lf * The function to apply to the left value. * @param rf @@ -20,21 +35,22 @@ public interface IPair<L, R> { Function<R, R2> rf); /** + * 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); + + /** * Collapse this pair to a single value. Does not change the internal * contents of this pair. + * @param <E> The resulting type after merging * * @param bf * The function to use to collapse the pair. * @return The collapsed value. */ 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 1e00a59..a9ae0bc 100644 --- a/BJC-Utils2/src/main/java/bjc/utils/data/IPrecedent.java +++ b/BJC-Utils2/src/main/java/bjc/utils/data/IPrecedent.java @@ -6,15 +6,9 @@ package bjc.utils.data; * @author ben * */ +@FunctionalInterface public interface IPrecedent { /** - * Get the precedence of the attached object - * - * @return The precedence of the attached object - */ - public int getPrecedence(); - - /** * Create a new object with set precedence * * @param prec @@ -24,4 +18,11 @@ public interface IPrecedent { public static IPrecedent newSimplePrecedent(int prec) { return () -> prec; } + + /** + * Get the precedence of the attached object + * + * @return The precedence of the attached object + */ + public int getPrecedence(); }
\ No newline at end of file diff --git a/BJC-Utils2/src/main/java/bjc/utils/data/Pair.java b/BJC-Utils2/src/main/java/bjc/utils/data/Pair.java index 4e7f6ae..4a8fbc3 100644 --- a/BJC-Utils2/src/main/java/bjc/utils/data/Pair.java +++ b/BJC-Utils2/src/main/java/bjc/utils/data/Pair.java @@ -7,6 +7,8 @@ import java.util.function.Function; /** * Holds a pair of values of two different types. * + * Is an eager variant of {@link IPair} + * * @author ben * * @param <L> @@ -45,28 +47,35 @@ public class Pair<L, R> implements IPair<L, R> { r = right; } - /* (non-Javadoc) - * @see bjc.utils.data.IPair#apply(java.util.function.Function, java.util.function.Function) + /* + * (non-Javadoc) + * + * @see bjc.utils.data.IPair#apply(java.util.function.Function, + * java.util.function.Function) */ @Override public <L2, R2> IPair<L2, R2> apply(Function<L, L2> lf, Function<R, R2> rf) { - return new Pair<L2, R2>(lf.apply(l), rf.apply(r)); + return new Pair<>(lf.apply(l), rf.apply(r)); } - /* (non-Javadoc) - * @see bjc.utils.data.IPair#merge(java.util.function.BiFunction) - */ - @Override - public <E> E merge(BiFunction<L, R, E> bf) { - return bf.apply(l, r); - } - - /* (non-Javadoc) + /* + * (non-Javadoc) + * * @see bjc.utils.data.IPair#doWith(java.util.function.BiConsumer) */ @Override public void doWith(BiConsumer<L, R> bc) { bc.accept(l, r); } + + /* + * (non-Javadoc) + * + * @see bjc.utils.data.IPair#merge(java.util.function.BiFunction) + */ + @Override + public <E> E merge(BiFunction<L, R, E> bf) { + return bf.apply(l, r); + } } diff --git a/BJC-Utils2/src/main/java/bjc/utils/data/lazy/LazyHolder.java b/BJC-Utils2/src/main/java/bjc/utils/data/lazy/LazyHolder.java index 43dec86..6321cc2 100644 --- a/BJC-Utils2/src/main/java/bjc/utils/data/lazy/LazyHolder.java +++ b/BJC-Utils2/src/main/java/bjc/utils/data/lazy/LazyHolder.java @@ -21,9 +21,9 @@ import bjc.utils.funcdata.FunctionalList; */ public class LazyHolder<T> implements IHolder<T> { /** - * The source for a value held by this lazy holder + * List of queued actions to be performed on realized values */ - private Supplier<T> heldSrc; + private FunctionalList<Function<T, T>> actions; /** * The value internally held by this lazy holder @@ -31,9 +31,9 @@ public class LazyHolder<T> implements IHolder<T> { private T held; /** - * List of queued actions to be performed on realized values + * The source for a value held by this lazy holder */ - private FunctionalList<Function<T, T>> actions; + private Supplier<T> heldSrc; /** * Create a new lazy holder with the given supplier @@ -58,8 +58,17 @@ public class LazyHolder<T> implements IHolder<T> { } @Override + public void doWith(Consumer<T> f) { + transform((val) -> { + f.accept(val); + + return val; + }); + } + + @Override public <NewT> IHolder<NewT> map(Function<T, NewT> f) { - return new LazyHolder<NewT>(() -> { + return new LazyHolder<>(() -> { if (held == null) { return actions.reduceAux(heldSrc.get(), Function<T, T>::apply, f::apply); @@ -89,13 +98,4 @@ public class LazyHolder<T> implements IHolder<T> { return f.apply(held); } - @Override - public void doWith(Consumer<T> f) { - transform((val) -> { - f.accept(val); - - return val; - }); - } - } diff --git a/BJC-Utils2/src/main/java/bjc/utils/data/lazy/LazyPair.java b/BJC-Utils2/src/main/java/bjc/utils/data/lazy/LazyPair.java index 2aab4ce..bd02b52 100644 --- a/BJC-Utils2/src/main/java/bjc/utils/data/lazy/LazyPair.java +++ b/BJC-Utils2/src/main/java/bjc/utils/data/lazy/LazyPair.java @@ -6,20 +6,41 @@ import java.util.function.Function; import bjc.utils.data.IPair; +/** + * A lazy holder of two values + * + * Lazy variant of {@link IPair} + * + * @author ben + * + * @param <L> + * The type of value stored on the left side of the pair + * @param <R> + * The type of value stored on the right side of the pair + */ public class LazyPair<L, R> implements IPair<L, R> { + /** + * The backing store for this pair + */ protected LazyHolder<IPair<L, R>> del; + /* + * (non-Javadoc) + * + * @see bjc.utils.data.IPair#apply(java.util.function.Function, + * java.util.function.Function) + */ @Override public <L2, R2> IPair<L2, R2> apply(Function<L, L2> lf, Function<R, R2> rf) { return del.unwrap((par) -> par.apply(lf, rf)); } - @Override - public <E> E merge(BiFunction<L, R, E> bf) { - return del.unwrap((par) -> par.merge(bf)); - } - + /* + * (non-Javadoc) + * + * @see bjc.utils.data.IPair#doWith(java.util.function.BiConsumer) + */ @Override public void doWith(BiConsumer<L, R> bc) { del.doWith((par) -> { @@ -27,4 +48,13 @@ public class LazyPair<L, R> implements IPair<L, R> { }); } + /* + * (non-Javadoc) + * + * @see bjc.utils.data.IPair#merge(java.util.function.BiFunction) + */ + @Override + public <E> E merge(BiFunction<L, R, E> bf) { + return del.unwrap((par) -> par.merge(bf)); + } } diff --git a/BJC-Utils2/src/main/java/bjc/utils/dice/BindingDiceExpression.java b/BJC-Utils2/src/main/java/bjc/utils/dice/BindingDiceExpression.java index a715074..9ecce97 100644 --- a/BJC-Utils2/src/main/java/bjc/utils/dice/BindingDiceExpression.java +++ b/BJC-Utils2/src/main/java/bjc/utils/dice/BindingDiceExpression.java @@ -10,10 +10,34 @@ import java.util.Map; * */ public class BindingDiceExpression implements IDiceExpression { - private String name; + /** + * The expression being bound to a name + */ private IDiceExpression exp; /** + * The name to bind the expression to + */ + private String name; + + /** + * Create a new dice expression binder from two expressions and an + * enviroment + * + * @param left + * The left side expression to get a name from. Must be a + * ReferenceDiceExpression + * @param right + * The right side to bind to the name + * @param env + * The enviroment to bind into + */ + public BindingDiceExpression(IDiceExpression left, + IDiceExpression right, Map<String, IDiceExpression> env) { + this(((ReferenceDiceExpression) left).getName(), right, env); + } + + /** * Create a new dice expression binder * * @param name @@ -31,16 +55,21 @@ public class BindingDiceExpression implements IDiceExpression { env.put(name, exp); } - public BindingDiceExpression(IDiceExpression left, - IDiceExpression right, Map<String, IDiceExpression> env) { - this(((ReferenceDiceExpression) left).getName(), right, env); - } - + /* + * (non-Javadoc) + * + * @see bjc.utils.dice.IDiceExpression#roll() + */ @Override public int roll() { return exp.roll(); } + /* + * (non-Javadoc) + * + * @see java.lang.Object#toString() + */ @Override public String toString() { return "assign[n=" + name + ", exp=" + exp.toString() + "]"; diff --git a/BJC-Utils2/src/main/java/bjc/utils/dice/ComplexDice.java b/BJC-Utils2/src/main/java/bjc/utils/dice/ComplexDice.java index 98a510a..226f9fd 100644 --- a/BJC-Utils2/src/main/java/bjc/utils/dice/ComplexDice.java +++ b/BJC-Utils2/src/main/java/bjc/utils/dice/ComplexDice.java @@ -9,6 +9,37 @@ package bjc.utils.dice; */ public class ComplexDice implements IDiceExpression { /** + * Create a dice from a string expression + * + * @param dice + * The string to parse the dice from + * @return A dice group parsed from the string + */ + public static ComplexDice fromString(String dice) { + /* + * Split it on the dice type marker + */ + String[] strangs = dice.split("d"); + + try { + /* + * Create the actual dice + */ + return new ComplexDice( + new ScalarDie(Integer.parseInt(strangs[0])), + new Die(Integer.parseInt(strangs[1]))); + } catch (NumberFormatException nfex) { + /* + * Tell the user the expression is invalid + */ + throw new IllegalArgumentException( + "Attempted to create a dice using something that's not" + + " an integer: " + strangs[0] + " and " + + strangs[1] + " are likely culprits."); + } + } + + /** * The die being rolled */ private IDiceExpression die; @@ -34,7 +65,7 @@ public class ComplexDice implements IDiceExpression { /** * Create a new collection of dice * - * @param nDce + * @param nSides * The number of dice in the collection * @param de * The type of dice the collection is composed of @@ -44,6 +75,11 @@ public class ComplexDice implements IDiceExpression { die = new Die(de); } + /* + * (non-Javadoc) + * + * @see bjc.utils.dice.IDiceExpression#roll() + */ @Override public int roll() { int res = 0; @@ -60,37 +96,11 @@ public class ComplexDice implements IDiceExpression { return res; } - /** - * Create a dice from a string expression + /* + * (non-Javadoc) * - * @param dice - * The string to parse the dice from - * @return A dice group parsed from the string + * @see java.lang.Object#toString() */ - public static ComplexDice fromString(String dice) { - /* - * Split it on the dice type marker - */ - String[] strangs = dice.split("d"); - - try { - /* - * Create the actual dice - */ - return new ComplexDice( - new ScalarDie(Integer.parseInt(strangs[0])), - new Die(Integer.parseInt(strangs[1]))); - } catch (NumberFormatException nfex) { - /* - * Tell the user the expression is invalid - */ - throw new IllegalArgumentException( - "Attempted to create a dice using something that's not" - + " an integer: " + strangs[0] + " and " - + strangs[1] + " are likely culprits."); - } - } - @Override public String toString() { if (nDice instanceof ScalarDie && die instanceof Die) { diff --git a/BJC-Utils2/src/main/java/bjc/utils/dice/CompoundDice.java b/BJC-Utils2/src/main/java/bjc/utils/dice/CompoundDice.java index 4e2e9f3..3393711 100644 --- a/BJC-Utils2/src/main/java/bjc/utils/dice/CompoundDice.java +++ b/BJC-Utils2/src/main/java/bjc/utils/dice/CompoundDice.java @@ -14,6 +14,7 @@ public class CompoundDice implements IDiceExpression { * The left die of the expression */ private IDiceExpression l; + /** * The right die of the expression */ @@ -32,14 +33,33 @@ public class CompoundDice implements IDiceExpression { this.r = r; } + /** + * Create a new compound dice from two dice strings + * + * @param l + * The left side dice + * @param r + * The right side dice + */ public CompoundDice(String l, String r) { this(ComplexDice.fromString(l), ComplexDice.fromString(r)); } + /** + * Create a new compound dice from an array of dice strings + * + * @param exps + * An array of dice strings + */ public CompoundDice(String[] exps) { this(exps[0], exps[1]); } + /* + * (non-Javadoc) + * + * @see bjc.utils.dice.IDiceExpression#roll() + */ @Override public int roll() { /* @@ -48,6 +68,11 @@ public class CompoundDice implements IDiceExpression { return Integer.parseInt(l.roll() + "" + r.roll()); } + /* + * (non-Javadoc) + * + * @see java.lang.Object#toString() + */ @Override public String toString() { return "compound[l=" + l.toString() + ", r=" + r.toString() + "]"; diff --git a/BJC-Utils2/src/main/java/bjc/utils/dice/CompoundDiceExpression.java b/BJC-Utils2/src/main/java/bjc/utils/dice/CompoundDiceExpression.java index 12238c8..41b1df2 100644 --- a/BJC-Utils2/src/main/java/bjc/utils/dice/CompoundDiceExpression.java +++ b/BJC-Utils2/src/main/java/bjc/utils/dice/CompoundDiceExpression.java @@ -39,6 +39,11 @@ public class CompoundDiceExpression implements IDiceExpression { this.det = det; } + /* + * (non-Javadoc) + * + * @see bjc.utils.dice.IDiceExpression#roll() + */ @Override public int roll() { /* @@ -65,6 +70,11 @@ public class CompoundDiceExpression implements IDiceExpression { } } + /* + * (non-Javadoc) + * + * @see java.lang.Object#toString() + */ @Override public String toString() { return "dice-exp[type=" + det + ", l=" + left.toString() + ", r=" diff --git a/BJC-Utils2/src/main/java/bjc/utils/dice/DiceExpressionParser.java b/BJC-Utils2/src/main/java/bjc/utils/dice/DiceExpressionParser.java index a7d1f8c..4113be4 100644 --- a/BJC-Utils2/src/main/java/bjc/utils/dice/DiceExpressionParser.java +++ b/BJC-Utils2/src/main/java/bjc/utils/dice/DiceExpressionParser.java @@ -21,6 +21,8 @@ public class DiceExpressionParser { * * @param exp * The string to parse an expression from + * @param env + * The enviroment to use when parsing expressions * @return The parsed dice expression */ public IDiceExpression parse(String exp, @@ -48,8 +50,8 @@ public class DiceExpressionParser { /* * Shunt the expression to postfix form */ - FunctionalList<String> ls = yard.postfix(fst.toList(s -> s), - s -> s); + FunctionalList<String> ls = + yard.postfix(fst.toList(s -> s), s -> s); /* * Create a stack for building an expression from parts 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 2259308..d719ae8 100644 --- a/BJC-Utils2/src/main/java/bjc/utils/dice/DiceExpressionType.java +++ b/BJC-Utils2/src/main/java/bjc/utils/dice/DiceExpressionType.java @@ -1,11 +1,35 @@ package bjc.utils.dice; -/* +/** * Enumeration for basic dice expression operators */ public enum DiceExpressionType { - ADD, DIVIDE, MULTIPLY, SUBTRACT; + /** + * Add two expressions + */ + ADD, + /** + * Divide two expressions + */ + DIVIDE, + + /** + * Multiply two expressions + */ + MULTIPLY, + + /** + * Subtract two expressions + */ + SUBTRACT; + + /* + * (non-Javadoc) + * + * @see java.lang.Enum#toString() + */ + @Override public String toString() { switch (this) { case ADD: diff --git a/BJC-Utils2/src/main/java/bjc/utils/dice/Die.java b/BJC-Utils2/src/main/java/bjc/utils/dice/Die.java index 9575df5..0bd6964 100644 --- a/BJC-Utils2/src/main/java/bjc/utils/dice/Die.java +++ b/BJC-Utils2/src/main/java/bjc/utils/dice/Die.java @@ -29,15 +29,19 @@ public class Die implements IDiceExpression { this.nSides = nSides; } - /** - * Roll this dice once - * - * @return The result of rolling the dice + /* + * (non-Javadoc) + * @see bjc.utils.dice.IDiceExpression#roll() */ + @Override public int roll() { return rng.nextInt(nSides) + 1; } + /* + * (non-Javadoc) + * @see java.lang.Object#toString() + */ @Override public String toString() { return "d" + nSides; diff --git a/BJC-Utils2/src/main/java/bjc/utils/dice/IDiceExpression.java b/BJC-Utils2/src/main/java/bjc/utils/dice/IDiceExpression.java index 4bd0973..5ead9ad 100644 --- a/BJC-Utils2/src/main/java/bjc/utils/dice/IDiceExpression.java +++ b/BJC-Utils2/src/main/java/bjc/utils/dice/IDiceExpression.java @@ -6,9 +6,11 @@ package bjc.utils.dice; * @author ben * */ +@FunctionalInterface public interface IDiceExpression { /** * Roll the dice once + * * @return The result of rowing the dice */ public int roll(); diff --git a/BJC-Utils2/src/main/java/bjc/utils/dice/PolyhedralDice.java b/BJC-Utils2/src/main/java/bjc/utils/dice/PolyhedralDice.java index 511fd99..314d47b 100644 --- a/BJC-Utils2/src/main/java/bjc/utils/dice/PolyhedralDice.java +++ b/BJC-Utils2/src/main/java/bjc/utils/dice/PolyhedralDice.java @@ -1,7 +1,7 @@ package bjc.utils.dice; /** - * Class that produces common polyhedral dice + * Utility class that produces common polyhedral dice * * @author ben * @@ -52,33 +52,33 @@ public class PolyhedralDice { } /** - * Produce the specified number of 10-sided dice + * Produce the specified number of 4-sided dice * * @param nDice - * The number of ten-sided dice to produce - * @return A group of ten-sided dice of the specified size + * The number of four-sided dice to produce + * @return A group of four-sided dice of the specified size */ public static IDiceExpression d4(int nDice) { return new ComplexDice(nDice, 4); } /** - * Produce the specified number of 10-sided dice + * Produce the specified number of 6-sided dice * * @param nDice - * The number of ten-sided dice to produce - * @return A group of ten-sided dice of the specified size + * The number of six-sided dice to produce + * @return A group of six-sided dice of the specified size */ public static IDiceExpression d6(int nDice) { return new ComplexDice(nDice, 6); } /** - * Produce the specified number of 10-sided dice + * Produce the specified number of 8-sided dice * * @param nDice - * The number of ten-sided dice to produce - * @return A group of ten-sided dice of the specified size + * The number of eight-sided dice to produce + * @return A group of eight-sided dice of the specified size */ public static IDiceExpression d8(int nDice) { return new ComplexDice(nDice, 8); diff --git a/BJC-Utils2/src/main/java/bjc/utils/dice/ReferenceDiceExpression.java b/BJC-Utils2/src/main/java/bjc/utils/dice/ReferenceDiceExpression.java index d8062da..d38e0f9 100644 --- a/BJC-Utils2/src/main/java/bjc/utils/dice/ReferenceDiceExpression.java +++ b/BJC-Utils2/src/main/java/bjc/utils/dice/ReferenceDiceExpression.java @@ -11,14 +11,14 @@ import java.util.Map; */ public class ReferenceDiceExpression implements IDiceExpression { /** - * The name of the bound variable + * The enviroment to do variable dereferencing against */ - private String name; + private Map<String, IDiceExpression> env; /** - * The enviroment to do variable dereferencing against + * The name of the bound variable */ - private Map<String, IDiceExpression> env; + private String name; /** * Create a new reference dice expression referring to the given name @@ -35,11 +35,30 @@ public class ReferenceDiceExpression implements IDiceExpression { this.env = env; } + /** + * Get the name of the referenced variable + * + * @return the name of the referenced variable + */ + public String getName() { + return name; + } + + /* + * (non-Javadoc) + * + * @see bjc.utils.dice.IDiceExpression#roll() + */ @Override public int roll() { return env.get(name).roll(); } + /* + * (non-Javadoc) + * + * @see java.lang.Object#toString() + */ @Override public String toString() { if (env.containsKey(name)) { @@ -48,13 +67,4 @@ public class ReferenceDiceExpression implements IDiceExpression { return name; } } - - /** - * Get the name of the referenced variable - * - * @return the name of the referenced variable - */ - public String getName() { - return name; - } } diff --git a/BJC-Utils2/src/main/java/bjc/utils/dice/ScalarDiceExpression.java b/BJC-Utils2/src/main/java/bjc/utils/dice/ScalarDiceExpression.java index 2b174c8..267e6ef 100644 --- a/BJC-Utils2/src/main/java/bjc/utils/dice/ScalarDiceExpression.java +++ b/BJC-Utils2/src/main/java/bjc/utils/dice/ScalarDiceExpression.java @@ -11,6 +11,7 @@ public class ScalarDiceExpression implements IDiceExpression { * The operation to combine with */ private DiceExpressionType det; + /** * The expression to be combined */ @@ -38,6 +39,10 @@ public class ScalarDiceExpression implements IDiceExpression { det = dt; } + /* + * (non-Javadoc) + * @see bjc.utils.dice.IDiceExpression#roll() + */ @Override public int roll() { switch (det) { @@ -56,6 +61,10 @@ public class ScalarDiceExpression implements IDiceExpression { } } + /* + * (non-Javadoc) + * @see java.lang.Object#toString() + */ @Override public String toString() { return "scalar-exp[type=" + det + ", l=" + scalar + ", r=" diff --git a/BJC-Utils2/src/main/java/bjc/utils/dice/ScalarDie.java b/BJC-Utils2/src/main/java/bjc/utils/dice/ScalarDie.java index bef68e1..4ed99b9 100644 --- a/BJC-Utils2/src/main/java/bjc/utils/dice/ScalarDie.java +++ b/BJC-Utils2/src/main/java/bjc/utils/dice/ScalarDie.java @@ -12,11 +12,6 @@ public class ScalarDie implements IDiceExpression { */ private int num; - @Override - public int roll() { - return num; - } - /** * Create a dice with the specified number * @@ -27,6 +22,21 @@ public class ScalarDie implements IDiceExpression { this.num = num; } + /* + * (non-Javadoc) + * + * @see bjc.utils.dice.IDiceExpression#roll() + */ + @Override + public int roll() { + return num; + } + + /* + * (non-Javadoc) + * + * @see java.lang.Object#toString() + */ @Override public String toString() { return Integer.toString(num); diff --git a/BJC-Utils2/src/main/java/bjc/utils/dice/ast/DiceASTExpression.java b/BJC-Utils2/src/main/java/bjc/utils/dice/ast/DiceASTExpression.java index 3b81888..aceeed0 100644 --- a/BJC-Utils2/src/main/java/bjc/utils/dice/ast/DiceASTExpression.java +++ b/BJC-Utils2/src/main/java/bjc/utils/dice/ast/DiceASTExpression.java @@ -12,48 +12,22 @@ import bjc.utils.dice.CompoundDice; import bjc.utils.dice.IDiceExpression; import bjc.utils.parserutils.AST; +/** + * An implementation of {@link IDiceExpression} backed by an AST of + * {@link IDiceASTNode}s + * + * @author ben + * + */ public class DiceASTExpression implements IDiceExpression { - private AST<IDiceASTNode> ast; - private Map<String, DiceASTExpression> env; - - public DiceASTExpression(AST<IDiceASTNode> ast, - Map<String, DiceASTExpression> env) { - this.ast = ast; - this.env = env; - } - - private Pair<Integer, AST<IDiceASTNode>> evalLeaf(IDiceASTNode tokn) { - if (tokn instanceof VariableDiceNode) { - String varName = ((VariableDiceNode) tokn).getVariable(); - - if (env.containsKey(varName)) { - return new Pair<>(env.get(varName).roll(), new AST<>(tokn)); - } else { - // Handle special case for defining variables - return new Pair<>(0, new AST<>(tokn)); - } - } else { - LiteralDiceNode lnod = (LiteralDiceNode) tokn; - String dat = lnod.getData(); - - if (StringUtils.countMatches(dat, 'c') == 1 - && !dat.equalsIgnoreCase("c")) { - String[] strangs = dat.split("c"); - return new Pair<>(new CompoundDice(strangs).roll(), - new AST<>(tokn)); - } else if (StringUtils.countMatches(dat, 'd') == 1 - && !dat.equalsIgnoreCase("d")) { - /* - * Handle dice groups - */ - return new Pair<>(ComplexDice.fromString(dat).roll(), - new AST<>(tokn)); - } else { - return new Pair<>(Integer.parseInt(dat), new AST<>(tokn)); - } - } - } + /** + * Build the map of operations to use when collapsing the AST + * + * @param env + * The enviroment to evaluate bindings and such against + * @return The operations to use when collapsing the AST + */ private static Map<IDiceASTNode, BinaryOperator<Pair<Integer, AST<IDiceASTNode>>>> buildOperations(Map<String, DiceASTExpression> env) { @@ -121,6 +95,83 @@ public class DiceASTExpression implements IDiceExpression { return opCollapsers; } + /** + * The AST this expression will evaluate + */ + private AST<IDiceASTNode> ast; + + /** + * The enviroment to evaluate bindings and such against + */ + private Map<String, DiceASTExpression> env; + + /** + * Create a new dice expression backed by an AST + * + * @param ast + * The AST backing this expression + * @param env + * The enviroment to evaluate bindings against + */ + public DiceASTExpression(AST<IDiceASTNode> ast, + Map<String, DiceASTExpression> env) { + this.ast = ast; + this.env = env; + } + + /** + * Expand a leaf AST token into a pair for evaluation + * + * @param tokn + * The token to evaluate + * @return A pair consisting of the token's value and the AST it + * represents + */ + private Pair<Integer, AST<IDiceASTNode>> evalLeaf(IDiceASTNode tokn) { + if (tokn instanceof VariableDiceNode) { + String varName = ((VariableDiceNode) tokn).getVariable(); + + if (env.containsKey(varName)) { + return new Pair<>(env.get(varName).roll(), + new AST<>(tokn)); + } else { + // Handle special case for defining variables + return new Pair<>(0, new AST<>(tokn)); + } + } else { + LiteralDiceNode lnod = (LiteralDiceNode) tokn; + String dat = lnod.getData(); + + if (StringUtils.countMatches(dat, 'c') == 1 + && !dat.equalsIgnoreCase("c")) { + String[] strangs = dat.split("c"); + return new Pair<>(new CompoundDice(strangs).roll(), + new AST<>(tokn)); + } else if (StringUtils.countMatches(dat, 'd') == 1 + && !dat.equalsIgnoreCase("d")) { + /* + * Handle dice groups + */ + return new Pair<>(ComplexDice.fromString(dat).roll(), + new AST<>(tokn)); + } else { + return new Pair<>(Integer.parseInt(dat), new AST<>(tokn)); + } + } + } + + /** + * Get the AST bound to this expression + * @return the ast + */ + public AST<IDiceASTNode> getAst() { + return ast; + } + + /* + * (non-Javadoc) + * @see bjc.utils.dice.IDiceExpression#roll() + */ @Override public int roll() { Map<IDiceASTNode, BinaryOperator<Pair<Integer, AST<IDiceASTNode>>>> operations = @@ -130,15 +181,12 @@ public class DiceASTExpression implements IDiceExpression { (r) -> r.merge((left, right) -> left)); } + /* + * (non-Javadoc) + * @see java.lang.Object#toString() + */ @Override public String toString() { return ast.toString(); } - - /** - * @return the ast - */ - public AST<IDiceASTNode> getAst() { - return ast; - } } diff --git a/BJC-Utils2/src/main/java/bjc/utils/dice/ast/DiceASTFlattener.java b/BJC-Utils2/src/main/java/bjc/utils/dice/ast/DiceASTFlattener.java index da402c3..70465a5 100644 --- a/BJC-Utils2/src/main/java/bjc/utils/dice/ast/DiceASTFlattener.java +++ b/BJC-Utils2/src/main/java/bjc/utils/dice/ast/DiceASTFlattener.java @@ -16,25 +16,25 @@ import bjc.utils.dice.ReferenceDiceExpression; import bjc.utils.dice.ScalarDie; import bjc.utils.parserutils.AST; +/** + * Flatten an {@link AST} of {@link IDiceASTNode} into a + * {@link IDiceExpression} + * + * @author ben + * + */ public class DiceASTFlattener { - public static IDiceExpression flatten(AST<IDiceASTNode> ast, - Map<String, IDiceExpression> env) { - Map<IDiceASTNode, BinaryOperator<IDiceExpression>> opCollapsers = buildOperations( - env); - - return ast.collapse((nod) -> { - if (nod instanceof LiteralDiceNode) { - return expFromLiteral((LiteralDiceNode) nod); - } else { - return new ReferenceDiceExpression( - ((VariableDiceNode) nod).getVariable(), env); - } - } , opCollapsers::get, (r) -> r); - } - - private static Map<IDiceASTNode, BinaryOperator<IDiceExpression>> buildOperations( - Map<String, IDiceExpression> env) { - Map<IDiceASTNode, BinaryOperator<IDiceExpression>> opCollapsers = new HashMap<>(); + /** + * Build the operations to use for tree flattening + * + * @param env + * The enviroment the tree will be flattened against + * @return The operations needed for tree flattening + */ + private static Map<IDiceASTNode, BinaryOperator<IDiceExpression>> + buildOperations(Map<String, IDiceExpression> env) { + Map<IDiceASTNode, BinaryOperator<IDiceExpression>> opCollapsers = + new HashMap<>(); opCollapsers.put(OperatorDiceNode.ADD, (left, right) -> { return new CompoundDiceExpression(right, left, DiceExpressionType.ADD); @@ -64,6 +64,13 @@ public class DiceASTFlattener { return opCollapsers; } + /** + * Create a dice expression from a literal token + * + * @param tok + * The token to convert to an expression + * @return The dice expression represented by the token + */ private static IDiceExpression expFromLiteral(LiteralDiceNode tok) { String data = tok.getData(); @@ -80,4 +87,28 @@ public class DiceASTFlattener { return new ScalarDie(Integer.parseInt(data)); } } + + /** + * Flatten a AST into a dice expression + * + * @param ast + * The AST to flatten + * @param env + * The enviroment to flatten against + * @return The AST, flattened into a dice expression + */ + public static IDiceExpression flatten(AST<IDiceASTNode> ast, + Map<String, IDiceExpression> env) { + Map<IDiceASTNode, BinaryOperator<IDiceExpression>> opCollapsers = + buildOperations(env); + + return ast.collapse((nod) -> { + if (nod instanceof LiteralDiceNode) { + return expFromLiteral((LiteralDiceNode) nod); + } else { + return new ReferenceDiceExpression( + ((VariableDiceNode) nod).getVariable(), env); + } + } , opCollapsers::get, (r) -> r); + } } diff --git a/BJC-Utils2/src/main/java/bjc/utils/dice/ast/DiceASTFreezer.java b/BJC-Utils2/src/main/java/bjc/utils/dice/ast/DiceASTFreezer.java index 04cc99b..efe37c0 100644 --- a/BJC-Utils2/src/main/java/bjc/utils/dice/ast/DiceASTFreezer.java +++ b/BJC-Utils2/src/main/java/bjc/utils/dice/ast/DiceASTFreezer.java @@ -4,7 +4,52 @@ import java.util.Map; import bjc.utils.parserutils.AST; +/** + * Freeze references in a dice AST, replacing variable references with what + * the variables refer to + * + * @author ben + * + */ public class DiceASTFreezer { + /** + * Expand a reference + * + * @param vnode + * The node containing the reference to expand + * @param env + * The enviroment to expand against + * @return The expanded reference + */ + private static AST<IDiceASTNode> expandNode(VariableDiceNode vnode, + Map<String, AST<IDiceASTNode>> env) { + return env.get(vnode.getVariable()); + } + + /** + * Expand a reference + * + * @param vnode + * The node containing the reference to expand + * @param env + * The enviroment to expand against + * @return The expanded reference + */ + private static AST<IDiceASTNode> expandNode2(VariableDiceNode vnode, + Map<String, DiceASTExpression> env) { + return env.get(vnode.getVariable()).getAst(); + } + + /** + * Freeze the references in an AST + * + * @param tree + * The tree to freeze references in + * @param env + * The enviroment to get reference values from + * @return The tree with references frozen + */ + @SuppressWarnings("unused") public static AST<IDiceASTNode> freezeAST(AST<IDiceASTNode> tree, Map<String, AST<IDiceASTNode>> env) { return tree.collapse((nod) -> { @@ -20,6 +65,16 @@ public class DiceASTFreezer { } , (r) -> r); } + /** + * Freeze the references in an expression backed by an AST + * + * @param tree + * The tree-backed expression to freeze references in + * @param env + * The enviroment to get reference values from + * @return The tree with references frozen + */ + @SuppressWarnings("unused") public static AST<IDiceASTNode> freezeAST(DiceASTExpression tree, Map<String, DiceASTExpression> env) { return tree.getAst().collapse((nod) -> { @@ -34,14 +89,4 @@ public class DiceASTFreezer { return new AST<IDiceASTNode>(op, left, right); } , (r) -> r); } - - private static AST<IDiceASTNode> expandNode(VariableDiceNode vnode, - Map<String, AST<IDiceASTNode>> env) { - return env.get(vnode.getVariable()); - } - - private static AST<IDiceASTNode> expandNode2(VariableDiceNode vnode, - Map<String, DiceASTExpression> env) { - return env.get(vnode.getVariable()).getAst(); - } } diff --git a/BJC-Utils2/src/main/java/bjc/utils/dice/ast/DiceASTParser.java b/BJC-Utils2/src/main/java/bjc/utils/dice/ast/DiceASTParser.java index d56ad0e..b25f5b4 100644 --- a/BJC-Utils2/src/main/java/bjc/utils/dice/ast/DiceASTParser.java +++ b/BJC-Utils2/src/main/java/bjc/utils/dice/ast/DiceASTParser.java @@ -8,7 +8,16 @@ import bjc.utils.parserutils.AST; import bjc.utils.parserutils.ShuntingYard; import bjc.utils.parserutils.TreeConstructor; +/** + * Create an AST from a string expression + * + * @author ben + * + */ public class DiceASTParser { + /** + * The yard to use for shunting expressions + */ private static ShuntingYard<String> yard; static { @@ -22,14 +31,21 @@ public class DiceASTParser { // expression } + /** + * Build an AST from a string expression + * + * @param exp + * The string to build from + * @return An AST built from the passed in string + */ public AST<IDiceASTNode> buildAST(String exp) { - FunctionalList<String> tokens = FunctionalStringTokenizer - .fromString(exp).toList((s) -> s); + FunctionalList<String> tokens = + FunctionalStringTokenizer.fromString(exp).toList((s) -> s); FunctionalList<String> shunted = yard.postfix(tokens, (s) -> s); - AST<String> rawAST = TreeConstructor.constructTree(shunted, - this::isOperator); + AST<String> rawAST = + TreeConstructor.constructTree(shunted, this::isOperator); AST<IDiceASTNode> bakedAST = rawAST.transmuteAST((tok) -> { if (isOperator(tok)) { @@ -44,22 +60,14 @@ public class DiceASTParser { return bakedAST; } - private boolean isOperator(String tok) { - switch (tok) { - case ":=": - case "+": - case "-": - case "*": - case "/": - case "c": - case "d": - return true; - default: - return false; - } - } - - private boolean isLiteral(String tok) { + /** + * Check if a token represents a literal + * + * @param tok + * The token to check + * @return Whether or not the token represents a literal + */ + private static boolean isLiteral(String tok) { if (StringUtils.countMatches(tok, 'c') == 1 && !tok.equalsIgnoreCase("c")) { return true; @@ -75,4 +83,26 @@ public class DiceASTParser { } } } + + /** + * Check if a token represents an operator + * + * @param tok + * The token to check if it represents an operator + * @return Whether or not the token represents an operator + */ + private boolean isOperator(String tok) { + switch (tok) { + case ":=": + case "+": + case "-": + case "*": + case "/": + case "c": + case "d": + return true; + default: + return false; + } + } } diff --git a/BJC-Utils2/src/main/java/bjc/utils/dice/ast/IDiceASTNode.java b/BJC-Utils2/src/main/java/bjc/utils/dice/ast/IDiceASTNode.java index 3fb14fe..073da89 100644 --- a/BJC-Utils2/src/main/java/bjc/utils/dice/ast/IDiceASTNode.java +++ b/BJC-Utils2/src/main/java/bjc/utils/dice/ast/IDiceASTNode.java @@ -1,5 +1,16 @@ package bjc.utils.dice.ast; +/** + * The interface for a node in a dice AST + * + * @author ben + * + */ public interface IDiceASTNode { + /** + * Check if this node represents an operator or not + * + * @return Whether or not this node represents an operator + */ public boolean isOperator(); } diff --git a/BJC-Utils2/src/main/java/bjc/utils/dice/ast/LiteralDiceNode.java b/BJC-Utils2/src/main/java/bjc/utils/dice/ast/LiteralDiceNode.java index 20358fb..b0c1400 100644 --- a/BJC-Utils2/src/main/java/bjc/utils/dice/ast/LiteralDiceNode.java +++ b/BJC-Utils2/src/main/java/bjc/utils/dice/ast/LiteralDiceNode.java @@ -1,8 +1,23 @@ package bjc.utils.dice.ast; +/** + * A AST node that represents a literal value + * + * @author ben + * + */ public class LiteralDiceNode implements IDiceASTNode { + /** + * The value contained by this node + */ private String data; + /** + * Create a new node with the given value + * + * @param data + * The value to be in this node + */ public LiteralDiceNode(String data) { this.data = data; } @@ -20,7 +35,12 @@ public class LiteralDiceNode implements IDiceASTNode { public String getData() { return data; } - + + /* + * (non-Javadoc) + * + * @see java.lang.Object#toString() + */ @Override public String toString() { return data; diff --git a/BJC-Utils2/src/main/java/bjc/utils/dice/ast/OperatorDiceNode.java b/BJC-Utils2/src/main/java/bjc/utils/dice/ast/OperatorDiceNode.java index 92b49b7..c4f7763 100644 --- a/BJC-Utils2/src/main/java/bjc/utils/dice/ast/OperatorDiceNode.java +++ b/BJC-Utils2/src/main/java/bjc/utils/dice/ast/OperatorDiceNode.java @@ -4,16 +4,51 @@ package bjc.utils.dice.ast; // 1. DiceASTExpression // 2. DiceASTFlattener // 3. DiceASTParser +/** + * A node that represents an operator + * + * @author ben + * + */ public enum OperatorDiceNode implements IDiceASTNode { - ASSIGN, ADD, SUBTRACT, MULTIPLY, DIVIDE, GROUP, COMPOUND; - - @Override - public boolean isOperator() { - return true; - } - + /** + * Represents adding two nodes + */ + ADD, + /** + * Represents assigning one node to another + */ + ASSIGN, + /** + * Representings combining two node values together + */ + COMPOUND, + /** + * Represents dividing two nodes + */ + DIVIDE, + /** + * Represents using one node a variable number of times + */ + GROUP, + /** + * Represents multiplying two nodes + */ + MULTIPLY, + /** + * Represents subtracting two nodes + */ + SUBTRACT; + + /** + * Create a operator node from a string + * + * @param s + * The string to convert to a node + * @return The operator corresponding to the node + */ public static OperatorDiceNode fromString(String s) { - switch(s) { + switch (s) { case ":=": return ASSIGN; case "+": @@ -29,7 +64,18 @@ public enum OperatorDiceNode implements IDiceASTNode { case "c": return COMPOUND; default: - throw new IllegalArgumentException(s + " is not a valid operator node"); + throw new IllegalArgumentException( + s + " is not a valid operator node"); } } + + /* + * (non-Javadoc) + * + * @see bjc.utils.dice.ast.IDiceASTNode#isOperator() + */ + @Override + public boolean isOperator() { + return true; + } } diff --git a/BJC-Utils2/src/main/java/bjc/utils/dice/ast/VariableDiceNode.java b/BJC-Utils2/src/main/java/bjc/utils/dice/ast/VariableDiceNode.java index 6ae3189..43a09b2 100644 --- a/BJC-Utils2/src/main/java/bjc/utils/dice/ast/VariableDiceNode.java +++ b/BJC-Utils2/src/main/java/bjc/utils/dice/ast/VariableDiceNode.java @@ -1,17 +1,27 @@ package bjc.utils.dice.ast; +/** + * A node that represents a variable reference + * + * @author ben + * + */ public class VariableDiceNode implements IDiceASTNode { + /** + * The variable referenced by this node + */ private String var; + /** + * Create a new node representing the specified variable + * + * @param data + * The name of the variable being referenced + */ public VariableDiceNode(String data) { this.var = data; } - @Override - public boolean isOperator() { - return false; - } - /** * Get the variable referenced by this AST node * @@ -20,7 +30,22 @@ public class VariableDiceNode implements IDiceASTNode { public String getVariable() { return var; } - + + /* + * (non-Javadoc) + * + * @see bjc.utils.dice.ast.IDiceASTNode#isOperator() + */ + @Override + public boolean isOperator() { + return false; + } + + /* + * (non-Javadoc) + * + * @see java.lang.Object#toString() + */ @Override public String toString() { return var; diff --git a/BJC-Utils2/src/main/java/bjc/utils/exceptions/FileNotChosenException.java b/BJC-Utils2/src/main/java/bjc/utils/exceptions/FileNotChosenException.java index e7cfe67..681a702 100644 --- a/BJC-Utils2/src/main/java/bjc/utils/exceptions/FileNotChosenException.java +++ b/BJC-Utils2/src/main/java/bjc/utils/exceptions/FileNotChosenException.java @@ -9,12 +9,18 @@ import java.io.IOException; * */ public class FileNotChosenException extends IOException { + /** + * Version ID for serialization + */ private static final long serialVersionUID = -8753348705210831096L; + /** + * Create a new exception + */ public FileNotChosenException() { super(); } - + /** * Create a new exception with the given cause * 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 72c5843..810e4f1 100644 --- a/BJC-Utils2/src/main/java/bjc/utils/funcdata/FunctionalList.java +++ b/BJC-Utils2/src/main/java/bjc/utils/funcdata/FunctionalList.java @@ -153,6 +153,11 @@ public class FunctionalList<E> implements Cloneable { * NOTE: The returned list will have the length of the shorter of this * list and the combined one. * + * @param <T> + * The type of the second list + * @param <F> + * The type of the combined list + * * @param l * The list to combine with * @param bf @@ -197,6 +202,9 @@ public class FunctionalList<E> implements Cloneable { * Apply a function to each member of the list, then flatten the * results. Does not change the underlying list. * + * @param <T> + * The type of the flattened list + * * @param f * The function to apply to each member of the list. * @return A new list containing the flattened results of applying the @@ -277,6 +285,15 @@ public class FunctionalList<E> implements Cloneable { } /** + * Retrieve the size of the wrapped list + * + * @return The size of the wrapped list + */ + public int getSize() { + return wrap.size(); + } + + /** * Check if this list is empty. * * @return Whether or not this list is empty. @@ -289,6 +306,9 @@ public class FunctionalList<E> implements Cloneable { * Create a new list by applying the given function to each element in * the list. Does not change the underlying list. * + * @param <T> + * The type of the transformed list + * * @param f * The function to apply to each element in the list * @return A new list containing the mapped elements of this list. @@ -304,6 +324,9 @@ public class FunctionalList<E> implements Cloneable { /** * Zip two lists into a list of pairs * + * @param <T> + * The type of the second list + * * @param fl * The list to use as the left side of the pair * @return A list containing pairs of this element and the specified @@ -314,6 +337,31 @@ public class FunctionalList<E> implements Cloneable { } /** + * Partition this list into a list of sublists + * + * @param nPerPart + * The size of elements to put into each one of the sublists + * @return A list partitioned into partitions of size nPerPart + */ + public FunctionalList<FunctionalList<E>> partition(int nPerPart) { + FunctionalList<FunctionalList<E>> ret = new FunctionalList<>(); + + GenHolder<FunctionalList<E>> currPart = + new GenHolder<>(new FunctionalList<>()); + + this.forEach((val) -> { + if (currPart.unwrap((vl) -> vl.getSize() >= nPerPart)) { + ret.add(currPart.unwrap((vl) -> vl)); + currPart.transform((vl) -> new FunctionalList<>()); + } else { + currPart.unwrap((vl) -> vl.add(val)); + } + }); + + return ret; + } + + /** * Prepend an item to the list * * @param item @@ -338,6 +386,11 @@ public class FunctionalList<E> implements Cloneable { /** * Reduce this list to a single value, using a accumulative approach. * + * @param <T> + * The in-between type of the values + * @param <F> + * The final value type + * * @param val * The initial value of the accumulative state. * @param bf @@ -369,6 +422,12 @@ public class FunctionalList<E> implements Cloneable { return wrap.removeIf(remPred); } + /** + * Remove all parameters that match a given parameter + * + * @param obj + * The object to remove all matching copies of + */ public void removeMatching(E obj) { removeIf((ele) -> ele.equals(obj)); } @@ -416,6 +475,7 @@ public class FunctionalList<E> implements Cloneable { * * @see java.lang.Object#toString() */ + @Override public String toString() { StringBuilder sb = new StringBuilder("("); @@ -426,38 +486,4 @@ public class FunctionalList<E> implements Cloneable { return sb.toString(); } - - /** - * Retrieve the size of the wrapped list - * - * @return The size of the wrapped list - */ - public int getSize() { - return wrap.size(); - } - - /** - * Partition this list into a list of sublists - * - * @param nPerPart - * The size of elements to put into each one of the sublists - * @return A list partitioned into partitions of size nPerPart - */ - public FunctionalList<FunctionalList<E>> partition(int nPerPart) { - FunctionalList<FunctionalList<E>> ret = new FunctionalList<>(); - - GenHolder<FunctionalList<E>> currPart = new GenHolder<>( - new FunctionalList<>()); - - this.forEach((val) -> { - if (currPart.unwrap((vl) -> vl.getSize() >= nPerPart)) { - ret.add(currPart.unwrap((vl) -> vl)); - currPart.transform((vl) -> new FunctionalList<>()); - } else { - currPart.unwrap((vl) -> vl.add(val)); - } - }); - - return ret; - } } diff --git a/BJC-Utils2/src/main/java/bjc/utils/funcdata/FunctionalStringTokenizer.java b/BJC-Utils2/src/main/java/bjc/utils/funcdata/FunctionalStringTokenizer.java index ed4b9d3..aa59a31 100644 --- a/BJC-Utils2/src/main/java/bjc/utils/funcdata/FunctionalStringTokenizer.java +++ b/BJC-Utils2/src/main/java/bjc/utils/funcdata/FunctionalStringTokenizer.java @@ -12,19 +12,20 @@ import java.util.function.Function; */ public class FunctionalStringTokenizer { /** - * The string tokenizer being driven + * Create a new tokenizer from the specified string. + * + * @param s + * The string to create a tokenizer from. + * @return A new tokenizer that splits the provided string on spaces. */ - private StringTokenizer inp; + public static FunctionalStringTokenizer fromString(String s) { + return new FunctionalStringTokenizer(new StringTokenizer(s, " ")); + } /** - * Create a functional string tokenizer from a non-functional one - * - * @param inp - * The non-functional string tokenizer to wrap + * The string tokenizer being driven */ - public FunctionalStringTokenizer(StringTokenizer inp) { - this.inp = inp; - } + private StringTokenizer inp; /** * Create a functional string tokenizer from a given string @@ -40,14 +41,26 @@ public class FunctionalStringTokenizer { * Create a functional string tokenizer from a given string and set of * seperators * - * @param inp The string to tokenize - * @param seps The string to use for splitting + * @param inp + * The string to tokenize + * @param seps + * The string to use for splitting */ public FunctionalStringTokenizer(String inp, String seps) { this.inp = new StringTokenizer(inp, seps); } /** + * Create a functional string tokenizer from a non-functional one + * + * @param inp + * The non-functional string tokenizer to wrap + */ + public FunctionalStringTokenizer(StringTokenizer inp) { + this.inp = inp; + } + + /** * Execute a provided action for each of the remaining tokens * * @param f @@ -60,6 +73,15 @@ public class FunctionalStringTokenizer { } /** + * Get the string tokenizer encapsuled by this + * + * @return The encapsulated tokenizer + */ + public StringTokenizer getInternal() { + return inp; + } + + /** * Return the next token from the tokenizer Returns null if no more * tokens are available * @@ -73,6 +95,9 @@ public class FunctionalStringTokenizer { * Convert the contents of this tokenizer into a list. Consumes all of * the input from this tokenizer. * + * @param <E> + * The type of the converted tokens + * * @param f * The function to use to convert tokens. * @return A list containing all of the converted tokens. @@ -84,23 +109,4 @@ public class FunctionalStringTokenizer { return r; } - - /** - * Create a new tokenizer from the specified string. - * - * @param s - * The string to create a tokenizer from. - * @return A new tokenizer that splits the provided string on spaces. - */ - public static FunctionalStringTokenizer fromString(String s) { - return new FunctionalStringTokenizer(new StringTokenizer(s, " ")); - } - - /** - * Get the string tokenizer encapsuled by this - * @return The encapsulated tokenizer - */ - public StringTokenizer getInternal() { - return inp; - } } diff --git a/BJC-Utils2/src/main/java/bjc/utils/funcdata/ITreePart.java b/BJC-Utils2/src/main/java/bjc/utils/funcdata/ITreePart.java index 24e68d4..7d8b6b0 100644 --- a/BJC-Utils2/src/main/java/bjc/utils/funcdata/ITreePart.java +++ b/BJC-Utils2/src/main/java/bjc/utils/funcdata/ITreePart.java @@ -55,6 +55,9 @@ public interface ITreePart<T> { * Collapses this tree part into a single value. Does not change the * underlying tree. * + * @param <E> + * The type of the final collapsed value + * * @param f * The function to use to transform data into mapped form. * @param bf 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 3f65481..0bd0119 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 @@ -53,7 +53,7 @@ public class BinarySearchTree<T> { nCount++; if (root == null) { - root = new BinarySearchTreeNode<T>(dat, null, null); + root = new BinarySearchTreeNode<>(dat, null, null); } else { root.add(dat, comp); } @@ -75,7 +75,8 @@ public class BinarySearchTree<T> { while ((piv - adj) >= 0 && (piv + adj) < elms.getSize()) { if (root == null) { - root = new BinarySearchTreeNode<T>(elms.getByIndex(piv), null, null); + root = new BinarySearchTreeNode<>(elms.getByIndex(piv), + null, null); } else { root.add(elms.getByIndex(piv + adj), comp); root.add(elms.getByIndex(piv - adj), comp); diff --git a/BJC-Utils2/src/main/java/bjc/utils/funcdata/bst/BinarySearchTreeLeaf.java b/BJC-Utils2/src/main/java/bjc/utils/funcdata/bst/BinarySearchTreeLeaf.java index 02b9c7a..9befd17 100644 --- a/BJC-Utils2/src/main/java/bjc/utils/funcdata/bst/BinarySearchTreeLeaf.java +++ b/BJC-Utils2/src/main/java/bjc/utils/funcdata/bst/BinarySearchTreeLeaf.java @@ -66,8 +66,8 @@ public class BinarySearchTreeLeaf<T> implements ITreePart<T> { * java.util.Comparator) */ @Override - public boolean contains(T data, Comparator<T> cmp) { - return this.data.equals(data); + public boolean contains(T dat, Comparator<T> cmp) { + return this.data.equals(dat); } /* @@ -117,6 +117,7 @@ public class BinarySearchTreeLeaf<T> implements ITreePart<T> { * bjc.utils.data.bst.ITreePart#forEach(bjc.utils.data.bst.ITreePart. * TreeLinearizationMethod, java.util.function.Predicate) */ + @Override public boolean forEach(TreeLinearizationMethod tlm, Predicate<T> c) { return c.test(data); } diff --git a/BJC-Utils2/src/main/java/bjc/utils/funcdata/bst/BinarySearchTreeNode.java b/BJC-Utils2/src/main/java/bjc/utils/funcdata/bst/BinarySearchTreeNode.java index 30a9fbd..715456c 100644 --- a/BJC-Utils2/src/main/java/bjc/utils/funcdata/bst/BinarySearchTreeNode.java +++ b/BJC-Utils2/src/main/java/bjc/utils/funcdata/bst/BinarySearchTreeNode.java @@ -55,7 +55,7 @@ public class BinarySearchTreeNode<T> extends BinarySearchTreeLeaf<T> { switch (comp.compare(data, dat)) { case -1: if (left == null) { - left = new BinarySearchTreeNode<T>(dat, null, null); + left = new BinarySearchTreeNode<>(dat, null, null); } else { left.add(dat, comp); } @@ -68,7 +68,7 @@ public class BinarySearchTreeNode<T> extends BinarySearchTreeLeaf<T> { } case 1: if (right == null) { - right = new BinarySearchTreeNode<T>(dat, null, null); + right = new BinarySearchTreeNode<>(dat, null, null); } else { right.add(dat, comp); } @@ -96,9 +96,9 @@ public class BinarySearchTreeNode<T> extends BinarySearchTreeLeaf<T> { } @Override - public boolean contains(T data, Comparator<T> cmp) { + public boolean contains(T dat, Comparator<T> cmp) { return directedWalk(ds -> { - switch (cmp.compare(data, ds)) { + switch (cmp.compare(dat, ds)) { case -1: return LEFT; case 0: diff --git a/BJC-Utils2/src/main/java/bjc/utils/funcutils/ListUtils.java b/BJC-Utils2/src/main/java/bjc/utils/funcutils/ListUtils.java index 1bc9a97..fc1894e 100644 --- a/BJC-Utils2/src/main/java/bjc/utils/funcutils/ListUtils.java +++ b/BJC-Utils2/src/main/java/bjc/utils/funcutils/ListUtils.java @@ -71,6 +71,9 @@ public class ListUtils { * Partition a list into a list of lists, where each element can count * for more than one element in a partition * + * @param <E> + * The type of elements in the list to partition + * * @param list * The list to partition * @param eleCount @@ -90,8 +93,8 @@ public class ListUtils { /* * List that holds current partition */ - GenHolder<FunctionalList<E>> currPart = new GenHolder<>( - new FunctionalList<>()); + GenHolder<FunctionalList<E>> currPart = + new GenHolder<>(new FunctionalList<>()); /* * List that holds elements rejected during current pass */ @@ -107,7 +110,7 @@ public class ListUtils { */ for (int nIterations = 0; nIterations < MAX_NTRIESPART && !rejects.isEmpty(); nIterations++) { - list.forEach(new GroupPartIteration<E>(ret, currPart, rejects, + list.forEach(new GroupPartIteration<>(ret, currPart, rejects, numInCurrPart, nPerPart, eleCount)); if (rejects.isEmpty()) { 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 623c212..463ad8b 100644 --- a/BJC-Utils2/src/main/java/bjc/utils/gen/WeightedGrammar.java +++ b/BJC-Utils2/src/main/java/bjc/utils/gen/WeightedGrammar.java @@ -50,6 +50,9 @@ public class WeightedGrammar<E> { /** * Create a new weighted grammar that uses the specified source of * randomness. + * + * @param src + * The source of randomness to use */ public WeightedGrammar(Random src) { this(); @@ -166,7 +169,10 @@ public class WeightedGrammar<E> { /** * Generate a generic sentance from a initial rule. * - * @param initRule + * @param <T> + * The type of the transformed output + * + * @param initRle * The initial rule to start with. * @param f * The function to transform grammar output into something. @@ -175,24 +181,24 @@ public class WeightedGrammar<E> { * @return A randomly generated sentance from the specified initial * rule. */ - public <T> FunctionalList<T> genGeneric(E initRule, Function<E, T> f, + public <T> FunctionalList<T> genGeneric(E initRle, Function<E, T> f, T spacer) { FunctionalList<T> r = new FunctionalList<>(); - if (subgrammars.containsKey(initRule)) { - subgrammars.get(initRule).genGeneric(initRule, f, spacer) + if (subgrammars.containsKey(initRle)) { + subgrammars.get(initRle).genGeneric(initRle, f, spacer) .forEach(rp -> { r.add(rp); r.add(spacer); }); - } else if (rules.containsKey(initRule)) { - rules.get(initRule).genVal().forEach( + } else if (rules.containsKey(initRle)) { + rules.get(initRle).genVal().forEach( rp -> genGeneric(rp, f, spacer).forEach(rp2 -> { r.add(rp2); r.add(spacer); })); } else { - r.add(f.apply(initRule)); + r.add(f.apply(initRle)); r.add(spacer); } @@ -203,17 +209,22 @@ public class WeightedGrammar<E> { * Generate a random list of grammar elements from a given initial * rule. * - * @param initRule + * @param initRle * The initial rule to start with. * @param spacer * The item to use to space the list. * @return A list of random grammar elements generated by the specified * rule. */ - public FunctionalList<E> genList(E initRule, E spacer) { - return genGeneric(initRule, s -> s, spacer); + public FunctionalList<E> genList(E initRle, E spacer) { + return genGeneric(initRle, s -> s, spacer); } + /** + * Get the initial rule of this grammar + * + * @return The initial rule of this grammar + */ public String getInitRule() { return initRule; } @@ -254,15 +265,16 @@ public class WeightedGrammar<E> { int nTimes) { WeightedRandom<FunctionalList<E>> rule = rules.get(rName); - FunctionalList<Pair<Integer, FunctionalList<E>>> newResults = new FunctionalList<>(); + FunctionalList<Pair<Integer, FunctionalList<E>>> newResults = + new FunctionalList<>(); rule.getValues().forEach((par) -> { FunctionalList<FunctionalList<E>> nls = new FunctionalList<>(); // TODO bugtest this. if it works, write multiSuffixWith for (int i = 1; i <= nTimes; i++) { - FunctionalList<E> nl = par - .merge((left, right) -> right.clone()); + FunctionalList<E> nl = + par.merge((left, right) -> right.clone()); for (int j = 1; j <= i; j++) { nl.prepend(prefixToken); @@ -293,11 +305,12 @@ public class WeightedGrammar<E> { public void prefixRule(E rName, E prefixToken, int addProb) { WeightedRandom<FunctionalList<E>> rule = rules.get(rName); - FunctionalList<Pair<Integer, FunctionalList<E>>> newResults = new FunctionalList<>(); + FunctionalList<Pair<Integer, FunctionalList<E>>> newResults = + new FunctionalList<>(); rule.getValues().forEach((par) -> { - FunctionalList<E> nl = par - .merge((left, right) -> right.clone()); + FunctionalList<E> nl = + par.merge((left, right) -> right.clone()); nl.prepend(prefixToken); newResults.add(new Pair<>( @@ -368,11 +381,12 @@ public class WeightedGrammar<E> { public void suffixRule(E rName, E prefixToken, int addProb) { WeightedRandom<FunctionalList<E>> rule = rules.get(rName); - FunctionalList<Pair<Integer, FunctionalList<E>>> newResults = new FunctionalList<>(); + FunctionalList<Pair<Integer, FunctionalList<E>>> newResults = + new FunctionalList<>(); rule.getValues().forEach((par) -> { - FunctionalList<E> nl = par - .merge((left, right) -> right.clone()); + FunctionalList<E> nl = + par.merge((left, right) -> right.clone()); nl.add(prefixToken); newResults.add(new Pair<>( diff --git a/BJC-Utils2/src/main/java/bjc/utils/gen/WeightedRandom.java b/BJC-Utils2/src/main/java/bjc/utils/gen/WeightedRandom.java index bf3b4b6..3ddb8ef 100644 --- a/BJC-Utils2/src/main/java/bjc/utils/gen/WeightedRandom.java +++ b/BJC-Utils2/src/main/java/bjc/utils/gen/WeightedRandom.java @@ -69,7 +69,7 @@ public class WeightedRandom<E> { */ public E genVal() { GenHolder<Integer> v = new GenHolder<>(src.nextInt(totalChance)); - IHolder<E> res = new GenHolder<E>(); + IHolder<E> res = new GenHolder<>(); GenHolder<Boolean> bl = new GenHolder<>(true); probs.forEachIndexed((i, p) -> { diff --git a/BJC-Utils2/src/main/java/bjc/utils/graph/Graph.java b/BJC-Utils2/src/main/java/bjc/utils/graph/Graph.java index 3b54b27..2f2ac04 100644 --- a/BJC-Utils2/src/main/java/bjc/utils/graph/Graph.java +++ b/BJC-Utils2/src/main/java/bjc/utils/graph/Graph.java @@ -35,7 +35,7 @@ public class Graph<T> { * Create a new graph */ public Graph() { - graph = new HashMap<T, Map<T, Integer>>(); + graph = new HashMap<>(); } /** @@ -121,20 +121,18 @@ public class Graph<T> { * Uses Prim's algorothm to calculate a MST for the graph. If the graph * is non-connected, this will lead to unpredictable results. * - * @param graph - * a connected graph. * @return a list of edges that constitute the MST */ public List<Edge<T>> getMinSpanTree() { // Set of all of the currently available edges - Queue<Edge<T>> availEdges = new PriorityQueue<Edge<T>>(10, + Queue<Edge<T>> availEdges = new PriorityQueue<>(10, (e1, e2) -> e1.getDistance() - e2.getDistance()); // The MST of the graph - List<Edge<T>> minEdges = new ArrayList<Edge<T>>(); + List<Edge<T>> minEdges = new ArrayList<>(); // The set of all of the visited vertices. - Set<T> visited = new HashSet<T>(); + Set<T> visited = new HashSet<>(); // Start at the initial vertex and visit it IHolder<T> src = new GenHolder<>(getInitial()); @@ -146,12 +144,11 @@ public class Graph<T> { forAllEdgesMatchingAt(src.unwrap(vl -> vl), (tgt, weight) -> !visited.contains(tgt), - (tgt, weight) -> availEdges.add(new Edge<T>( + (tgt, weight) -> availEdges.add(new Edge<>( src.unwrap(vl -> vl), tgt, weight))); // Get the edge with the minimum distance - IHolder<Edge<T>> minEdge = new GenHolder<>( - availEdges.poll()); + IHolder<Edge<T>> minEdge = new GenHolder<>(availEdges.poll()); // Only consider edges where we haven't visited the target of // the edge @@ -240,6 +237,7 @@ public class Graph<T> { /** * Create a graph from a list of edges + * @param <E> The type of data stored in the edges * * @param edges * The list of edges to build from 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 0acbd65..f18378e 100644 --- a/BJC-Utils2/src/main/java/bjc/utils/gui/SimpleDialogs.java +++ b/BJC-Utils2/src/main/java/bjc/utils/gui/SimpleDialogs.java @@ -51,6 +51,9 @@ public class SimpleDialogs { /** * Asks the user to pick an option from a series of choices. * + * @param <E> + * The type of choices for the user to pick + * * @param parent * The parent frame for this dialog * @param title @@ -137,6 +140,7 @@ public class SimpleDialogs { /** * Get a value parsable from a string from the user. + * @param <E> The type of the value parsed from the string * * @param parent * The parent component for dialogs. diff --git a/BJC-Utils2/src/main/java/bjc/utils/gui/SimpleJList.java b/BJC-Utils2/src/main/java/bjc/utils/gui/SimpleJList.java index 8bca490..614cf33 100644 --- a/BJC-Utils2/src/main/java/bjc/utils/gui/SimpleJList.java +++ b/BJC-Utils2/src/main/java/bjc/utils/gui/SimpleJList.java @@ -14,17 +14,23 @@ public class SimpleJList { /** * Create a new JList from a given list. * + * @param <E> + * The type of data in the JList + * * @param ls * The list to populate the JList with. * @return A JList populated with the elements from ls. */ public static <E> JList<E> buildFromList(Iterable<E> ls) { - return new JList<E>(buildModel(ls)); + return new JList<>(buildModel(ls)); } /** * Create a new list model from a given list. * + * @param <E> + * The type of data in the list model + * * @param ls * The list to fill the list model from. * @return A list model populated with the elements from ls. diff --git a/BJC-Utils2/src/main/java/bjc/utils/parserutils/AST.java b/BJC-Utils2/src/main/java/bjc/utils/parserutils/AST.java index f390155..943d177 100644 --- a/BJC-Utils2/src/main/java/bjc/utils/parserutils/AST.java +++ b/BJC-Utils2/src/main/java/bjc/utils/parserutils/AST.java @@ -1,6 +1,5 @@ package bjc.utils.parserutils; -import java.util.Map; import java.util.function.BinaryOperator; import java.util.function.Consumer; import java.util.function.Function; @@ -41,9 +40,9 @@ public class AST<T> { * * @param tokn * The token in this node - * @param left + * @param lft * The left child of this AST - * @param right + * @param rght * The right child of this AST */ public AST(T tokn, AST<T> lft, AST<T> rght) { @@ -53,6 +52,14 @@ public class AST<T> { right = rght; } + /** + * Traverse an AST + * + * @param tlm + * The way to traverse the tree + * @param con + * The function to call on each traversed element + */ public void traverse(TreeLinearizationMethod tlm, Consumer<T> con) { if (left != null && right != null) { switch (tlm) { @@ -83,6 +90,8 @@ public class AST<T> { /** * Collapse this tree into a single node + * @param <E> The final value of the collapsed tree + * @param <T2> * * @param tokenTransform * The function to transform nodes into data @@ -159,7 +168,7 @@ public class AST<T> { * @param n * The number of levels to indent */ - private void indentNLevels(StringBuilder sb, int n) { + private static void indentNLevels(StringBuilder sb, int n) { for (int i = 0; i <= n; i++) { sb.append("\t"); } @@ -190,6 +199,7 @@ public class AST<T> { /** * Transmute the tokens in an AST into a different sort of token + * @param <E> The type of the transformed tokens * * @param tokenTransformer * The transform to run on the tokens @@ -207,6 +217,6 @@ public class AST<T> { r = right.transmuteAST(tokenTransformer); } - return new AST<E>(tokenTransformer.apply(token), l, r); + return new AST<>(tokenTransformer.apply(token), l, r); } } 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 963437e..12f6891 100644 --- a/BJC-Utils2/src/main/java/bjc/utils/parserutils/RuleBasedConfigReader.java +++ b/BJC-Utils2/src/main/java/bjc/utils/parserutils/RuleBasedConfigReader.java @@ -17,8 +17,8 @@ import bjc.utils.funcdata.FunctionalStringTokenizer; * * @author ben * - * @param <E>The - * type of the state object to use + * @param <E> + * The type of the state object to use */ public class RuleBasedConfigReader<E> { private BiConsumer<FunctionalStringTokenizer, Pair<String, E>> startRule; @@ -48,11 +48,28 @@ public class RuleBasedConfigReader<E> { this.pragmas = new HashMap<>(); } + /** + * Add a pragma to this reader + * + * @param pragName + * The name of the pragma to add + * @param pragAct + * The function to execute when this pragma is read + */ public void addPragma(String pragName, BiConsumer<FunctionalStringTokenizer, E> pragAct) { pragmas.put(pragName, pragAct); } + /** + * Run a stream through this reader + * + * @param is + * The stream to get input + * @param initState + * The initial state of the reader + * @return The final state of the reader + */ public E fromStream(InputStream is, E initState) { Scanner scn = new Scanner(is); @@ -68,8 +85,8 @@ public class RuleBasedConfigReader<E> { continueRule.accept(new FunctionalStringTokenizer( ln.substring(1), " "), stat); } else { - FunctionalStringTokenizer stk = new FunctionalStringTokenizer( - ln, " "); + FunctionalStringTokenizer stk = + new FunctionalStringTokenizer(ln, " "); String nxtToken = stk.nextToken(); if (nxtToken.equals("#")) { @@ -82,8 +99,7 @@ public class RuleBasedConfigReader<E> { "Unknown pragma " + tk); }).accept(stk, stat); } else { - startRule.accept(stk, - new Pair<String, E>(nxtToken, stat)); + startRule.accept(stk, new Pair<>(nxtToken, stat)); } } } @@ -93,15 +109,33 @@ public class RuleBasedConfigReader<E> { return stat; } + /** + * Set the action to execute when continuing a rule + * + * @param continueRule + * The action to execute on continuation of a rule + */ public void setContinueRule( BiConsumer<FunctionalStringTokenizer, E> continueRule) { this.continueRule = continueRule; } + /** + * Set the action to execute when ending a rule + * + * @param endRule + * The action to execute on ending of a rule + */ public void setEndRule(Consumer<E> endRule) { this.endRule = endRule; } + /** + * Set the action to execute when starting a rule + * + * @param startRule + * The action to execute on starting of a rule + */ 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 8a036ce..b5f48e7 100644 --- a/BJC-Utils2/src/main/java/bjc/utils/parserutils/ShuntingYard.java +++ b/BJC-Utils2/src/main/java/bjc/utils/parserutils/ShuntingYard.java @@ -19,8 +19,29 @@ import bjc.utils.funcdata.FunctionalList; */ public class ShuntingYard<E> { + /** + * A enum representing the fundamental operator types + * + * @author ben + * + */ public static enum Operator implements IPrecedent { - ADD(1), DIVIDE(4), MULTIPLY(3), SUBTRACT(2); + /** + * Represents addition + */ + ADD(1), + /** + * Represents division + */ + DIVIDE(4), + /** + * Represents multiplication + */ + MULTIPLY(3), + /** + * Represents subtraction + */ + SUBTRACT(2); private final int precedence; @@ -61,6 +82,7 @@ public class ShuntingYard<E> { * * @param tok * The token representing the operator + * @param i The precedence of the operator to add */ public void addOp(String tok, int i) { this.addOp(tok, IPrecedent.newSimplePrecedent(i)); diff --git a/BJC-Utils2/src/main/java/bjc/utils/parserutils/TreeConstructor.java b/BJC-Utils2/src/main/java/bjc/utils/parserutils/TreeConstructor.java index e1a03f1..30c147e 100644 --- a/BJC-Utils2/src/main/java/bjc/utils/parserutils/TreeConstructor.java +++ b/BJC-Utils2/src/main/java/bjc/utils/parserutils/TreeConstructor.java @@ -13,8 +13,6 @@ import bjc.utils.funcdata.FunctionalList; * * @author ben * - * @param <T> - * The elements of the parse tree */ public class TreeConstructor { /** @@ -22,6 +20,8 @@ public class TreeConstructor { * * Only binary operators are accepted. * + * @param <T> + * The elements of the parse tree * @param toks * The list of tokens to build a tree from * @param opPredicate @@ -43,7 +43,7 @@ public class TreeConstructor { AST<T> right = deq.pop(); AST<T> left = deq.pop(); - AST<T> newAST = new AST<T>(ele, left, right); + AST<T> newAST = new AST<>(ele, left, right); deq.push(newAST); @@ -61,7 +61,7 @@ public class TreeConstructor { initState.doWith((par) -> par.doWith((deq, ast) -> { deq.push(newAST); })); - + initState.transform((par) -> { return (Pair<Deque<AST<T>>, AST<T>>) par .apply((d) -> d, (a) -> newAST); |
