summaryrefslogtreecommitdiff
path: root/dice/src/main/java/bjc/dicelang/neodice/diepool/TimesDiePool.java
blob: 56fe0e5f4ab8279cd041e48ba6cce84b349758b3 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
package bjc.dicelang.neodice.diepool;

import java.util.*;
import java.util.stream.*;

import bjc.dicelang.neodice.*;

public class TimesDiePool<SideType> implements IDiePool<SideType> {
	private final IDie<SideType> contained;
	private final int numDice;

	public TimesDiePool(IDie<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<IDie<SideType>> contained() {
		List<IDie<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;
	}
}