diff options
| author | Ben Culkin <scorpress@gmail.com> | 2021-03-13 09:11:12 -0500 |
|---|---|---|
| committer | Ben Culkin <scorpress@gmail.com> | 2021-03-13 09:11:12 -0500 |
| commit | 7efb7b9e997e0977c8343718cd8b5149805ea57b (patch) | |
| tree | 869ba045f07a6c5aa8a9c6d388941cc5a4862192 /dice/src/main/java/bjc | |
| parent | c7c7503bd4a31e88924514d8e6fd4885fcfac042 (diff) | |
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
Diffstat (limited to 'dice/src/main/java/bjc')
6 files changed, 224 insertions, 190 deletions
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 <SideType> The type of value represented by this die. * */ @FunctionalInterface @@ -40,6 +41,7 @@ public interface Die<SideType> { /** * 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<SideType> { * 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<SideType> { return Stream.generate(() -> this.roll(rng)); } + /** + * Create a new transforming die. + * + * @param <NewType> The new type of the die. + * + * @param mapper The function to use for mapping. + * + * @return A die that transforms with the given mapping. + */ default <NewType> Die<NewType> transform(Function<SideType, NewType> 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<SideType> { /** * Create a die pool containing the provided dice. * + * @param <Side> 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<SideType> { static <Side> DiePool<Side> containing(Die<Side>... dice) { return new FixedDiePool<>(dice); } + + /** + * Create an expanding die pool + * + * @param <Side> 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 <Side> DiePool<Side> expanding(Die<Side> contained, + BiFunction<Die<Side>, Random, Stream<Side>> expander) + { + return new ExpandDiePool<>(contained, expander); + } +} + +/** + * A die pool that can expand dice. + * @author Ben Culkin + * + * @param <SideType> The type the die uses. + */ +class ExpandDiePool<SideType> implements DiePool<SideType> { + private final Die<SideType> contained; + + private final BiFunction<Die<SideType>, Random, Stream<SideType>> expander; + + /** + * Create a new expanding die pool. + * + * @param contained The die to expand. + * @param expander The function to use for expanding. + */ + public ExpandDiePool(Die<SideType> contained, + BiFunction<Die<SideType>, Random, Stream<SideType>> expander) { + this.contained = contained; + this.expander = expander; + } + + @Override + public Stream<SideType> roll(Random rng) { + return expander.apply(contained, rng); + } + + @Override + public List<Die<SideType>> contained() + { + return Arrays.asList(contained); + } +} + +/** + * A die pool that has a fixed size. + * + * @author Ben Culkin + * + * @param <SideType> The type of the sides of the dice. + */ +class FixedDiePool<SideType> implements DiePool<SideType> { + private final List<Die<SideType>> dice; + + /** + * Create a new fixed dice pool. + * @param dice The dice to put into the pool. + */ + public FixedDiePool(List<Die<SideType>> 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<SideType>...dice) { + this.dice = new ArrayList<>(dice.length); + for (Die<SideType> die : dice) { + this.dice.add(die); + } + } + + @Override + public Stream<SideType> roll(Random rng) { + return dice.stream().map((die) -> die.roll(rng)); + } + + @Override + public List<Die<SideType>> contained() { + return dice; + } + + + @Override + public String toString() { + return dice.stream() + .map(Die<SideType>::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<SideType> implements DiePool<SideType> { + private final Die<SideType> contained; + private final int numDice; + + public TimesDiePool(Die<SideType> contained, int numDice) { + this.contained = contained; + this.numDice = numDice; + } + + @Override + public Stream<SideType> roll(Random rng) { + return Stream.generate(() -> contained.roll(rng)) + .limit(numDice); + } + + @Override + public List<Die<SideType>> contained() { + List<Die<SideType>> 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<SideType> implements DiePool<SideType> { + private final DiePool<SideType> contained; + + private UnaryOperator<Stream<SideType>> transform; + + public TransformDiePool(DiePool<SideType> contained, + UnaryOperator<Stream<SideType>> transform) { + super(); + this.contained = contained; + this.transform = transform; + } + + @Override + public Stream<SideType> roll(Random rng) { + return transform.apply(contained.roll(rng)); + } + + @Override + public List<Die<SideType>> 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<SideType> implements DiePool<SideType> { - private final Die<SideType> contained; - - private final BiFunction<Die<SideType>, Random, Stream<SideType>> expander; - - public ExpandDiePool(Die<SideType> contained, - BiFunction<Die<SideType>, Random, Stream<SideType>> expander) { - this.contained = contained; - this.expander = expander; - } - - - @Override - public Stream<SideType> 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<SideType> implements DiePool<SideType> { - private final List<Die<SideType>> dice; - - public FixedDiePool(List<Die<SideType>> dice) { - this.dice = dice; - } - - @SafeVarargs - public FixedDiePool(Die<SideType>...dice) { - this.dice = new ArrayList<>(dice.length); - for (Die<SideType> die : dice) { - this.dice.add(die); - } - } - - @Override - public Stream<SideType> roll(Random rng) { - return dice.stream().map((die) -> die.roll(rng)); - } - - @Override - public List<Die<SideType>> contained() { - return dice; - } - - - @Override - public String toString() { - return dice.stream() - .map(Die<SideType>::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<SideType> implements DiePool<SideType> { - private final Die<SideType> contained; - private final int numDice; - - public TimesDiePool(Die<SideType> contained, int numDice) { - this.contained = contained; - this.numDice = numDice; - } - - @Override - public Stream<SideType> roll(Random rng) { - return Stream.generate(() -> contained.roll(rng)) - .limit(numDice); - } - - @Override - public List<Die<SideType>> contained() { - List<Die<SideType>> 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<SideType> implements DiePool<SideType> { - private final DiePool<SideType> contained; - - private UnaryOperator<Stream<SideType>> transform; - - public TransformDiePool(DiePool<SideType> contained, - UnaryOperator<Stream<SideType>> transform) { - super(); - this.contained = contained; - this.transform = transform; - } - - @Override - public Stream<SideType> roll(Random rng) { - return transform.apply(contained.roll(rng)); - } - - @Override - public List<Die<SideType>> 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)"; - } -} |
