View Javadoc

1   /*
2    * Copyright 2006 - 2012 Christina Bohk and Roland Ewald
3    *  
4    * Licensed under the Apache License, Version 2.0 (the "License"); 
5    * you may not use this file except in compliance with the License. 
6    * You may obtain a copy of the License at 
7    *  
8    *  http://www.apache.org/licenses/LICENSE-2.0
9    *  
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 
13   * See the License for the specific language governing permissions and 
14   * limitations under the License. 
15   */
16  package p3j.simulation.assignments.exhaustive;
17  
18  import java.util.List;
19  
20  import p3j.misc.Misc;
21  
22  /**
23   * Stores a selection of (Settype, parameter,...) assignments and their overall
24   * probability.
25   * 
26   * Created: August 23, 2008
27   * 
28   * @author Christina Bohk
29   * @author Roland Ewald
30   * 
31   */
32  class Assignment implements Comparable<Assignment> {
33  
34  	/** Assignment indices. */
35  	private final List<Integer> assignmentIndices;
36  
37  	/** The probability of this assignment. */
38  	private final Double probability;
39  
40  	/** Index sum (required for sorting). */
41  	private final Integer indexSum;
42  
43  	/** The id of this assignment: index1+index2+etc (concatenation). */
44  	private final String id;
45  
46  	/**
47  	 * Default constructor.
48  	 * 
49  	 * @param stAssigns
50  	 *          indices of assignments
51  	 * @param prob
52  	 *          their probability
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 	 * Gets the ID.
107 	 * 
108 	 * @return the id
109 	 */
110 	public String getID() {
111 		return id;
112 	}
113 
114 	public Integer getIndexSum() {
115 		return indexSum;
116 	}
117 
118 }