bug fixes and rocket optimization
[debian/openrocket] / src / net / sf / openrocket / unit / Value.java
1 package net.sf.openrocket.unit;
2
3 import net.sf.openrocket.util.MathUtil;
4
5 /**
6  * A class representing an SI value and a unit.  The toString() method yields the
7  * current value in the current units.  This class may be used to encapsulate
8  * a sortable value for example for tables.  The sorting is performed by the
9  * value in the current units, ignoring the unit.
10  * 
11  * @author Sampo Niskanen <sampo.niskanen@iki.fi>
12  */
13 public class Value implements Comparable<Value> {
14         
15         private double value;
16         private Unit unit;
17         
18         
19         /**
20          * Create a new Value object.
21          * 
22          * @param value         the value to set.
23          * @param unit          the unit to set (<code>null</code> not allowed)
24          */
25         public Value(double value, Unit unit) {
26                 if (unit == null) {
27                         throw new IllegalArgumentException("unit is null");
28                 }
29                 this.value = value;
30                 this.unit = unit;
31         }
32         
33         
34         /**
35          * Creates a new Value object using unit group.  Currently it simply uses the default
36          * unit of the group, but may later change.
37          * 
38          * @param value         the value to set.
39          * @param group         the group the value belongs to.
40          */
41         public Value(double value, UnitGroup group) {
42                 this(value, group.getDefaultUnit());
43         }
44         
45         
46         /**
47          * Get the value of this object.
48          * 
49          * @return the value
50          */
51         public double getValue() {
52                 return value;
53         }
54         
55         /**
56          * Set the value of this object.
57          * 
58          * @param value the value to set
59          */
60         public void setValue(double value) {
61                 this.value = value;
62         }
63         
64         
65         /**
66          * Get the value of this object in the current units.
67          * 
68          * @return      the value in the current units.
69          */
70         public double getUnitValue() {
71                 return unit.toUnit(value);
72         }
73         
74         
75         /**
76          * Set the value of this object in the current units.
77          * 
78          * @param value         the value in current units.
79          */
80         public void setUnitValue(double value) {
81                 this.value = unit.fromUnit(value);
82         }
83         
84         
85         /**
86          * Get the unit of this object.
87          * 
88          * @return the unit
89          */
90         public Unit getUnit() {
91                 return unit;
92         }
93         
94         /**
95          * Set the value of this object.
96          * 
97          * @param unit the unit to set (<code>null</code> not allowed)
98          */
99         public void setUnit(Unit unit) {
100                 if (unit == null) {
101                         throw new IllegalArgumentException("unit is null");
102                 }
103                 this.unit = unit;
104         }
105         
106         
107         /**
108          * Return a string formatted using the {@link Unit#toStringUnit(double)} method
109          * of the current unit.  If the unit is <code>null</code> then the UNITS_NONE
110          * group is used.
111          */
112         @Override
113         public String toString() {
114                 return unit.toStringUnit(value);
115         }
116         
117         
118
119         @Override
120         public boolean equals(Object obj) {
121                 if (this == obj)
122                         return true;
123                 if (obj == null)
124                         return false;
125                 if (getClass() != obj.getClass())
126                         return false;
127                 
128                 Value other = (Value) obj;
129                 if (this.unit != other.unit) {
130                         return false;
131                 }
132                 
133                 if (!MathUtil.equals(this.value, other.value)) {
134                         return false;
135                 }
136                 
137                 return true;
138         }
139         
140         
141         @Override
142         public int hashCode() {
143                 final int prime = 31;
144                 int result = 1;
145                 result = prime * result + ((unit == null) ? 0 : unit.hashCode());
146                 long temp;
147                 temp = Double.doubleToLongBits(value);
148                 result = prime * result + (int) (temp ^ (temp >>> 32));
149                 return result;
150         }
151         
152         
153         /**
154          * Compare this value to another value.  The comparison is performed primarily by
155          * the unit text, secondarily the value in the unit values.
156          */
157         @Override
158         public int compareTo(Value o) {
159                 int n = this.getUnit().getUnit().compareTo(o.getUnit().getUnit());
160                 if (n != 0)
161                         return n;
162                 
163                 double us = this.getUnitValue();
164                 double them = o.getUnitValue();
165                 
166                 if (Double.isNaN(us)) {
167                         if (Double.isNaN(them))
168                                 return 0;
169                         else
170                                 return 1;
171                 }
172                 if (Double.isNaN(them))
173                         return -1;
174                 
175                 if (us < them)
176                         return -1;
177                 else if (us > them)
178                         return 1;
179                 else
180                         return 0;
181         }
182         
183 }