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 --- .../main/java/bjc/dicelang/neodice/DiePool.java | 213 ++++++++++++++++++++- 1 file changed, 211 insertions(+), 2 deletions(-) (limited to 'dice/src/main/java/bjc/dicelang/neodice/DiePool.java') 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 -- cgit v1.2.3