updates for 0.9.4
[debian/openrocket] / src / net / sf / openrocket / unit / Value.java
1 package net.sf.openrocket.unit;
2
3 /**
4  * A class representing an SI value and a unit.  The toString() method yields the
5  * current value in the current units.  This class may be used to encapsulate
6  * a sortable value for example for tables.  The sorting is performed by the
7  * value in the current units, ignoring the unit.
8  * 
9  * @author Sampo Niskanen <sampo.niskanen@iki.fi>
10  */
11 public class Value implements Comparable<Value> {
12         
13         private double value;
14         private Unit unit;
15         
16         
17         /**
18          * Create a new Value object.
19          * 
20          * @param value         the value to set.
21          * @param unit          the unit to set (<code>null</code> not allowed)
22          */
23         public Value(double value, Unit unit) {
24                 if (unit == null) {
25                         throw new IllegalArgumentException("unit is null");
26                 }
27                 this.value = value;
28                 this.unit = unit;
29         }
30
31         
32         /**
33          * Get the value of this object.
34          * 
35          * @return the value
36          */
37         public double getValue() {
38                 return value;
39         }
40
41         /**
42          * Set the value of this object.
43          * 
44          * @param value the value to set
45          */
46         public void setValue(double value) {
47                 this.value = value;
48         }
49         
50         
51         /**
52          * Get the value of this object in the current units.
53          * 
54          * @return      the value in the current units.
55          */
56         public double getUnitValue() {
57                 return unit.toUnit(value);
58         }
59         
60         
61         /**
62          * Set the value of this object in the current units.
63          * 
64          * @param value         the value in current units.
65          */
66         public void setUnitValue(double value) {
67                 this.value = unit.fromUnit(value);
68         }
69         
70
71         /**
72          * Get the unit of this object.
73          * 
74          * @return the unit
75          */
76         public Unit getUnit() {
77                 return unit;
78         }
79
80         /**
81          * Set the value of this object.
82          * 
83          * @param unit the unit to set (<code>null</code> not allowed)
84          */
85         public void setUnit(Unit unit) {
86                 if (unit == null) {
87                         throw new IllegalArgumentException("unit is null");
88                 }
89                 this.unit = unit;
90         }
91         
92         
93         /**
94          * Return a string formatted using the {@link Unit#toStringUnit(double)} method
95          * of the current unit.  If the unit is <code>null</code> then the UNITS_NONE
96          * group is used.
97          */
98         @Override
99         public String toString() {
100                 return unit.toStringUnit(value);
101         }
102
103         
104         
105         /**
106          * Compare this value to another value.  The comparison is performed primarily by
107          * the unit text, secondarily the value in the unit values.
108          */
109         @Override
110         public int compareTo(Value o) {
111                 int n = this.getUnit().getUnit().compareTo(o.getUnit().getUnit());
112                 if (n != 0)
113                         return n;
114                 
115                 double us = this.getUnitValue();
116                 double them = o.getUnitValue();
117                 
118                 if (Double.isNaN(us)) {
119                         if (Double.isNaN(them))
120                                 return 0;
121                         else
122                                 return 1;
123                 }
124                 if (Double.isNaN(them))
125                         return -1;
126                 
127                 if (us < them)
128                         return -1;
129                 else if (us > them)
130                         return 1;
131                 else
132                         return 0;
133         }
134
135 }