From 7efb7b9e997e0977c8343718cd8b5149805ea57b Mon Sep 17 00:00:00 2001 From: Ben Culkin Date: Sat, 13 Mar 2021 09:11:12 -0500 Subject: Add more documentation Also, changed my mind on the way DiePool and its implementations should be structured. The implementations go in the die pool file as internal classes, because nobody should particularly care the specifics about their die pool, only that it does what it says it should --- dice/src/main/java/bjc/dicelang/neodice/Die.java | 14 +- .../main/java/bjc/dicelang/neodice/DiePool.java | 213 ++++++++++++++++++++- .../dicelang/neodice/diepool/ExpandDiePool.java | 25 --- .../bjc/dicelang/neodice/diepool/FixedDiePool.java | 56 ------ .../bjc/dicelang/neodice/diepool/TimesDiePool.java | 54 ------ .../dicelang/neodice/diepool/TransformDiePool.java | 52 ----- 6 files changed, 224 insertions(+), 190 deletions(-) delete mode 100644 dice/src/main/java/bjc/dicelang/neodice/diepool/ExpandDiePool.java delete mode 100644 dice/src/main/java/bjc/dicelang/neodice/diepool/FixedDiePool.java delete mode 100644 dice/src/main/java/bjc/dicelang/neodice/diepool/TimesDiePool.java delete mode 100644 dice/src/main/java/bjc/dicelang/neodice/diepool/TransformDiePool.java (limited to 'dice/src/main') diff --git a/dice/src/main/java/bjc/dicelang/neodice/Die.java b/dice/src/main/java/bjc/dicelang/neodice/Die.java index 910d173..9d1ac4b 100644 --- a/dice/src/main/java/bjc/dicelang/neodice/Die.java +++ b/dice/src/main/java/bjc/dicelang/neodice/Die.java @@ -5,11 +5,12 @@ import java.util.function.*; import java.util.stream.*; import bjc.dicelang.neodice.die.*; -import bjc.dicelang.neodice.diepool.*; /** * Represents a single polyhedral die. * @author Ben Culkin + * + * @param The type of value represented by this die. * */ @FunctionalInterface @@ -40,6 +41,7 @@ public interface Die { /** * Returns a die which will reroll this die as long as the provided condition is true. * + * @param comparer The thing to use to compare die values. * @param condition The condition to reroll the die on. * * @return A die that rerolls when the given condition is met. @@ -55,6 +57,7 @@ public interface Die { * Returns a die which will reroll this die up to a specified number of times, * as long as the provided condition is true. * + * @param comparer The thing to use to compare die values. * @param condition The condition to reroll the die on. * @param limit The maximum number of times to reroll the die. * @@ -79,6 +82,15 @@ public interface Die { return Stream.generate(() -> this.roll(rng)); } + /** + * Create a new transforming die. + * + * @param The new type of the die. + * + * @param mapper The function to use for mapping. + * + * @return A die that transforms with the given mapping. + */ default Die transform(Function mapper) { return (rng) -> mapper.apply(this.roll(rng)); } diff --git a/dice/src/main/java/bjc/dicelang/neodice/DiePool.java b/dice/src/main/java/bjc/dicelang/neodice/DiePool.java index e3a5782..4513682 100644 --- a/dice/src/main/java/bjc/dicelang/neodice/DiePool.java +++ b/dice/src/main/java/bjc/dicelang/neodice/DiePool.java @@ -4,8 +4,6 @@ import java.util.*; import java.util.function.*; import java.util.stream.*; -import bjc.dicelang.neodice.diepool.*; - /** * Represents a pool of dice. * @@ -211,6 +209,8 @@ public interface DiePool { /** * Create a die pool containing the provided dice. * + * @param The type of the sides. + * * @param dice The dice to put into the pool. * * @return A pool which contains the provided dice. @@ -219,4 +219,213 @@ public interface DiePool { static DiePool containing(Die... dice) { return new FixedDiePool<>(dice); } + + /** + * Create an expanding die pool + * + * @param The type of the sides. + * + * @param contained The contained die. + * @param expander The expanding function. + * + * @return A die pool that expands the result given the provided function. + */ + static DiePool expanding(Die contained, + BiFunction, Random, Stream> expander) + { + return new ExpandDiePool<>(contained, expander); + } +} + +/** + * A die pool that can expand dice. + * @author Ben Culkin + * + * @param The type the die uses. + */ +class ExpandDiePool implements DiePool { + private final Die contained; + + private final BiFunction, Random, Stream> expander; + + /** + * Create a new expanding die pool. + * + * @param contained The die to expand. + * @param expander The function to use for expanding. + */ + public ExpandDiePool(Die contained, + BiFunction, Random, Stream> expander) { + this.contained = contained; + this.expander = expander; + } + + @Override + public Stream roll(Random rng) { + return expander.apply(contained, rng); + } + + @Override + public List> contained() + { + return Arrays.asList(contained); + } +} + +/** + * A die pool that has a fixed size. + * + * @author Ben Culkin + * + * @param The type of the sides of the dice. + */ +class FixedDiePool implements DiePool { + private final List> dice; + + /** + * Create a new fixed dice pool. + * @param dice The dice to put into the pool. + */ + public FixedDiePool(List> dice) { + this.dice = dice; + } + + /** + * Create a new fixed dice pool from an array of dice. + * @param dice The dice to put into the pool. + */ + @SafeVarargs + public FixedDiePool(Die...dice) { + this.dice = new ArrayList<>(dice.length); + for (Die die : dice) { + this.dice.add(die); + } + } + + @Override + public Stream roll(Random rng) { + return dice.stream().map((die) -> die.roll(rng)); + } + + @Override + public List> contained() { + return dice; + } + + + @Override + public String toString() { + return dice.stream() + .map(Die::toString) + .collect(Collectors.joining(", ")); + } + + @Override + public int hashCode() { + return Objects.hash(dice); + } + + @Override + public boolean equals(Object obj) { + if (this == obj) return true; + if (obj == null) return false; + if (getClass() != obj.getClass()) return false; + + FixedDiePool other = (FixedDiePool) obj; + + return Objects.equals(dice, other.dice); + } +} + +class TimesDiePool implements DiePool { + private final Die contained; + private final int numDice; + + public TimesDiePool(Die contained, int numDice) { + this.contained = contained; + this.numDice = numDice; + } + + @Override + public Stream roll(Random rng) { + return Stream.generate(() -> contained.roll(rng)) + .limit(numDice); + } + + @Override + public List> contained() { + List> results = new ArrayList<>(numDice); + + for (int index = 0; index < numDice; index++) { + results.add(contained); + } + + return results; + } + + @Override + public String toString() { + return String.format("%d%s", numDice, contained); + } + + @Override + public int hashCode() { + return Objects.hash(contained, numDice); + } + + @Override + public boolean equals(Object obj) { + if (this == obj) return true; + if (obj == null) return false; + if (getClass() != obj.getClass()) return false; + + TimesDiePool other = (TimesDiePool) obj; + + return Objects.equals(contained, other.contained) && numDice == other.numDice; + } +} + +class TransformDiePool implements DiePool { + private final DiePool contained; + + private UnaryOperator> transform; + + public TransformDiePool(DiePool contained, + UnaryOperator> transform) { + super(); + this.contained = contained; + this.transform = transform; + } + + @Override + public Stream roll(Random rng) { + return transform.apply(contained.roll(rng)); + } + + @Override + public List> contained() { + return contained.contained(); + } + + @Override + public int hashCode() { + return Objects.hash(contained, transform); + } + + @Override + public boolean equals(Object obj) { + if (this == obj) return true; + if (obj == null) return false; + if (getClass() != obj.getClass()) return false; + + TransformDiePool other = (TransformDiePool) obj; + + return Objects.equals(contained, other.contained) + && Objects.equals(transform, other.transform); + } + + @Override + public String toString() { + return contained.toString() + "(transformed)"; + } } \ No newline at end of file diff --git a/dice/src/main/java/bjc/dicelang/neodice/diepool/ExpandDiePool.java b/dice/src/main/java/bjc/dicelang/neodice/diepool/ExpandDiePool.java deleted file mode 100644 index 97acc79..0000000 --- a/dice/src/main/java/bjc/dicelang/neodice/diepool/ExpandDiePool.java +++ /dev/null @@ -1,25 +0,0 @@ -package bjc.dicelang.neodice.diepool; - -import java.util.*; -import java.util.function.*; -import java.util.stream.*; - -import bjc.dicelang.neodice.*; - -public class ExpandDiePool implements DiePool { - private final Die contained; - - private final BiFunction, Random, Stream> expander; - - public ExpandDiePool(Die contained, - BiFunction, Random, Stream> expander) { - this.contained = contained; - this.expander = expander; - } - - - @Override - public Stream roll(Random rng) { - return expander.apply(contained, rng); - } -} diff --git a/dice/src/main/java/bjc/dicelang/neodice/diepool/FixedDiePool.java b/dice/src/main/java/bjc/dicelang/neodice/diepool/FixedDiePool.java deleted file mode 100644 index 4950407..0000000 --- a/dice/src/main/java/bjc/dicelang/neodice/diepool/FixedDiePool.java +++ /dev/null @@ -1,56 +0,0 @@ -package bjc.dicelang.neodice.diepool; - -import java.util.*; -import java.util.stream.*; - -import bjc.dicelang.neodice.*; - -public class FixedDiePool implements DiePool { - private final List> dice; - - public FixedDiePool(List> dice) { - this.dice = dice; - } - - @SafeVarargs - public FixedDiePool(Die...dice) { - this.dice = new ArrayList<>(dice.length); - for (Die die : dice) { - this.dice.add(die); - } - } - - @Override - public Stream roll(Random rng) { - return dice.stream().map((die) -> die.roll(rng)); - } - - @Override - public List> contained() { - return dice; - } - - - @Override - public String toString() { - return dice.stream() - .map(Die::toString) - .collect(Collectors.joining(", ")); - } - - @Override - public int hashCode() { - return Objects.hash(dice); - } - - @Override - public boolean equals(Object obj) { - if (this == obj) return true; - if (obj == null) return false; - if (getClass() != obj.getClass()) return false; - - FixedDiePool other = (FixedDiePool) obj; - - return Objects.equals(dice, other.dice); - } -} \ No newline at end of file diff --git a/dice/src/main/java/bjc/dicelang/neodice/diepool/TimesDiePool.java b/dice/src/main/java/bjc/dicelang/neodice/diepool/TimesDiePool.java deleted file mode 100644 index e039e47..0000000 --- a/dice/src/main/java/bjc/dicelang/neodice/diepool/TimesDiePool.java +++ /dev/null @@ -1,54 +0,0 @@ -package bjc.dicelang.neodice.diepool; - -import java.util.*; -import java.util.stream.*; - -import bjc.dicelang.neodice.*; - -public class TimesDiePool implements DiePool { - private final Die contained; - private final int numDice; - - public TimesDiePool(Die contained, int numDice) { - this.contained = contained; - this.numDice = numDice; - } - - @Override - public Stream roll(Random rng) { - return Stream.generate(() -> contained.roll(rng)) - .limit(numDice); - } - - @Override - public List> contained() { - List> results = new ArrayList<>(numDice); - - for (int index = 0; index < numDice; index++) { - results.add(contained); - } - - return results; - } - - @Override - public String toString() { - return String.format("%d%s", numDice, contained); - } - - @Override - public int hashCode() { - return Objects.hash(contained, numDice); - } - - @Override - public boolean equals(Object obj) { - if (this == obj) return true; - if (obj == null) return false; - if (getClass() != obj.getClass()) return false; - - TimesDiePool other = (TimesDiePool) obj; - - return Objects.equals(contained, other.contained) && numDice == other.numDice; - } -} \ No newline at end of file diff --git a/dice/src/main/java/bjc/dicelang/neodice/diepool/TransformDiePool.java b/dice/src/main/java/bjc/dicelang/neodice/diepool/TransformDiePool.java deleted file mode 100644 index 80b563f..0000000 --- a/dice/src/main/java/bjc/dicelang/neodice/diepool/TransformDiePool.java +++ /dev/null @@ -1,52 +0,0 @@ -package bjc.dicelang.neodice.diepool; - -import java.util.*; -import java.util.function.*; -import java.util.stream.*; - -import bjc.dicelang.neodice.*; - -public class TransformDiePool implements DiePool { - private final DiePool contained; - - private UnaryOperator> transform; - - public TransformDiePool(DiePool contained, - UnaryOperator> transform) { - super(); - this.contained = contained; - this.transform = transform; - } - - @Override - public Stream roll(Random rng) { - return transform.apply(contained.roll(rng)); - } - - @Override - public List> contained() { - return contained.contained(); - } - - @Override - public int hashCode() { - return Objects.hash(contained, transform); - } - - @Override - public boolean equals(Object obj) { - if (this == obj) return true; - if (obj == null) return false; - if (getClass() != obj.getClass()) return false; - - TransformDiePool other = (TransformDiePool) obj; - - return Objects.equals(contained, other.contained) - && Objects.equals(transform, other.transform); - } - - @Override - public String toString() { - return contained.toString() + "(transformed)"; - } -} -- cgit v1.2.3