diff options
| author | Ben Culkin <scorpress@gmail.com> | 2020-11-21 23:11:43 -0500 |
|---|---|---|
| committer | Ben Culkin <scorpress@gmail.com> | 2020-11-21 23:11:43 -0500 |
| commit | eda9a86d8d48758e9982cfffd470c3b38a0a4b0b (patch) | |
| tree | fbe073650de751486725844ed41dfe70986a914d /dice/src/main/java/bjc/dicelang/neodice/diepool/TimesDiePool.java | |
| parent | b5c2fb1ed923d43412694729b4445a66fa9f47fc (diff) | |
Make dice generic
Convert dice from dealing exclusively with ints, to deal with objects of
arbitrary types
Diffstat (limited to 'dice/src/main/java/bjc/dicelang/neodice/diepool/TimesDiePool.java')
| -rw-r--r-- | dice/src/main/java/bjc/dicelang/neodice/diepool/TimesDiePool.java | 26 |
1 files changed, 11 insertions, 15 deletions
diff --git a/dice/src/main/java/bjc/dicelang/neodice/diepool/TimesDiePool.java b/dice/src/main/java/bjc/dicelang/neodice/diepool/TimesDiePool.java index 1b34247..56fe0e5 100644 --- a/dice/src/main/java/bjc/dicelang/neodice/diepool/TimesDiePool.java +++ b/dice/src/main/java/bjc/dicelang/neodice/diepool/TimesDiePool.java @@ -1,35 +1,31 @@ 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; +public class TimesDiePool<SideType> implements IDiePool<SideType> { + private final IDie<SideType> contained; private final int numDice; - public TimesDiePool(Die contained, int numDice) { + public TimesDiePool(IDie<SideType> contained, int numDice) { this.contained = contained; this.numDice = numDice; } @Override - public int[] roll(Random rng) { - int[] results = new int[numDice]; - - for (int index = 0; index < numDice; index++) { - results[index] = contained.roll(rng); - } - - return results; + public Stream<SideType> roll(Random rng) { + return Stream.generate(() -> contained.roll(rng)) + .limit(numDice); } @Override - public Die[] contained() { - Die[] results = new Die[numDice]; + public List<IDie<SideType>> contained() { + List<IDie<SideType>> results = new ArrayList<>(numDice); for (int index = 0; index < numDice; index++) { - results[index] = contained; + results.add(contained); } return results; @@ -51,7 +47,7 @@ public class TimesDiePool implements DiePool { if (obj == null) return false; if (getClass() != obj.getClass()) return false; - TimesDiePool other = (TimesDiePool) obj; + TimesDiePool<?> other = (TimesDiePool<?>) obj; return Objects.equals(contained, other.contained) && numDice == other.numDice; } |
