From ebe04a4223e3945264013fdc8b27b2e55fc6fb74 Mon Sep 17 00:00:00 2001 From: Ben Culkin Date: Sat, 13 Mar 2021 07:20:54 -0500 Subject: Reorganize examples a bit The random generation examples are now in a more appropriate package. Also, moved another random generation example from RGens to here, since it didn't use any RGens specific things --- .../java/bjc/utils/examples/gen/DiabloItemGen.java | 96 +++++++++ .../utils/examples/gen/RandomStringExamples.java | 67 +++++++ .../java/bjc/utils/examples/gen/ZadronsPouch.java | 220 +++++++++++++++++++++ .../bjc/utils/examples/rangen/DiabloItemGen.java | 96 --------- .../examples/rangen/RandomStringExamples.java | 67 ------- 5 files changed, 383 insertions(+), 163 deletions(-) create mode 100644 base/src/examples/java/bjc/utils/examples/gen/DiabloItemGen.java create mode 100644 base/src/examples/java/bjc/utils/examples/gen/RandomStringExamples.java create mode 100644 base/src/examples/java/bjc/utils/examples/gen/ZadronsPouch.java delete mode 100644 base/src/examples/java/bjc/utils/examples/rangen/DiabloItemGen.java delete mode 100644 base/src/examples/java/bjc/utils/examples/rangen/RandomStringExamples.java (limited to 'base/src') diff --git a/base/src/examples/java/bjc/utils/examples/gen/DiabloItemGen.java b/base/src/examples/java/bjc/utils/examples/gen/DiabloItemGen.java new file mode 100644 index 0000000..8699527 --- /dev/null +++ b/base/src/examples/java/bjc/utils/examples/gen/DiabloItemGen.java @@ -0,0 +1,96 @@ +package bjc.utils.examples.gen; + +import bjc.funcdata.FunctionalStringTokenizer; +import bjc.funcdata.ListEx; +import bjc.utils.gen.WeightedGrammar; + +/** + * Example showing how to use the weighted random number generator. + * + * @author ben + * + */ +public class DiabloItemGen { + private static WeightedGrammar rules = new WeightedGrammar<>(); + + private static void addCase(final String ruleName, final int probability, + final String ruleParts) { + final ListEx parts = FunctionalStringTokenizer.fromString(ruleParts) + .toList(strang -> strang); + + rules.addCase(ruleName, probability, parts); + } + + private static void addInfixRules() { + final String rn = ""; + + addCase(rn, 60, "sword"); + addCase(rn, 50, "armor"); + addCase(rn, 40, "rune"); + addCase(rn, 30, "scroll"); + addCase(rn, 20, "potion"); + addCase(rn, 10, "helm"); + } + + private static void addItemRules() { + final String rn = ""; + + addCase(rn, 10, ""); + addCase(rn, 20, " "); + addCase(rn, 30, " "); + addCase(rn, 40, " "); + addCase(rn, 50, " "); + addCase(rn, 60, " "); + } + + private static void addPrefixRules() { + final String rn = ""; + + addCase(rn, 60, "sturdy"); + addCase(rn, 50, "fine"); + addCase(rn, 40, "strong"); + addCase(rn, 30, "azure"); + addCase(rn, 20, "crimson"); + addCase(rn, 10, "phasing"); + } + + private static void addSuffixRules() { + final String rn = ""; + + addCase(rn, 60, "of Health"); + addCase(rn, 50, "of Wealth"); + addCase(rn, 40, "of Life"); + addCase(rn, 30, "of the Jackal"); + addCase(rn, 20, "of Vitality"); + addCase(rn, 10, "of Ability"); + } + + /** + * Main Method + * + * @param args + * Unused CLI args + */ + public static void main(final String[] args) { + rules.addRule(""); + addItemRules(); + + rules.addRule(""); + addSuffixRules(); + + rules.addRule(""); + addPrefixRules(); + + rules.addRule(""); + addInfixRules(); + + for (int i = 0; i < 100; i++) { + final ListEx ls = rules.generateListValues("", " "); + + final StringBuilder sb = new StringBuilder(); + ls.forEach(sb::append); + + System.out.println(sb.toString().replaceAll("\\s+", " ")); + } + } +} diff --git a/base/src/examples/java/bjc/utils/examples/gen/RandomStringExamples.java b/base/src/examples/java/bjc/utils/examples/gen/RandomStringExamples.java new file mode 100644 index 0000000..5e71ce0 --- /dev/null +++ b/base/src/examples/java/bjc/utils/examples/gen/RandomStringExamples.java @@ -0,0 +1,67 @@ +package bjc.utils.examples.gen; + +import bjc.funcdata.FunctionalList; +import bjc.funcdata.FunctionalStringTokenizer; +import bjc.funcdata.ListEx; +import bjc.utils.gen.RandomGrammar; + +/** + * Examples of random grammar + * + * @author ben + * + */ +public class RandomStringExamples { + private static RandomGrammar rg; + + private static void addRule(final String rule, final String... cases) { + final ListEx> cses = new FunctionalList<>(); + + for (final String strang : cases) { + final ListEx lst + = FunctionalStringTokenizer.fromString(strang).toList(s -> s); + + cses.add(lst); + } + + rg.makeRule(rule, cses); + } + + /** + * Main method + * + * @param args + * Unused CLI args + */ + public static void main(final String[] args) { + rg = new RandomGrammar<>(); + + addRule("", " ", + " thinks that I am ", "I ", + "You think that I am "); + + addRule("", "dancing", "eating", "sleeping"); + + addRule("", "", "life", "my computer", "my friends"); + + addRule("", "hate", "am jealous of", "love"); + + addRule("", "hates", "loves"); + + addRule("", "my sister", "my father", "my girlfriend", + "the man next door"); + + addRule("", "creative", "intelligent"); + + addRule("", "", " with ", ""); + + for (int i = 0; i < 10; i++) { + final ListEx ls = rg.generateListValues("", " "); + + final StringBuilder sb = new StringBuilder(); + ls.forEach(sb::append); + + System.out.println(sb.toString().replaceAll("\\s+", " ")); + } + } +} diff --git a/base/src/examples/java/bjc/utils/examples/gen/ZadronsPouch.java b/base/src/examples/java/bjc/utils/examples/gen/ZadronsPouch.java new file mode 100644 index 0000000..184d127 --- /dev/null +++ b/base/src/examples/java/bjc/utils/examples/gen/ZadronsPouch.java @@ -0,0 +1,220 @@ +package bjc.utils.examples.gen; + +import bjc.funcdata.FunctionalList; +import bjc.funcdata.FunctionalStringTokenizer; +import bjc.funcdata.ListEx; +import bjc.utils.gen.RandomGrammar; + +/** + * Example showing code manipulate of random grammars + * + * @author ben + */ +public class ZadronsPouch { + /** + * Main method for running application + * + * @param args + * Unused CLI args + */ + public static void main(String[] args) { + ZadronsPouch zp = new ZadronsPouch(); + + for (int i = 0; i < 100; i++) { + ListEx ls = zp.grammar.generateListValues("[item]", " "); + + StringBuilder sb = new StringBuilder(); + + ls.forEach(sp -> sb.append(sp)); + + System.out.println(sb.toString().replaceAll("\\s+", " ")); + } + } + + private RandomGrammar grammar; + + /** Create a new instance with a grammar */ + public ZadronsPouch() { + grammar = new RandomGrammar<>(); + + /* + * @NOTE + * Should there be some sort of builder sort of interface? + */ + addRule("[item]", + "[egg]", "[glove]", "[crys-sphere]", "[rock]", + "[figurine]", "[vial]", "[mini-weapon]", "[bag]", + "[card]", "[rope]", "[box]", "[wand]"); + + addEggRules(); + addGloveRules(); + addCrysSphereRules(); + addRockRules(); + + addFigurineRules(); + addVialRules(); + addMiniWeaponRules(); + addBagRules(); + + addCardRules(); + addRopeRules(); + addBoxRules(); + addWandRules(); + } + + private void addBagRules() { + addRule("[bag]", + "bag of [bag-type]", "[sack-type] sack", "[purse-type] purse"); + addRule("[bag-type]", + "holding", "tricks", "useful items", + "devouring", "dwarf-kind", "invisible cloth", + "monster summoning"); + addRule("[sack-type]", + "lunch", "recursive"); + addRule("[purse-type]", + "everfull"); + } + + private void addBoxRules() { + addRule("[box]", + "[box-type] box", "cube of [box-type]"); + addRule("[box-type]", + "limited-force", "frost-resisting", "morphing", + "self-destructing", "pandora", "panicking"); + } + + private void addCardRules() { + addRule("[card]", + "card of [card-type]", "[card-type] card"); + addRule("[card-type]", + "fate", "teleporting", "elusive treasure", "spell-storing", + "many-things", "imprisoning", "messaging", "bounty"); + } + + private void addCrysSphereRules() { + addRule("[crys-sphere]", + "[sphere-type] spheres", "[sphere-type] sphere", + "lens of [lens-type]", "[crystal-type] crystal", + "crystal of [crystal-type]", "crystal ball", + "crystal ball of [crys-suffix]"); + addRule("[sphere-type]", + "microphonic", "seeing-eye"); + addRule("[lens-type]", + "detection"); + addRule("[crystal-type]", + "prison", "radar"); + addRule("[crys-suffix]", + "jumping"); + } + + private void addEggRules() { + addRule("[egg]", + "[egg-type] egg"); + addRule("[egg-type]", + "copper", "stone", "golden", + "white", "white/pink", "glass"); + } + + private void addFigurineRules() { + addRule("[figurine]", + "[fig-material] [fig-animal]"); + addRule("[fig-material]", + "golden", "onyx", "serpentine", "ivory", + "marble", "bronze", "jade", "limestone"); + addRule("[fig-animal]", + "lion", "dog", "owl", "goat", + "elephant", "warrior", "palace", "leprechaun"); + } + + private void addGloveRules() { + addRule("[glove]", + "gauntlets of [gauntlet-type]", + "gloves of [glove-type]", + "[glove-type] gloves"); + addRule("[gauntlet-type]", + "dexterity", "power"); + addRule("[glove-type]", + "pushing", "choking", "bigby", "stunning"); + } + + private void addMiniWeaponRules() { + addRule("[mini-weapon]", + "minature [weapon-type]", "small [weapon-type]", + "tiny [weapon-type]", "[sling-type] sling", + "[weapon-type]"); + addRule("[weapon-type]", + "boomerang", "arrow", "net", + "catapult", "hammer", "sword", "club"); + addRule("[sling-type]", + "seeking"); + } + + private void addRockRules() { + addRule("[rock]", + "[pebble-type] pebble", "stone of [stone-type]", + "[stone-type] stone", "brick of [brick-type]", + "[geode-type] geode"); + addRule("[pebble-type]", + "inscribed", "elemental control"); + addRule("[stone-type]", + "good-luck", "weight", + "blind-defense", "metal-clinging"); + addRule("[brick-type]", + "flying"); + addRule("[geode-type]", + "ioun"); + } + + private void addRopeRules() { + addRule("[rope]", + "[rope-type] rope", "rope of [rope-type]", + "ball of [string-type] [string-kind]"); + addRule("[rope-type]", + "trick", "entangling", "climbing", "dancing", + "tripping", "snaring", "levitating", "self-entangling"); + addRule("[string-type]", + "endless"); + addRule("[string-kind]", + "string", "yarn"); + } + + private void addRule(String rule, String... cases) { + ListEx> cses = new FunctionalList<>(); + + for (String strang : cases) { + cses.add(FunctionalStringTokenizer.fromString(strang).toList(s -> s)); + } + + grammar.makeRule(rule, cses); + } + + private void addVialRules() { + addRule("[vial]", + "vial of [vial-type]", "[vial-type] vial", + "[bottle-type] bottle", "[flask-type] flask"); + addRule("[vial-type]", + "holding", "trapping", + "experience", "unnatural regeneration"); + addRule("[bottle-type]", + "ever-smoking", "wheezing", + "blank potion"); + addRule("[flask-type]", + "iron"); + } + + private void addWandRules() { + addRule("[wand]", + "[wand-type] wand", "wand of [wand-type]", + "canceling [wand-type] wand"); + addRule("[wand-type]", + "magic missile", "[spell-1]", "[spell-2]", + "gusting", "life-detecting", "zadron"); + addRule("[spell-1]", + "frost", "fire", "lightning", "fear", + "illumination", "polymorphing", "conjuration", "paralyzing"); + addRule("[spell-2]", + "[spell2-type] detecting"); + addRule("[spell2-type]", + "magic", "enemy", "secret door/trap"); + } +} diff --git a/base/src/examples/java/bjc/utils/examples/rangen/DiabloItemGen.java b/base/src/examples/java/bjc/utils/examples/rangen/DiabloItemGen.java deleted file mode 100644 index 784ff03..0000000 --- a/base/src/examples/java/bjc/utils/examples/rangen/DiabloItemGen.java +++ /dev/null @@ -1,96 +0,0 @@ -package bjc.utils.examples.rangen; - -import bjc.funcdata.FunctionalStringTokenizer; -import bjc.funcdata.ListEx; -import bjc.utils.gen.WeightedGrammar; - -/** - * Example showing how to use the weighted random number generator. - * - * @author ben - * - */ -public class DiabloItemGen { - private static WeightedGrammar rules = new WeightedGrammar<>(); - - private static void addCase(final String ruleName, final int probability, - final String ruleParts) { - final ListEx parts = FunctionalStringTokenizer.fromString(ruleParts) - .toList(strang -> strang); - - rules.addCase(ruleName, probability, parts); - } - - private static void addInfixRules() { - final String rn = ""; - - addCase(rn, 60, "sword"); - addCase(rn, 50, "armor"); - addCase(rn, 40, "rune"); - addCase(rn, 30, "scroll"); - addCase(rn, 20, "potion"); - addCase(rn, 10, "helm"); - } - - private static void addItemRules() { - final String rn = ""; - - addCase(rn, 10, ""); - addCase(rn, 20, " "); - addCase(rn, 30, " "); - addCase(rn, 40, " "); - addCase(rn, 50, " "); - addCase(rn, 60, " "); - } - - private static void addPrefixRules() { - final String rn = ""; - - addCase(rn, 60, "sturdy"); - addCase(rn, 50, "fine"); - addCase(rn, 40, "strong"); - addCase(rn, 30, "azure"); - addCase(rn, 20, "crimson"); - addCase(rn, 10, "phasing"); - } - - private static void addSuffixRules() { - final String rn = ""; - - addCase(rn, 60, "of Health"); - addCase(rn, 50, "of Wealth"); - addCase(rn, 40, "of Life"); - addCase(rn, 30, "of the Jackal"); - addCase(rn, 20, "of Vitality"); - addCase(rn, 10, "of Ability"); - } - - /** - * Main Method - * - * @param args - * Unused CLI args - */ - public static void main(final String[] args) { - rules.addRule(""); - addItemRules(); - - rules.addRule(""); - addSuffixRules(); - - rules.addRule(""); - addPrefixRules(); - - rules.addRule(""); - addInfixRules(); - - for (int i = 0; i < 100; i++) { - final ListEx ls = rules.generateListValues("", " "); - - final StringBuilder sb = new StringBuilder(); - ls.forEach(sb::append); - - System.out.println(sb.toString().replaceAll("\\s+", " ")); - } - } -} diff --git a/base/src/examples/java/bjc/utils/examples/rangen/RandomStringExamples.java b/base/src/examples/java/bjc/utils/examples/rangen/RandomStringExamples.java deleted file mode 100644 index 8458de7..0000000 --- a/base/src/examples/java/bjc/utils/examples/rangen/RandomStringExamples.java +++ /dev/null @@ -1,67 +0,0 @@ -package bjc.utils.examples.rangen; - -import bjc.funcdata.FunctionalList; -import bjc.funcdata.FunctionalStringTokenizer; -import bjc.funcdata.ListEx; -import bjc.utils.gen.RandomGrammar; - -/** - * Examples of random grammar - * - * @author ben - * - */ -public class RandomStringExamples { - private static RandomGrammar rg; - - private static void addRule(final String rule, final String... cases) { - final ListEx> cses = new FunctionalList<>(); - - for (final String strang : cases) { - final ListEx lst - = FunctionalStringTokenizer.fromString(strang).toList(s -> s); - - cses.add(lst); - } - - rg.makeRule(rule, cses); - } - - /** - * Main method - * - * @param args - * Unused CLI args - */ - public static void main(final String[] args) { - rg = new RandomGrammar<>(); - - addRule("", " ", - " thinks that I am ", "I ", - "You think that I am "); - - addRule("", "dancing", "eating", "sleeping"); - - addRule("", "", "life", "my computer", "my friends"); - - addRule("", "hate", "am jealous of", "love"); - - addRule("", "hates", "loves"); - - addRule("", "my sister", "my father", "my girlfriend", - "the man next door"); - - addRule("", "creative", "intelligent"); - - addRule("", "", " with ", ""); - - for (int i = 0; i < 10; i++) { - final ListEx ls = rg.generateListValues("", " "); - - final StringBuilder sb = new StringBuilder(); - ls.forEach(sb::append); - - System.out.println(sb.toString().replaceAll("\\s+", " ")); - } - } -} -- cgit v1.2.3 From ca94c31fb77450b644e87c6cc87fdeb3d584d27d Mon Sep 17 00:00:00 2001 From: Ben Culkin Date: Sat, 13 Mar 2021 07:36:44 -0500 Subject: Remove un-implemented LineReader class Not sure what this was originally intended to be, but the LineNumberReader class from java.io looks like it will work for what I generally want --- base/src/main/java/bjc/utils/ioutils/LineReader.java | 18 ------------------ 1 file changed, 18 deletions(-) delete mode 100644 base/src/main/java/bjc/utils/ioutils/LineReader.java (limited to 'base/src') diff --git a/base/src/main/java/bjc/utils/ioutils/LineReader.java b/base/src/main/java/bjc/utils/ioutils/LineReader.java deleted file mode 100644 index 2ac2797..0000000 --- a/base/src/main/java/bjc/utils/ioutils/LineReader.java +++ /dev/null @@ -1,18 +0,0 @@ -package bjc.utils.ioutils; - -/** - * A line reader - * - * @author bjculkin - * - */ -public class LineReader implements AutoCloseable { - //private Scanner scn; - - @Override - public void close() { - //scn.close(); - } - - // @TODO Implement me - ben, 1/6/20 -} -- cgit v1.2.3 From 89ecc3004f9f513d249d9df9b2e48e08fd56f315 Mon Sep 17 00:00:00 2001 From: Ben Culkin Date: Sat, 13 Mar 2021 08:16:49 -0500 Subject: Clear up some warnings --- base/src/main/java/bjc/utils/ioutils/SimpleProperties.java | 12 ++++++++---- .../main/java/bjc/utils/patterns/MutablePatternMatcher.java | 3 ++- 2 files changed, 10 insertions(+), 5 deletions(-) (limited to 'base/src') diff --git a/base/src/main/java/bjc/utils/ioutils/SimpleProperties.java b/base/src/main/java/bjc/utils/ioutils/SimpleProperties.java index 754ed45..d380866 100644 --- a/base/src/main/java/bjc/utils/ioutils/SimpleProperties.java +++ b/base/src/main/java/bjc/utils/ioutils/SimpleProperties.java @@ -170,17 +170,20 @@ public class SimpleProperties implements Map { return props.isEmpty(); } - @Override + @SuppressWarnings("unlikely-arg-type") + @Override public boolean containsKey(final Object key) { return props.containsKey(key); } - @Override + @SuppressWarnings("unlikely-arg-type") + @Override public boolean containsValue(final Object value) { return props.containsValue(value); } - @Override + @SuppressWarnings("unlikely-arg-type") + @Override public String get(final Object key) { return props.get(key); } @@ -190,7 +193,8 @@ public class SimpleProperties implements Map { return props.put(key, value); } - @Override + @SuppressWarnings("unlikely-arg-type") + @Override public String remove(final Object key) { return props.remove(key); } diff --git a/base/src/main/java/bjc/utils/patterns/MutablePatternMatcher.java b/base/src/main/java/bjc/utils/patterns/MutablePatternMatcher.java index 28e9cd7..176f588 100644 --- a/base/src/main/java/bjc/utils/patterns/MutablePatternMatcher.java +++ b/base/src/main/java/bjc/utils/patterns/MutablePatternMatcher.java @@ -79,7 +79,8 @@ public class MutablePatternMatcher * * @return Whether or not the pattern was removed. */ - public boolean removePattern(ComplexPattern pattern) { + @SuppressWarnings("unlikely-arg-type") + public boolean removePattern(ComplexPattern pattern) { return patterns.remove(pattern); } } -- cgit v1.2.3 From 58e0022923d573782f5db1f22a1713a98712e37b Mon Sep 17 00:00:00 2001 From: Ben Culkin Date: Sat, 13 Mar 2021 10:13:12 -0500 Subject: Update documentation --- .../main/java/bjc/utils/funcutils/LambdaLock.java | 12 ++++++---- .../main/java/bjc/utils/funcutils/ListUtils.java | 25 +++++--------------- .../main/java/bjc/utils/funcutils/SetUtils.java | 8 +++---- .../main/java/bjc/utils/funcutils/TestUtils.java | 27 ++++++++++------------ .../main/java/bjc/utils/funcutils/TreeUtils.java | 7 +++--- .../bjc/utils/patterns/SimplePatternMatcher.java | 1 + 6 files changed, 33 insertions(+), 47 deletions(-) (limited to 'base/src') diff --git a/base/src/main/java/bjc/utils/funcutils/LambdaLock.java b/base/src/main/java/bjc/utils/funcutils/LambdaLock.java index f3637f9..c7ba09a 100644 --- a/base/src/main/java/bjc/utils/funcutils/LambdaLock.java +++ b/base/src/main/java/bjc/utils/funcutils/LambdaLock.java @@ -36,8 +36,9 @@ public class LambdaLock { /** * Execute an action with the read lock taken. * - * @param supp - * The action to call. + * @param The type returned by the action. + * + * @param supp The action to call. * * @return The result of the action. */ @@ -53,9 +54,10 @@ public class LambdaLock { /** * Execute an action with the write lock taken. - * - * @param supp - * The action to call. + * + * @param The type returned by the action. + * + * @param supp The action to call. * * @return The result of the action. */ diff --git a/base/src/main/java/bjc/utils/funcutils/ListUtils.java b/base/src/main/java/bjc/utils/funcutils/ListUtils.java index 17642ce..ab32ccc 100644 --- a/base/src/main/java/bjc/utils/funcutils/ListUtils.java +++ b/base/src/main/java/bjc/utils/funcutils/ListUtils.java @@ -1,9 +1,6 @@ package bjc.utils.funcutils; -import java.util.ArrayList; -import java.util.Iterator; -import java.util.LinkedList; -import java.util.List; +import java.util.*; import java.util.function.*; import bjc.funcdata.FunctionalList; @@ -321,8 +318,9 @@ public class ListUtils { * * This is a version of Algorith P (Plain Changes) from Knuth (vol 4A, pg 322) * - * @param list - * The list to generate permutations from. + * @param The type of elements in the list. + * + * @param list The list to generate permutations from. * @return The list of permutations of the list. */ public static List> permuteList(List list) { @@ -343,19 +341,8 @@ public class ListUtils { T elm1 = list.get(0); T elm2 = list.get(1); - List currPerm = new ArrayList<>(2); - - currPerm.add(elm1); - currPerm.add(elm2); - - permutes.add(currPerm); - - currPerm = new ArrayList<>(2); - - currPerm.add(elm2); - currPerm.add(elm1); - - permutes.add(currPerm); + permutes.add(Arrays.asList(elm1, elm2)); + permutes.add(Arrays.asList(elm2, elm1)); return permutes; } diff --git a/base/src/main/java/bjc/utils/funcutils/SetUtils.java b/base/src/main/java/bjc/utils/funcutils/SetUtils.java index babdb8e..b721d10 100644 --- a/base/src/main/java/bjc/utils/funcutils/SetUtils.java +++ b/base/src/main/java/bjc/utils/funcutils/SetUtils.java @@ -15,8 +15,8 @@ public class SetUtils { /** * Create a power-set (set of all subsets) of a given set. * - * @param originalSet - * The set to create a power-set of. + * @param The type of elements contained in the set. + * @param originalSet The set to create a power-set of. * @return The power-set of the set. */ public static Set> powerSet(Set originalSet) { @@ -55,8 +55,8 @@ public class SetUtils { /** * Utility method for set construction. * - * @param elms - * The elements to stick in the set. + * @param The type of elements in the set. + * @param elms The elements to stick in the set. * @return A set containing the specified elements. */ @SafeVarargs diff --git a/base/src/main/java/bjc/utils/funcutils/TestUtils.java b/base/src/main/java/bjc/utils/funcutils/TestUtils.java index c7aeec1..bf38cbe 100644 --- a/base/src/main/java/bjc/utils/funcutils/TestUtils.java +++ b/base/src/main/java/bjc/utils/funcutils/TestUtils.java @@ -15,10 +15,9 @@ public class TestUtils { /** * Assert an iterator provides a particular sequence of values. * - * @param src - * The iterator to pull values from. - * @param vals - * The values to expect from the iterator. + * @param The type of the values. + * @param src The iterator to pull values from. + * @param vals The values to expect from the iterator. */ @SafeVarargs public static void assertIteratorEquals(Iterator src, T... vals) { @@ -28,12 +27,10 @@ public class TestUtils { /** * Assert an iterator provides a particular sequence of values. * - * @param src - * The iterator to pull values from. - * @param hasMore - * The expected value of hasNext for the iterator. - * @param vals - * The values to expect from the iterator. + * @param The type of the values. + * @param src The iterator to pull values from. + * @param hasMore The expected value of hasNext for the iterator. + * @param vals The values to expect from the iterator. */ @SafeVarargs public static void assertIteratorEquals(boolean hasMore, Iterator src, @@ -44,6 +41,8 @@ public class TestUtils { * Even though it's awkward, the boolean has to come first. Otherwise, there are * cases where the compiler will get confused as to what the right value for T * is, and be unable to pick an overload. + * + * This is a case where named parameter would be rather useful. */ assertIteratorEquals(src, vals); @@ -53,11 +52,9 @@ public class TestUtils { /** * Assert that a list has a given set of contents. * - * @param src - * The list of actual elements. - * - * @param exps - * The list of expected elements. + * @param The type of the values. + * @param src The list of actual elements. + * @param exps The list of expected elements. */ @SafeVarargs public static void assertListEquals(List src, T... exps) { diff --git a/base/src/main/java/bjc/utils/funcutils/TreeUtils.java b/base/src/main/java/bjc/utils/funcutils/TreeUtils.java index 41a01d8..5a1d9c8 100644 --- a/base/src/main/java/bjc/utils/funcutils/TreeUtils.java +++ b/base/src/main/java/bjc/utils/funcutils/TreeUtils.java @@ -15,10 +15,9 @@ public class TreeUtils { /** * Convert a tree into a list of outline nodes that match a certain path. * - * @param tre - * The tree to outline. - * @param leafMarker - * The path to mark nodes with. + * @param The type of the values. + * @param tre The tree to outline. + * @param leafMarker The path to mark nodes with. * @return The list of marked paths. */ public static ListEx> outlineTree(Tree tre, Predicate leafMarker) { diff --git a/base/src/main/java/bjc/utils/patterns/SimplePatternMatcher.java b/base/src/main/java/bjc/utils/patterns/SimplePatternMatcher.java index fea947a..9fae976 100644 --- a/base/src/main/java/bjc/utils/patterns/SimplePatternMatcher.java +++ b/base/src/main/java/bjc/utils/patterns/SimplePatternMatcher.java @@ -7,6 +7,7 @@ import bjc.data.*; * * @author Ben Culkin * + * @param The type input to the pattern matcher. * @param The type returned by the pattern. */ public class SimplePatternMatcher -- cgit v1.2.3 From aba8e262d0e89ebef4229ad807b67660fc9bef35 Mon Sep 17 00:00:00 2001 From: Ben Culkin Date: Sun, 11 Apr 2021 10:51:42 -0400 Subject: Tweak a few things --- .../main/java/bjc/utils/funcutils/StringUtils.java | 175 +++++++-------------- .../main/java/bjc/utils/ioutils/LevelSplitter.java | 49 +++--- .../bjc/utils/test/ioutils/LevelSplitterTest.java | 12 +- 3 files changed, 86 insertions(+), 150 deletions(-) (limited to 'base/src') diff --git a/base/src/main/java/bjc/utils/funcutils/StringUtils.java b/base/src/main/java/bjc/utils/funcutils/StringUtils.java index b029e1d..0b57e18 100644 --- a/base/src/main/java/bjc/utils/funcutils/StringUtils.java +++ b/base/src/main/java/bjc/utils/funcutils/StringUtils.java @@ -8,21 +8,15 @@ import com.ibm.icu.text.BreakIterator; import bjc.utils.ioutils.LevelSplitter; -/** - * Utility methods for operations on strings. +/** Utility methods for operations on strings. * - * @author ben - */ + * @author ben */ public class StringUtils { - /** - * Check if a string consists only of one or more matches of a regular + /**Check if a string consists only of one or more matches of a regular * expression. * - * @param input - * The string to check. - * - * @param rRegex - * The regex to see if the string only contains matches of. + * @param input The string to check. + * @param rRegex The regex to see if the string only contains matches of. * * @return Whether or not the string consists only of multiple matches of the * provided regex. @@ -31,38 +25,29 @@ public class StringUtils { if (input == null) throw new NullPointerException("Input must not be null"); else if (rRegex == null) throw new NullPointerException("Regex must not be null"); - /* - * This regular expression is fairly simple. + /* This regular expression is fairly simple. * * First, we match the beginning of the string. Then, we start a non-capturing * group whose contents are the passed in regex. That group is then matched one - * or more times and the pattern matches to the end of the string. - */ + * or more times and the pattern matches to the end of the string. */ return input.matches("\\A(?:" + rRegex + ")+\\Z"); } - /** - * Indent the string being built in a StringBuilder n levels. - * - * @param builder - * The builder to indent in. + /** Indent the string being built in a StringBuilder n levels. * - * @param levels - * The number of levels to indent. + * @param builder The builder to indent in. + * @param levels The number of levels to indent. */ public static void indentNLevels(final StringBuilder builder, final int levels) { for (int i = 0; i < levels; i++) builder.append("\t"); } - /** - * Print out a deque with a special case for easily showing a deque is empty. - * - * @param - * The type in the deque. + /** Print out a deque with a special case for easily showing a deque is empty. * - * @param queue - * The deque to print. + * @param The type in the deque. * + * @param queue The deque to print. + * * @return A string version of the deque, with allowance for an empty deque. */ public static String printDeque(final Deque queue) { @@ -72,15 +57,10 @@ public class StringUtils { /** * Converts a sequence to an English list. * - * @param objects - * The sequence to convert to an English list. - * + * @param objects The sequence to convert to an English list. * @param join - * The string to use for separating the last element from the - * rest. - * - * @param comma - * The string to use as a comma + * The string to use for separating the last element from the rest. + * @param comma The string to use as a comma * * @return The sequence as an English list. */ @@ -129,15 +109,11 @@ public class StringUtils { return sb.toString(); } - /** - * Converts a sequence to an English list. - * - * @param objects - * The sequence to convert to an English list. + /** Converts a sequence to an English list. * + * @param objects The sequence to convert to an English list. * @param join - * The string to use for separating the last element from the - * rest. + * The string to use for separating the last element from the rest. * * @return The sequence as an English list. */ @@ -145,14 +121,10 @@ public class StringUtils { return toEnglishList(objects, join, ","); } - /** - * Converts a sequence to an English list. - * - * @param objects - * The sequence to convert to an English list. + /** Converts a sequence to an English list. * - * @param and - * Whether to use 'and' or 'or'. + * @param objects The sequence to convert to an English list. + * @param and Whether to use 'and' or 'or'. * * @return The sequence as an English list. */ @@ -161,11 +133,9 @@ public class StringUtils { else return toEnglishList(objects, "or"); } - /** - * Count the number of graphemes in a string. + /** Count the number of graphemes in a string. * - * @param value - * The string to check. + * @param value The string to check. * * @return The number of graphemes in the string. */ @@ -179,14 +149,11 @@ public class StringUtils { return count; } - /** - * Count the number of times a pattern matches in a given string. + /** Count the number of times a pattern matches in a given string. * - * @param value - * The string to count occurances in. - * - * @param pattern - * The pattern to count occurances of. + * @param value The string to count occurrences in. + * @param pattern The pattern to count occurrences of. + * * @return The number of times the pattern matches. */ public static int countMatches(final String value, final String pattern) { @@ -198,42 +165,36 @@ public class StringUtils { return num; } - /** - * Get a substring until a specified string. + /** Get a substring until a specified string. * - * @param strang - * The string to substring. - * @param vx - * The place to substring until. + * @param strang The string to substring. + * @param until The place to substring until. * * @return The specified substring. */ - public static String substringTo(String strang, String vx) { - return substringTo(strang, vx, true); + public static String substringTo(String strang, String until) { + return substringTo(strang, until, true); } /** * Get a substring until a specified string. * - * @param strang - * The string to substring. - * @param vx - * The place to substring until. - * @param allowFail - * Whether or not to allow failure. + * @param strang The string to substring. + * @param until The place to substring until. + * @param allowFail Whether or not to allow failure. * * @return The specified substring, or null if the specified place to substring * to was not found, and we were not allowed to fail. */ - public static String substringTo(String strang, String vx, boolean allowFail) { - int idx = strang.indexOf(vx); + public static String substringTo(String strang, String until, boolean allowFail) { + int idx = strang.indexOf(until); if (idx == -1) { if (allowFail) return strang; else return null; } - return strang.substring(0, strang.indexOf(vx)); + return strang.substring(0, strang.indexOf(until)); } private static class LineIterator implements Iterator { @@ -282,11 +243,9 @@ public class StringUtils { } } - /** - * Read a series of lines from an input source. + /** Read a series of lines from an input source. * - * @param scn - * The source to read the lines from. + * @param scn The source to read the lines from. * * @return An iterator over the lines from the input source. */ @@ -297,17 +256,10 @@ public class StringUtils { /** * Read a series of lines from an input source. * - * @param scn - * The source to read the lines from. - * - * @param processComments - * Whether or not to skip comment lines. - * - * @param commentInd - * Indicator for starting comment lines. - * - * @param skipBlanks - * Whether or not to skip blank lines. + * @param scn The source to read the lines from. + * @param processComments Whether or not to skip comment lines. + * @param commentInd Indicator for starting comment lines. + * @param skipBlanks Whether or not to skip blank lines. * * @return An iterator over the lines from the input source. */ @@ -323,27 +275,23 @@ public class StringUtils { return itr; } - /** - * Check if a string contains any one of a specified number of things, + /** Check if a string contains any one of a specified number of things, * respecting groups. * - * @param haystack - * The string to look in. - * @param needles - * The strings to look for. + * @param haystack The string to look in. + * @param needles The strings to look for. + * * @return Whether or not any of the strings were contained outside of groups. */ public static boolean levelContains(String haystack, String... needles) { return LevelSplitter.def.levelContains(haystack, needles); } - /** - * Split a string, respecting groups. + /** Split a string, respecting groups. * - * @param phrase - * The string to split. - * @param splits - * The strings to split on. + * @param phrase The string to split. + * @param splits The strings to split on. + * * @return A list of split strings. If keepDelims is true, it also includes the * delimiters in between the split strings. */ @@ -351,15 +299,12 @@ public class StringUtils { return LevelSplitter.def.levelSplit(phrase, false, splits); } - /** - * Split a string, respecting groups. + /** Split a string, respecting groups. * - * @param phrase - * The string to split. - * @param keepDelims - * Whether or not to include the delimiters in the results. - * @param splits - * The strings to split on. + * @param phrase The string to split. + * @param keepDelims Whether or not to include the delimiters in the results. + * @param splits The strings to split on. + * * @return A list of split strings. If keepDelims is true, it also includes the * delimiters in between the split strings. */ @@ -367,4 +312,4 @@ public class StringUtils { String... splits) { return LevelSplitter.def.levelSplit(phrase, keepDelims, splits); } -} +} \ No newline at end of file diff --git a/base/src/main/java/bjc/utils/ioutils/LevelSplitter.java b/base/src/main/java/bjc/utils/ioutils/LevelSplitter.java index c2467ae..93bc424 100644 --- a/base/src/main/java/bjc/utils/ioutils/LevelSplitter.java +++ b/base/src/main/java/bjc/utils/ioutils/LevelSplitter.java @@ -5,29 +5,24 @@ import java.util.List; import java.util.regex.Matcher; import java.util.regex.Pattern; -/** - * Splits a string on a delimiter, respecting grouping delimiters. +/** Splits a string on a delimiter, respecting grouping delimiters. * * By default, grouping delimiters are (), [], {}, and <>, as well as single and * double quoted strings. * - * @author bjculkin - * - */ + * @author bjculkin */ public class LevelSplitter { - /** - * Defaultly configured level splitter. - */ + /** Default configured level splitter. */ public final static LevelSplitter def = new LevelSplitter(); - /** - * Check if a string contains any one of a specified number of things, + /** Should empty strings be ignored? */ + public boolean ignoreEmpty = false; + + /** Check if a string contains any one of a specified number of things, * respecting groups. * - * @param haystack - * The string to look in. - * @param needles - * The strings to look for. + * @param haystack The string to look in. + * @param needles The strings to look for. * @return Whether or not any of the strings were contained outside of groups. */ public boolean levelContains(String haystack, String... needles) { @@ -88,13 +83,11 @@ public class LevelSplitter { return false; } - /** - * Split a string, respecting groups. + /** Split a string, respecting groups. * - * @param phrase - * The string to split. - * @param splits - * The strings to split on. + * @param phrase The string to split. + * @param splits The strings to split on. + * * @return A list of split strings. If keepDelims is true, it also includes the * delimiters in between the split strings. */ @@ -102,15 +95,12 @@ public class LevelSplitter { return levelSplit(phrase, false, splits); } - /** - * Split a string, respecting groups. + /** Split a string, respecting groups. * - * @param phrase - * The string to split. - * @param keepDelims - * Whether or not to include the delimiters in the results. - * @param splits - * The strings to split on. + * @param phrase The string to split. + * @param keepDelims Whether or not to include the delimiters in the results. + * @param splits The strings to split on. + * * @return A list of split strings. If keepDelims is true, it also includes the * delimiters in between the split strings. */ @@ -140,8 +130,7 @@ public class LevelSplitter { if (work.regionMatches(i, split, 0, split.length())) { strangs.add(work.substring(0, i)); - if (keepDelims) - strangs.add(split); + if (keepDelims) strangs.add(split); work = work.substring(i + split.length()); i = 0; diff --git a/base/src/test/java/bjc/utils/test/ioutils/LevelSplitterTest.java b/base/src/test/java/bjc/utils/test/ioutils/LevelSplitterTest.java index 5fb3756..b0db059 100644 --- a/base/src/test/java/bjc/utils/test/ioutils/LevelSplitterTest.java +++ b/base/src/test/java/bjc/utils/test/ioutils/LevelSplitterTest.java @@ -3,6 +3,8 @@ package bjc.utils.test.ioutils; import static bjc.utils.funcutils.TestUtils.assertListEquals; import static bjc.utils.test.ioutils.LevelSplitterTest.RXPair.pair; +import java.util.*; + import org.junit.Test; import bjc.utils.ioutils.LevelSplitter; @@ -28,21 +30,21 @@ public class LevelSplitterTest { } } - /** - * Test regex splitter. - */ + /** Test regex splitter. */ @Test public void testRXSplit() { // LevelSplitter splitter = LevelSplitter.def; // Check generic splitting works - assertRXSplit("\\s+", pair("", ""), pair("a", "a"), pair("a b", "a", "b"), + assertRXSplit("\\s+", + pair("", ""), pair("a", "a"), pair("a b", "a", "b"), pair("a b", "a", "b"), pair("a\t \tb", "a", "b")); } private static void assertRXSplit(String pat, RXPair... pairs) { for (RXPair pair : pairs) { - assertListEquals(LevelSplitter.def.levelSplitRX(pair.inp, pat), pair.outp); + List res = LevelSplitter.def.levelSplitRX(pair.inp, pat); + assertListEquals(res, pair.outp); } } } -- cgit v1.2.3