summaryrefslogtreecommitdiff
path: root/dice/src/main/java/bjc/dicelang/neodice/DiePoolFactory.java
blob: 6d9314d2f539a0e8d65d7ef6b106ab1c516a11de (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
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
package bjc.dicelang.neodice;

import java.util.*;

/**
 * Various static functions which create instances of DiePool.
 * 
 * @author Ben Culkin
 *
 */
public class DiePoolFactory {
	/**
	 * Create a die pool containing the provided dice.
	 * 
	 * @param dice The dice to put into the pool.
	 * 
	 * @return A pool which contains the provided dice.
	 */
	public static DiePool containing(Die... dice) {
		return new FixedDiePool(dice);
	}
}

final class FixedDiePool implements DiePool {
	private final Die[] dice;

	public FixedDiePool(Die[] dice) {
		this.dice = dice;
	}

	@Override
	public int[] roll(Random rng) {
		int[] results = new int[dice.length];
		
		for (int index = 0; index < dice.length; index++) {
			results[index] = dice[index].roll(rng);
		}
		
		return results;
	}

	@Override
	public Die[] contained() {
		return dice;
	}

	
	@Override
	public String toString() {
		StringBuilder builder = new StringBuilder();
		
		for (int i = 0; i < dice.length; i++) {
			Die die = dice[i];
			
			builder.append(die);
			
			// Don't add an extra trailing comma
			if (i < dice.length - 1) builder.append(", ");
		}
		
		return builder.toString();
	}

	@Override
	public int hashCode() {
		final int prime = 31;
		int result = 1;
		result = prime * result + Arrays.hashCode(dice);
		return result;
	}

	@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 Arrays.equals(dice, other.dice);
	}
}