From 889fac2bdf993dc86f64a8893c0260fdcf848acb Mon Sep 17 00:00:00 2001 From: bculkin2442 Date: Mon, 10 Apr 2017 16:40:33 -0400 Subject: Cleanup --- .../main/java/bjc/utils/gen/WeightedRandom.java | 26 +++++++++++----------- 1 file changed, 13 insertions(+), 13 deletions(-) (limited to 'BJC-Utils2/src/main/java/bjc/utils/gen/WeightedRandom.java') 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 88f623e..18225ef 100644 --- a/BJC-Utils2/src/main/java/bjc/utils/gen/WeightedRandom.java +++ b/BJC-Utils2/src/main/java/bjc/utils/gen/WeightedRandom.java @@ -1,13 +1,13 @@ package bjc.utils.gen; +import java.util.Random; + import bjc.utils.data.IHolder; import bjc.utils.data.IPair; import bjc.utils.data.Identity; import bjc.utils.funcdata.FunctionalList; import bjc.utils.funcdata.IList; -import java.util.Random; - /** * Represents a random number generator where certain results are weighted more * heavily than others. @@ -21,17 +21,17 @@ public class WeightedRandom { /* * The list of probabilities for each result */ - private IList probabilities; + private final IList probabilities; /* * The list of possible results to pick from */ - private IList results; + private final IList results; /* * The source for any needed random numbers */ - private Random source; + private final Random source; private int totalChance; @@ -42,11 +42,11 @@ public class WeightedRandom { * @param src * The source of randomness to use. */ - public WeightedRandom(Random src) { + public WeightedRandom(final Random src) { probabilities = new FunctionalList<>(); results = 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; } @@ -59,7 +59,7 @@ public class WeightedRandom { * @param result * The result to get when the chance comes up. */ - public void addProbability(int chance, E result) { + public void addProbability(final int chance, final E result) { probabilities.add(chance); results.add(result); @@ -72,13 +72,13 @@ public class WeightedRandom { * @return A random value selected in a weighted fashion. */ public E generateValue() { - IHolder value = new Identity<>(source.nextInt(totalChance)); - IHolder current = new Identity<>(); - IHolder picked = new Identity<>(true); + final IHolder value = new Identity<>(source.nextInt(totalChance)); + final IHolder current = new Identity<>(); + final IHolder picked = new Identity<>(true); probabilities.forEachIndexed((index, probability) -> { - if(picked.unwrap(bool -> bool)) { - if(value.unwrap((number) -> number < probability)) { + if (picked.unwrap(bool -> bool)) { + if (value.unwrap((number) -> number < probability)) { current.transform((result) -> results.getByIndex(index)); picked.transform((bool) -> false); -- cgit v1.2.3