From 6fb202443b07b9ba93bf036720a0808b4dcde3ac Mon Sep 17 00:00:00 2001 From: Hannes Greule Date: Sat, 15 Aug 2020 13:18:14 +0200 Subject: [PATCH] Clean up RandomCollection architecture --- .../collection/FastRandomCollection.java | 30 ++++++++++++++----- .../object/collection/RandomCollection.java | 12 ++++---- .../collection/SimpleRandomCollection.java | 5 ++-- 3 files changed, 31 insertions(+), 16 deletions(-) diff --git a/worldedit-core/src/main/java/com/boydti/fawe/object/collection/FastRandomCollection.java b/worldedit-core/src/main/java/com/boydti/fawe/object/collection/FastRandomCollection.java index 41ac563cf..6b68e9191 100644 --- a/worldedit-core/src/main/java/com/boydti/fawe/object/collection/FastRandomCollection.java +++ b/worldedit-core/src/main/java/com/boydti/fawe/object/collection/FastRandomCollection.java @@ -5,12 +5,25 @@ import com.boydti.fawe.util.MathMan; import java.util.ArrayList; import java.util.Map; +import java.util.Optional; public class FastRandomCollection extends RandomCollection { - private T[] values; + private final T[] values; - public FastRandomCollection(Map weights, SimpleRandom random) { - super(weights, random); + private FastRandomCollection(T[] values, SimpleRandom random) { + super(random); + this.values = values; + } + + /** + * Create a new FastRandomCollection if the given values and weights match the criteria. + * @param weights the weight of the values. + * @param random the random generator to use for this collection. + * @param the value type. + * @return an {@link Optional} containing the new collection if it could + * be created, {@link Optional#empty()} otherwise. + */ + public static Optional> create(Map weights, SimpleRandom random) { int max = 0; int[] counts = new int[weights.size()]; Double[] weightDoubles = weights.values().toArray(new Double[0]); @@ -18,7 +31,7 @@ public class FastRandomCollection extends RandomCollection { int weight = (int) (weightDoubles[i] * 100); counts[i] = weight; if (weight != (weightDoubles[i] * 100)) { - throw new IllegalArgumentException("Too small"); + return Optional.empty(); } if (weight > max) { max = weight; @@ -26,7 +39,7 @@ public class FastRandomCollection extends RandomCollection { } int gcd = MathMan.gcd(counts); if (max / gcd > 100000) { - throw new IllegalArgumentException("Too large"); + return Optional.empty(); } ArrayList parsed = new ArrayList<>(); for (Map.Entry entry : weights.entrySet()) { @@ -35,11 +48,14 @@ public class FastRandomCollection extends RandomCollection { parsed.add(entry.getKey()); } } - this.values = (T[]) parsed.toArray(); + @SuppressWarnings("unchecked") + T[] values = (T[]) parsed.toArray(); + FastRandomCollection fastRandomCollection = new FastRandomCollection<>(values, random); + return Optional.of(fastRandomCollection); } @Override public T next(int x, int y, int z) { - return values[random.nextInt(x, y, z, values.length)]; + return values[getRandom().nextInt(x, y, z, values.length)]; } } diff --git a/worldedit-core/src/main/java/com/boydti/fawe/object/collection/RandomCollection.java b/worldedit-core/src/main/java/com/boydti/fawe/object/collection/RandomCollection.java index fc12c1ac6..b847b476a 100644 --- a/worldedit-core/src/main/java/com/boydti/fawe/object/collection/RandomCollection.java +++ b/worldedit-core/src/main/java/com/boydti/fawe/object/collection/RandomCollection.java @@ -7,18 +7,16 @@ import java.util.Map; import static com.google.common.base.Preconditions.checkNotNull; public abstract class RandomCollection { - protected SimpleRandom random; + private SimpleRandom random; - public RandomCollection(Map weights, SimpleRandom random) { + protected RandomCollection(SimpleRandom random) { this.random = random; } public static RandomCollection of(Map weights, SimpleRandom random) { - try { - return new FastRandomCollection<>(weights, random); - } catch (IllegalArgumentException ignore) { - return new SimpleRandomCollection<>(weights, random); - } + checkNotNull(random); + return FastRandomCollection.create(weights, random) + .orElse(new SimpleRandomCollection<>(weights, random)); } public void setRandom(SimpleRandom random) { diff --git a/worldedit-core/src/main/java/com/boydti/fawe/object/collection/SimpleRandomCollection.java b/worldedit-core/src/main/java/com/boydti/fawe/object/collection/SimpleRandomCollection.java index e99593c0f..a5ddcde42 100644 --- a/worldedit-core/src/main/java/com/boydti/fawe/object/collection/SimpleRandomCollection.java +++ b/worldedit-core/src/main/java/com/boydti/fawe/object/collection/SimpleRandomCollection.java @@ -12,7 +12,7 @@ public class SimpleRandomCollection extends RandomCollection { private double total = 0; public SimpleRandomCollection(Map weights, SimpleRandom random) { - super(weights, random); + super(random); for (Map.Entry entry : weights.entrySet()) { add(entry.getValue(), entry.getKey()); } @@ -24,7 +24,8 @@ public class SimpleRandomCollection extends RandomCollection { map.put(total, result); } + @Override public E next(int x, int y, int z) { - return map.ceilingEntry(random.nextDouble(x, y, z)).getValue(); + return map.ceilingEntry(getRandom().nextDouble(x, y, z)).getValue(); } }