1 | package dk.deepthought.sidious.greenhouse; |
2 | |
3 | import net.jcip.annotations.Immutable; |
4 | |
5 | import org.apache.commons.logging.Log; |
6 | import org.apache.commons.logging.LogFactory; |
7 | |
8 | import dk.deepthought.sidious.supportsystem.SuperLinkID; |
9 | |
10 | /** |
11 | * This class represents a sensor input from the environment at a specific time. |
12 | * <p> |
13 | * It holds the id and the value of the given sensor at given time. |
14 | * |
15 | * @author Deepthought |
16 | * |
17 | */ |
18 | @Immutable |
19 | public final class SensorInput { |
20 | /** |
21 | * Logger for this class |
22 | */ |
23 | private static final Log logger = LogFactory.getLog(SensorInput.class); |
24 | |
25 | /** |
26 | * The id of the sensor this represent. |
27 | */ |
28 | private final SuperLinkID superLinkID; |
29 | |
30 | /** |
31 | * Current value of this sensor. |
32 | */ |
33 | private final double value; |
34 | |
35 | /** |
36 | * Creates a new <code>SensorInput</code> object from the specified |
37 | * <code>SuperLinkID</code> and <code>value</code>. |
38 | * |
39 | * @param superLinkID |
40 | * the SuperLink Identifier of this sensor |
41 | * @param value |
42 | * value of this sensor |
43 | */ |
44 | public SensorInput(SuperLinkID superLinkID, double value) { |
45 | if (Double.isNaN(value)) { |
46 | logger.error("SensorInput(SuperLinkID superLinkID=" + superLinkID |
47 | + ", double value=" + value + ") - NaN not a legal value"); |
48 | throw new IllegalArgumentException("NaN not a legal value"); |
49 | } |
50 | this.superLinkID = superLinkID; |
51 | this.value = round(value); |
52 | } |
53 | |
54 | /** |
55 | * Rounds the specified value. |
56 | * |
57 | * @param val |
58 | * the value to round |
59 | * @return the rounded value |
60 | */ |
61 | private double round(double val) { |
62 | return ((int) (val * 10)) / 10d; |
63 | } |
64 | |
65 | /** |
66 | * Creates a new instance of this type of <code>SensorInput</code> (i.e. |
67 | * with the same id) with the specified new <code>value</code>. |
68 | * |
69 | * @param value |
70 | * the value of the new instance |
71 | * @return a new instance of this <code>SensorInput</code> |
72 | */ |
73 | public SensorInput newInstanceWithNewValue(double value) { |
74 | if (logger.isDebugEnabled()) { |
75 | logger.debug("newInstanceWithNewValue(double value=" + value |
76 | + ") - start"); |
77 | } |
78 | |
79 | SensorInput returnSensorInput = new SensorInput(superLinkID, value); |
80 | if (logger.isDebugEnabled()) { |
81 | logger.debug("newInstanceWithNewValue(double value=" + value |
82 | + ") - end - return value=" + returnSensorInput); |
83 | } |
84 | return returnSensorInput; |
85 | } |
86 | |
87 | /** |
88 | * Gets the value of this sensor. |
89 | * |
90 | * @return the value of this sensor |
91 | */ |
92 | public double getValue() { |
93 | return value; |
94 | } |
95 | |
96 | /** |
97 | * Compares two sensor inputs on super link equality. |
98 | * <p> |
99 | * Returns <code>true</code> if and only if the <code>SuperLinkID</code> |
100 | * specified sensor and this sensor are equal. |
101 | * |
102 | * @param sensor |
103 | * the input sensor |
104 | * @return <code>true</code> if sensors have matching SuperLinkID's |
105 | */ |
106 | public boolean equalsOnSuperLinkID(SensorInput sensor) { |
107 | return superLinkID.equals(sensor.superLinkID); |
108 | } |
109 | |
110 | /** |
111 | * Returns the id of this sensor. |
112 | * |
113 | * @return the id of this sensor |
114 | */ |
115 | public SuperLinkID getID() { |
116 | return superLinkID; |
117 | } |
118 | |
119 | /** |
120 | * Method hashes <i>only</i> on <code>SuperLinkID</code> |
121 | * |
122 | * @see java.lang.Object#hashCode() |
123 | */ |
124 | @Override |
125 | public int hashCode() { |
126 | final int PRIME = 31; |
127 | int result = 1; |
128 | result = PRIME * result |
129 | + ((superLinkID == null) ? 0 : superLinkID.hashCode()); |
130 | return result; |
131 | } |
132 | |
133 | /* |
134 | * (non-Javadoc) |
135 | * |
136 | * @see java.lang.Object#equals(java.lang.Object) |
137 | */ |
138 | @Override |
139 | public boolean equals(Object obj) { |
140 | if (this == obj) { |
141 | return true; |
142 | } |
143 | if (obj == null) { |
144 | return false; |
145 | } |
146 | if (getClass() != obj.getClass()) { |
147 | return false; |
148 | } |
149 | final SensorInput other = (SensorInput) obj; |
150 | if (superLinkID == null) { |
151 | if (other.superLinkID != null) { |
152 | return false; |
153 | } |
154 | } else if (!superLinkID.equals(other.superLinkID)) { |
155 | return false; |
156 | } |
157 | if (Double.doubleToLongBits(value) != Double |
158 | .doubleToLongBits(other.value)) { |
159 | return false; |
160 | } |
161 | return true; |
162 | } |
163 | |
164 | /* |
165 | * (non-Javadoc) |
166 | * |
167 | * @see java.lang.Object#toString() |
168 | */ |
169 | @Override |
170 | public String toString() { |
171 | return getClass().getSimpleName() + "[id=" + superLinkID |
172 | + ", value=" + value + "]"; |
173 | } |
174 | |
175 | } |