summaryrefslogtreecommitdiff
path: root/base/src/main/java/bjc/utils/gen
diff options
context:
space:
mode:
authorBen Culkin <scorpress@gmail.com>2020-04-13 18:40:41 -0400
committerBen Culkin <scorpress@gmail.com>2020-04-13 18:40:41 -0400
commitd4ca769e542b2489b1e23cfcbdc3a0b7275b87cd (patch)
tree1653a7399f97fb0c63ce62e3f60fd830d5c37f70 /base/src/main/java/bjc/utils/gen
parent2ac2e31a56ae59ee582e43a90c3495f86dd9ee7a (diff)
Cleanup pass
Cleanup pass to uniformize things
Diffstat (limited to 'base/src/main/java/bjc/utils/gen')
-rw-r--r--base/src/main/java/bjc/utils/gen/RandomGrammar.java21
-rw-r--r--base/src/main/java/bjc/utils/gen/WeightedGrammar.java255
-rw-r--r--base/src/main/java/bjc/utils/gen/WeightedRandom.java104
3 files changed, 202 insertions, 178 deletions
diff --git a/base/src/main/java/bjc/utils/gen/RandomGrammar.java b/base/src/main/java/bjc/utils/gen/RandomGrammar.java
index d0a71c5..050165b 100644
--- a/base/src/main/java/bjc/utils/gen/RandomGrammar.java
+++ b/base/src/main/java/bjc/utils/gen/RandomGrammar.java
@@ -9,7 +9,7 @@ import bjc.funcdata.IList;
* @author ben
*
* @param <E>
- * The type of grammar elements to use.
+ * The type of grammar elements to use.
*/
public class RandomGrammar<E> extends WeightedGrammar<E> {
/** Create a new random grammar. */
@@ -21,14 +21,14 @@ public class RandomGrammar<E> extends WeightedGrammar<E> {
* Add cases to a specified rule.
*
* @param rule
- * The name of the rule to add cases to.
+ * The name of the rule to add cases to.
*
* @param cases
- * The cases to add for this rule.
+ * The cases to add for this rule.
*/
@SafeVarargs
public final void addCases(final E rule, final IList<E>... cases) {
- for(final IList<E> currentCase : cases) {
+ for (final IList<E> currentCase : cases) {
super.addCase(rule, 1, currentCase);
}
}
@@ -37,16 +37,16 @@ public class RandomGrammar<E> extends WeightedGrammar<E> {
* Create a rule with the specified name and cases.
*
* @param rule
- * The name of the rule to add.
+ * The name of the rule to add.
*
* @param cases
- * The cases to add for this rule.
+ * The cases to add for this rule.
*/
@SafeVarargs
public final void makeRule(final E rule, final IList<E>... cases) {
super.addRule(rule);
- for(final IList<E> currentCase : cases) {
+ for (final IList<E> currentCase : cases) {
super.addCase(rule, 1, currentCase);
}
}
@@ -55,13 +55,14 @@ public class RandomGrammar<E> extends WeightedGrammar<E> {
* Create a rule with the specified name and cases.
*
* @param rule
- * The name of the rule to add.
+ * The name of the rule to add.
*
* @param cases
- * The cases to add for this rule.
+ * The cases to add for this rule.
*/
public void makeRule(final E rule, final IList<IList<E>> cases) {
- if(cases == null) throw new NullPointerException("Cases must not be null");
+ if (cases == null)
+ throw new NullPointerException("Cases must not be null");
super.addRule(rule);
diff --git a/base/src/main/java/bjc/utils/gen/WeightedGrammar.java b/base/src/main/java/bjc/utils/gen/WeightedGrammar.java
index 774d694..324a80c 100644
--- a/base/src/main/java/bjc/utils/gen/WeightedGrammar.java
+++ b/base/src/main/java/bjc/utils/gen/WeightedGrammar.java
@@ -19,7 +19,7 @@ import bjc.funcdata.IMap;
* @author ben
*
* @param <E>
- * The values that make up sentences of this grammar.
+ * The values that make up sentences of this grammar.
*/
public class WeightedGrammar<E> {
/** The initial rule of the grammar */
@@ -51,16 +51,16 @@ public class WeightedGrammar<E> {
}
/**
- * Create a new weighted grammar that uses the specified source of
- * randomness.
+ * Create a new weighted grammar that uses the specified source of randomness.
*
* @param source
- * The source of randomness to use
+ * The source of randomness to use
*/
public WeightedGrammar(final Random source) {
this();
- if(source == null) throw new NullPointerException("Source of randomness must be non-null");
+ if (source == null)
+ throw new NullPointerException("Source of randomness must be non-null");
rng = source;
}
@@ -69,10 +69,10 @@ public class WeightedGrammar<E> {
* Configure the action to perform on special tokens.
*
* @param marker
- * The marker to find special tokens.
+ * The marker to find special tokens.
*
* @param action
- * The action to take on those tokens.
+ * The action to take on those tokens.
*/
public void configureSpecial(final Predicate<E> marker,
final BiFunction<E, WeightedGrammar<E>, IList<E>> action) {
@@ -84,15 +84,15 @@ public class WeightedGrammar<E> {
* Adds a special rule to the grammar.
*
* @param ruleName
- * The name of the special rule.
+ * The name of the special rule.
*
* @param cse
- * The case for the rule.
+ * The case for the rule.
*/
public void addSpecialRule(final E ruleName, final Supplier<IList<E>> cse) {
- if(ruleName == null) {
+ if (ruleName == null) {
throw new NullPointerException("Rule name must not be null");
- } else if(cse == null) {
+ } else if (cse == null) {
throw new NullPointerException("Case must not be null");
}
@@ -103,18 +103,18 @@ public class WeightedGrammar<E> {
* Add a case to an already existing rule.
*
* @param ruleName
- * The rule to add a case to.
+ * The rule to add a case to.
*
* @param probability
- * The probability for this rule to be chosen.
+ * The probability for this rule to be chosen.
*
* @param cse
- * The case being added.
+ * The case being added.
*/
public void addCase(final E ruleName, final int probability, final IList<E> cse) {
- if(ruleName == null) {
+ if (ruleName == null) {
throw new NullPointerException("Rule name must be not null");
- } else if(cse == null) {
+ } else if (cse == null) {
throw new NullPointerException("Case body must not be null");
}
@@ -125,23 +125,24 @@ public class WeightedGrammar<E> {
* Add a alias for an existing subgrammar.
*
* @param name
- * The name of the subgrammar to alias.
+ * The name of the subgrammar to alias.
*
* @param alias
- * The alias of the subgrammar.
+ * The alias of the subgrammar.
*
* @return Whether the alias was succesfully created.
*/
public boolean addGrammarAlias(final E name, final E alias) {
- if(name == null) {
+ if (name == null) {
throw new NullPointerException("Subgrammar name must not be null");
- } else if(alias == null) {
+ } else if (alias == null) {
throw new NullPointerException("Subgrammar alias must not be null");
}
- if(subgrammars.containsKey(alias)) return false;
+ if (subgrammars.containsKey(alias))
+ return false;
- if(subgrammars.containsKey(name)) {
+ if (subgrammars.containsKey(name)) {
subgrammars.put(alias, subgrammars.get(name));
return true;
}
@@ -153,16 +154,17 @@ public class WeightedGrammar<E> {
* Add a new rule with no cases.
*
* @param name
- * The name of the rule to add.
+ * The name of the rule to add.
*
* @return Whether or not the rule was successfully added.
*/
public boolean addRule(final E name) {
- if(rng == null) {
+ if (rng == null) {
rng = new Random();
}
- if(name == null) throw new NullPointerException("Rule name must not be null");
+ if (name == null)
+ throw new NullPointerException("Rule name must not be null");
return addRule(name, new WeightedRandom<>(rng));
}
@@ -171,21 +173,22 @@ public class WeightedGrammar<E> {
* Add a new rule with a set of cases.
*
* @param name
- * The name of the rule to add.
+ * The name of the rule to add.
*
* @param cases
- * The set of cases for the rule.
+ * The set of cases for the rule.
*
* @return Whether or not the rule was succesfully added.
*/
public boolean addRule(final E name, final WeightedRandom<IList<E>> cases) {
- if(name == null) {
+ if (name == null) {
throw new NullPointerException("Name must not be null");
- } else if(cases == null) {
+ } else if (cases == null) {
throw new NullPointerException("Cases must not be null");
}
- if(rules.containsKey(name)) return false;
+ if (rules.containsKey(name))
+ return false;
rules.put(name, cases);
return true;
@@ -195,21 +198,22 @@ public class WeightedGrammar<E> {
* Add a subgrammar.
*
* @param name
- * The name of the subgrammar.
+ * The name of the subgrammar.
*
* @param subgrammar
- * The subgrammar to add.
+ * The subgrammar to add.
*
* @return Whether or not the subgrammar was succesfully added.
*/
public boolean addSubgrammar(final E name, final WeightedGrammar<E> subgrammar) {
- if(name == null) {
+ if (name == null) {
throw new NullPointerException("Subgrammar name must not be null");
- } else if(subgrammar == null) {
+ } else if (subgrammar == null) {
throw new NullPointerException("Subgrammar must not be null");
}
- if(subgrammars.containsKey(name)) return false;
+ if (subgrammars.containsKey(name))
+ return false;
subgrammars.put(name, subgrammar);
return true;
@@ -219,10 +223,11 @@ public class WeightedGrammar<E> {
* Remove a rule with the specified name.
*
* @param name
- * The name of the rule to remove.
+ * The name of the rule to remove.
*/
public void deleteRule(final E name) {
- if(name == null) throw new NullPointerException("Rule name must not be null");
+ if (name == null)
+ throw new NullPointerException("Rule name must not be null");
rules.remove(name);
}
@@ -231,10 +236,11 @@ public class WeightedGrammar<E> {
* Remove a subgrammar with the specified name.
*
* @param name
- * The name of the subgrammar to remove.
+ * The name of the subgrammar to remove.
*/
public void deleteSubgrammar(final E name) {
- if(name == null) throw new NullPointerException("Rule name must not be null");
+ if (name == null)
+ throw new NullPointerException("Rule name must not be null");
subgrammars.remove(name);
}
@@ -245,18 +251,19 @@ public class WeightedGrammar<E> {
* Only generates sentences one layer deep.
*
* @param ruleName
- * The rule to test.
+ * The rule to test.
*
* @return A set of sentences generated by the specified rule.
*/
public IList<IList<E>> generateDebugValues(final E ruleName) {
- if(ruleName == null) throw new NullPointerException("Rule name must not be null");
+ if (ruleName == null)
+ throw new NullPointerException("Rule name must not be null");
final IList<IList<E>> returnedList = new FunctionalList<>();
final WeightedRandom<IList<E>> ruleGenerator = rules.get(ruleName);
- for(int i = 0; i < 10; i++) {
+ for (int i = 0; i < 10; i++) {
returnedList.add(ruleGenerator.generateValue());
}
@@ -267,27 +274,27 @@ public class WeightedGrammar<E> {
* Generate a generic sentence from a initial rule.
*
* @param <T>
- * The type of the transformed output
+ * The type of the transformed output
*
* @param initRules
- * The initial rule to start with.
+ * The initial rule to start with.
*
* @param tokenTransformer
- * The function to transform grammar output into something.
+ * The function to transform grammar output into
+ * something.
*
* @param spacer
- * The spacer element to add in between output tokens.
+ * The spacer element to add in between output tokens.
*
- * @return A randomly generated sentence from the specified initial
- * rule.
+ * @return A randomly generated sentence from the specified initial rule.
*/
- public <T> IList<T> generateGenericValues(final E initRules, final Function<E, T> tokenTransformer,
- final T spacer) {
- if(initRules == null) {
+ public <T> IList<T> generateGenericValues(final E initRules,
+ final Function<E, T> tokenTransformer, final T spacer) {
+ if (initRules == null) {
throw new NullPointerException("Initial rule must not be null");
- } else if(tokenTransformer == null) {
+ } else if (tokenTransformer == null) {
throw new NullPointerException("Transformer must not be null");
- } else if(spacer == null) {
+ } else if (spacer == null) {
throw new NullPointerException("Spacer must not be null");
}
@@ -295,8 +302,8 @@ public class WeightedGrammar<E> {
IList<E> genRules = new FunctionalList<>(initRules);
- if(specialMarker != null) {
- if(specialMarker.test(initRules)) {
+ if (specialMarker != null) {
+ if (specialMarker.test(initRules)) {
genRules = specialAction.apply(initRules, this);
}
}
@@ -304,33 +311,37 @@ public class WeightedGrammar<E> {
/*
* @NOTE Can this loop be simplified in some way?
*/
- for(final E initRule : genRules.toIterable()) {
- if(specialRules.containsKey(initRule)) {
- for(final E rulePart : specialRules.get(initRule).get().toIterable()) {
- final Iterable<T> generatedRuleParts = generateGenericValues(rulePart,
- tokenTransformer, spacer).toIterable();
-
- for(final T generatedRulePart : generatedRuleParts) {
+ for (final E initRule : genRules.toIterable()) {
+ if (specialRules.containsKey(initRule)) {
+ for (final E rulePart : specialRules.get(initRule).get().toIterable()) {
+ final Iterable<T> generatedRuleParts
+ = generateGenericValues(rulePart, tokenTransformer, spacer)
+ .toIterable();
+
+ for (final T generatedRulePart : generatedRuleParts) {
returnedList.add(generatedRulePart);
returnedList.add(spacer);
}
}
- } else if(subgrammars.containsKey(initRule)) {
+ } else if (subgrammars.containsKey(initRule)) {
final Iterable<T> ruleParts = subgrammars.get(initRule)
- .generateGenericValues(initRule, tokenTransformer, spacer).toIterable();
+ .generateGenericValues(initRule, tokenTransformer, spacer)
+ .toIterable();
- for(final T rulePart : ruleParts) {
+ for (final T rulePart : ruleParts) {
returnedList.add(rulePart);
returnedList.add(spacer);
}
- } else if(rules.containsKey(initRule)) {
- final Iterable<E> ruleParts = rules.get(initRule).generateValue().toIterable();
+ } else if (rules.containsKey(initRule)) {
+ final Iterable<E> ruleParts
+ = rules.get(initRule).generateValue().toIterable();
- for(final E rulePart : ruleParts) {
- final Iterable<T> generatedRuleParts = generateGenericValues(rulePart,
- tokenTransformer, spacer).toIterable();
+ for (final E rulePart : ruleParts) {
+ final Iterable<T> generatedRuleParts
+ = generateGenericValues(rulePart, tokenTransformer, spacer)
+ .toIterable();
- for(final T generatedRulePart : generatedRuleParts) {
+ for (final T generatedRulePart : generatedRuleParts) {
returnedList.add(generatedRulePart);
returnedList.add(spacer);
}
@@ -338,7 +349,7 @@ public class WeightedGrammar<E> {
} else {
final T transformedToken = tokenTransformer.apply(initRule);
- if(transformedToken == null)
+ if (transformedToken == null)
throw new NullPointerException("Transformer created null token");
returnedList.add(transformedToken);
@@ -353,16 +364,16 @@ public class WeightedGrammar<E> {
* Generate a random list of grammar elements from a given initial rule.
*
* @param initRule
- * The initial rule to start with.
+ * The initial rule to start with.
*
* @param spacer
- * The item to use to space the list.
+ * The item to use to space the list.
*
- * @return A list of random grammar elements generated by the specified
- * rule.
+ * @return A list of random grammar elements generated by the specified rule.
*/
public IList<E> generateListValues(final E initRule, final E spacer) {
- final IList<E> retList = generateGenericValues(initRule, strang -> strang, spacer);
+ final IList<E> retList
+ = generateGenericValues(initRule, strang -> strang, spacer);
return retList;
}
@@ -403,12 +414,13 @@ public class WeightedGrammar<E> {
* Get the subgrammar with the specified name.
*
* @param name
- * The name of the subgrammar to get.
+ * The name of the subgrammar to get.
*
* @return The subgrammar with the specified name.
*/
public WeightedGrammar<E> getSubgrammar(final E name) {
- if(name == null) throw new NullPointerException("Subgrammar name must not be null");
+ if (name == null)
+ throw new NullPointerException("Subgrammar name must not be null");
return subgrammars.get(name);
}
@@ -426,7 +438,7 @@ public class WeightedGrammar<E> {
* Check if this grammar has a given rule.
*
* @param ruleName
- * The rule to check for.
+ * The rule to check for.
*
* @return Whether or not the grammar has a rule by that name.
*/
@@ -438,25 +450,26 @@ public class WeightedGrammar<E> {
* Prefix a given rule with a token multiple times.
*
* @param ruleName
- * The name of the rule to prefix.
+ * The name of the rule to prefix.
*
* @param prefixToken
- * The token to prefix to the rules.
+ * The token to prefix to the rules.
*
* @param additionalProbability
- * The additional probability of the tokens.
+ * The additional probability of the tokens.
*
* @param numberOfTimes
- * The number of times to prefix the token.
+ * The number of times to prefix the token.
*/
- public void multiPrefixRule(final E ruleName, final E prefixToken, final int additionalProbability,
- final int numberOfTimes) {
- if(ruleName == null)
+ public void multiPrefixRule(final E ruleName, final E prefixToken,
+ final int additionalProbability, final int numberOfTimes) {
+ if (ruleName == null)
throw new NullPointerException("Rule name must not be null");
- else if(prefixToken == null)
+ else if (prefixToken == null)
throw new NullPointerException("Prefix token must not be null");
- else if(numberOfTimes < 1)
- throw new IllegalArgumentException("Number of times to prefix must be positive.");
+ else if (numberOfTimes < 1)
+ throw new IllegalArgumentException(
+ "Number of times to prefix must be positive.");
final WeightedRandom<IList<E>> rule = rules.get(ruleName);
@@ -465,21 +478,21 @@ public class WeightedGrammar<E> {
/*
* @NOTE Can this be simplified?
*/
- rule.getValues().forEach((pair) -> {
+ rule.getValues().forEach(pair -> {
final IList<IList<E>> newRule = new FunctionalList<>();
- for(int i = 1; i <= numberOfTimes; i++) {
+ for (int i = 1; i <= numberOfTimes; i++) {
final IList<E> newCase = pair.merge((left, right) -> {
final IList<E> returnVal = new FunctionalList<>();
- for(final E val : right.toIterable()) {
+ for (final E val : right.toIterable()) {
returnVal.add(val);
}
return returnVal;
});
- for(int j = 1; j <= i; j++) {
+ for (int j = 1; j <= i; j++) {
newCase.prepend(prefixToken);
}
@@ -493,7 +506,7 @@ public class WeightedGrammar<E> {
});
});
- newResults.forEach((pair) -> {
+ newResults.forEach(pair -> {
pair.doWith((left, right) -> {
addCase(ruleName, left, right);
});
@@ -501,22 +514,23 @@ public class WeightedGrammar<E> {
}
/**
- * Create a series of alternatives for a rule by prefixing them with a
- * given token.
+ * Create a series of alternatives for a rule by prefixing them with a given
+ * token.
*
* @param additionalProbability
- * The amount to adjust the probability by.
+ * The amount to adjust the probability by.
*
* @param ruleName
- * The name of the rule to prefix.
+ * The name of the rule to prefix.
*
* @param prefixToken
- * The token to prefix to the rule.
+ * The token to prefix to the rule.
*/
- public void prefixRule(final E ruleName, final E prefixToken, final int additionalProbability) {
- if(ruleName == null) {
+ public void prefixRule(final E ruleName, final E prefixToken,
+ final int additionalProbability) {
+ if (ruleName == null) {
throw new NullPointerException("Rule name must not be null");
- } else if(prefixToken == null) {
+ } else if (prefixToken == null) {
throw new NullPointerException("Prefix token must not be null");
}
@@ -524,11 +538,11 @@ public class WeightedGrammar<E> {
final IList<IPair<Integer, IList<E>>> newResults = new FunctionalList<>();
- rule.getValues().forEach((pair) -> {
+ rule.getValues().forEach(pair -> {
final IList<E> newCase = pair.merge((left, right) -> {
final IList<E> returnVal = new FunctionalList<>();
- for(final E val : right.toIterable()) {
+ for (final E val : right.toIterable()) {
returnVal.add(val);
}
@@ -537,17 +551,19 @@ public class WeightedGrammar<E> {
newCase.prepend(prefixToken);
- newResults.add(new Pair<>(pair.merge((left, right) -> left) + additionalProbability, newCase));
+ newResults.add(new Pair<>(
+ pair.merge((left, right) -> left) + additionalProbability, newCase));
});
- newResults.forEach((pair) -> pair.doWith((left, right) -> addCase(ruleName, left, right)));
+ newResults.forEach(
+ pair -> pair.doWith((left, right) -> addCase(ruleName, left, right)));
}
/**
* Set the initial rule of the grammar.
*
* @param initRule
- * The initial rule of this grammar.
+ * The initial rule of this grammar.
*/
public void setInitialRule(final String initRule) {
this.initialRule = initRule;
@@ -557,18 +573,19 @@ public class WeightedGrammar<E> {
* Suffix a token to a rule.
*
* @param ruleName
- * The rule to suffix.
+ * The rule to suffix.
*
* @param suffixToken
- * The token to prefix to the rule.
+ * The token to prefix to the rule.
*
* @param additionalProbability
- * Additional probability of the prefixed rule.
+ * Additional probability of the prefixed rule.
*/
- public void suffixRule(final E ruleName, final E suffixToken, final int additionalProbability) {
- if(ruleName == null) {
+ public void suffixRule(final E ruleName, final E suffixToken,
+ final int additionalProbability) {
+ if (ruleName == null) {
throw new NullPointerException("Rule name must not be null");
- } else if(suffixToken == null) {
+ } else if (suffixToken == null) {
throw new NullPointerException("Prefix token must not be null");
}
@@ -576,11 +593,11 @@ public class WeightedGrammar<E> {
final IList<IPair<Integer, IList<E>>> newResults = new FunctionalList<>();
- rule.getValues().forEach((par) -> {
+ rule.getValues().forEach(par -> {
final IList<E> newCase = par.merge((left, right) -> {
final IList<E> returnVal = new FunctionalList<>();
- for(final E val : right.toIterable()) {
+ for (final E val : right.toIterable()) {
returnVal.add(val);
}
@@ -589,9 +606,11 @@ public class WeightedGrammar<E> {
newCase.add(suffixToken);
- newResults.add(new Pair<>(par.merge((left, right) -> left) + additionalProbability, newCase));
+ newResults.add(new Pair<>(
+ par.merge((left, right) -> left) + additionalProbability, newCase));
});
- newResults.forEach((pair) -> pair.doWith((left, right) -> addCase(ruleName, left, right)));
+ newResults.forEach(
+ pair -> pair.doWith((left, right) -> addCase(ruleName, left, right)));
}
}
diff --git a/base/src/main/java/bjc/utils/gen/WeightedRandom.java b/base/src/main/java/bjc/utils/gen/WeightedRandom.java
index 15f5821..b17017b 100644
--- a/base/src/main/java/bjc/utils/gen/WeightedRandom.java
+++ b/base/src/main/java/bjc/utils/gen/WeightedRandom.java
@@ -14,7 +14,7 @@ import bjc.funcdata.IList;
* @author ben
*
* @param <E>
- * The type of values that are randomly selected.
+ * The type of values that are randomly selected.
*/
public class WeightedRandom<E> {
private final IList<IPair<Integer, E>> values;
@@ -33,12 +33,13 @@ public class WeightedRandom<E> {
* randomness.
*
* @param src
- * The source of randomness to use.
+ * The source of randomness to use.
*/
public WeightedRandom(Random src) {
values = new FunctionalList<>();
- if (src == null) throw new NullPointerException("Source of randomness must not be null");
+ if (src == null)
+ throw new NullPointerException("Source of randomness must not be null");
source = src;
}
@@ -62,10 +63,10 @@ public class WeightedRandom<E> {
* Add a probability for a specific result to be given.
*
* @param chance
- * The chance to get this result.
+ * The chance to get this result.
*
* @param result
- * The result to get when the chance comes up.
+ * The result to get when the chance comes up.
*/
public void addProbability(final int chance, final E result) {
values.add(new Pair<>(chance, result));
@@ -84,9 +85,9 @@ public class WeightedRandom<E> {
/**
* Generate a random value, using the specified Random.
- *
+ *
* @param rn
- * The Random instance to use.
+ * The Random instance to use.
* @return A random value.
*/
public E generateValue(Random rn) {
@@ -130,13 +131,12 @@ public class WeightedRandom<E> {
/**
* Get a descending value.
- *
- * Descending values are quite simple. You have a 1 in factor chance to
- * advance to the next value, otherwise, the current value is the one
- * returned.
- *
+ *
+ * Descending values are quite simple. You have a 1 in factor chance to advance
+ * to the next value, otherwise, the current value is the one returned.
+ *
* @param factor
- * The descent factor to use.
+ * The descent factor to use.
* @return A random value.
*/
public E getDescent(int factor) {
@@ -145,22 +145,23 @@ public class WeightedRandom<E> {
/**
* Get a descending value.
- *
- * Descending values are quite simple. You have a 1 in factor chance to
- * advance to the next value, otherwise, the current value is the one
- * returned.
- *
+ *
+ * Descending values are quite simple. You have a 1 in factor chance to advance
+ * to the next value, otherwise, the current value is the one returned.
+ *
* @param factor
- * The descent factor to use.
+ * The descent factor to use.
* @param rn
- * The Random instance to use.
+ * The Random instance to use.
* @return A random value.
*/
public E getDescent(int factor, Random rn) {
- if (values.getSize() == 0) return null;
+ if (values.getSize() == 0)
+ return null;
for (IPair<Integer, E> val : values) {
- if (rn.nextInt(factor) == 0) continue;
+ if (rn.nextInt(factor) == 0)
+ continue;
if (exhaust) {
totalChance -= val.getLeft();
@@ -172,25 +173,25 @@ public class WeightedRandom<E> {
}
IPair<Integer, E> val = values.getByIndex(values.getSize() - 1);
- if (exhaust) values.removeMatching(val);
+ if (exhaust)
+ values.removeMatching(val);
return val.getRight();
}
/**
* Get a value, treating this as a set of binomial trials.
- *
- * Essentially, the system rolls a bound-sided dice trials times, and
- * marks a success for every roll less than or equal to target.
- *
+ *
+ * Essentially, the system rolls a bound-sided dice trials times, and marks a
+ * success for every roll less than or equal to target.
+ *
* @param target
- * The number to roll at or under.
+ * The number to roll at or under.
* @param bound
- * The maximum roll value.
+ * The maximum roll value.
* @param trials
- * The number of times to roll.
- * @return The value at the index corresponding to the number of
- * successes.
+ * The number of times to roll.
+ * @return The value at the index corresponding to the number of successes.
*/
public E getBinomial(int target, int bound, int trials) {
return getBinomial(target, bound, trials, source);
@@ -198,41 +199,43 @@ public class WeightedRandom<E> {
/**
* Get a value, treating this as a set of binomial trials.
- *
- * Essentially, the system rolls a bound-sided dice trials times, and
- * marks a success for every roll less than or equal to target.
- *
+ *
+ * Essentially, the system rolls a bound-sided dice trials times, and marks a
+ * success for every roll less than or equal to target.
+ *
* @param target
- * The number to roll at or under.
+ * The number to roll at or under.
* @param bound
- * The maximum roll value.
+ * The maximum roll value.
* @param trials
- * The number of times to roll.
+ * The number of times to roll.
* @param rn
- * The Random instance to use.
- * @return The value at the index corresponding to the number of
- * successes.
+ * The Random instance to use.
+ * @return The value at the index corresponding to the number of successes.
*/
public E getBinomial(int target, int bound, int trials, Random rn) {
- if (values.getSize() == 0) return null;
+ if (values.getSize() == 0)
+ return null;
int numSuc = 0;
for (int i = 0; i < trials; i++) {
/*
- * Adjust for zero, because it's easy to think of this
- * as rolling a bound-sided dice and marking a success
- * for every roll less than or equal to target.
+ * Adjust for zero, because it's easy to think of this as rolling a
+ * bound-sided dice and marking a success for every roll less than or equal to
+ * target.
*/
int num = rn.nextInt(bound) + 1;
if (num <= target) {
- //System.err.printf("\t\tTRACE: mark binomial success (%d <= 1d%d, %d)\n", target, bound, num);
+ // System.err.printf("\t\tTRACE: mark binomial success (%d <= 1d%d,
+ // %d)\n", target, bound, num);
numSuc += 1;
}
}
- //System.err.printf("\tTRACE: got %d success for binomial trials (%d <= 1d%d, %d times)\n", numSuc, target, bound, trials);
+ // System.err.printf("\tTRACE: got %d success for binomial trials (%d <= 1d%d,
+ // %d times)\n", numSuc, target, bound, trials);
IPair<Integer, E> val = values.getByIndex(Math.min(numSuc, values.getSize() - 1));
if (exhaust) {
totalChance -= val.getLeft();
@@ -244,8 +247,9 @@ public class WeightedRandom<E> {
}
/**
- * Return a new WeightedRandom that is exhaustible (only returns one value once).
- *
+ * Return a new WeightedRandom that is exhaustible (only returns one value
+ * once).
+ *
* @return A new WeightedRandom that is exhaustible.
*/
public WeightedRandom<E> exhaustible() {