1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16 package p3j.simulation.assignments.exhaustive;
17
18 import java.util.List;
19
20 import p3j.misc.Misc;
21
22
23
24
25
26
27
28
29
30
31
32 class Assignment implements Comparable<Assignment> {
33
34
35 private final List<Integer> assignmentIndices;
36
37
38 private final Double probability;
39
40
41 private final Integer indexSum;
42
43
44 private final String id;
45
46
47
48
49
50
51
52
53
54 Assignment(List<Integer> stAssigns, double prob) {
55 assignmentIndices = stAssigns;
56 probability = prob;
57
58 int sum = 0;
59 StringBuffer strBuf = new StringBuffer();
60 for (Integer index : stAssigns) {
61 sum += index;
62 strBuf.append(index);
63 strBuf.append('-');
64 }
65
66 id = strBuf.toString();
67 indexSum = sum;
68 }
69
70 public List<Integer> getAssignmentIndices() {
71 return assignmentIndices;
72 }
73
74 public Double getProbability() {
75 return probability;
76 }
77
78 @Override
79 public int compareTo(Assignment a) {
80 if (!Misc.numEqual(probability, a.probability)) {
81 return a.probability.compareTo(probability);
82 }
83 return id.compareTo(a.id);
84 }
85
86 @Override
87 public boolean equals(Object o) {
88 if (!(o instanceof Assignment)) {
89 return false;
90 }
91 return compareTo(((Assignment) o)) == 0;
92 }
93
94 @Override
95 public int hashCode() {
96 int code = 0;
97 int multiplier = 1;
98 for (int index : assignmentIndices) {
99 code += multiplier * index;
100 multiplier *= Misc.BASE_NUM;
101 }
102 return code;
103 }
104
105
106
107
108
109
110 public String getID() {
111 return id;
112 }
113
114 public Integer getIndexSum() {
115 return indexSum;
116 }
117
118 }