5b518673fc602eeacbfc0dcc3ff23bd9d2e7c47b
[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          * Creates a new Value object using unit group.  Currently it simply uses the default
34          * unit of the group, but may later change.
35          * 
36          * @param value         the value to set.
37          * @param group         the group the value belongs to.
38          */
39         public Value(double value, UnitGroup group) {
40                 this(value, group.getDefaultUnit());
41         }
42
43         
44         /**
45          * Get the value of this object.
46          * 
47          * @return the value
48          */
49         public double getValue() {
50                 return value;
51         }
52
53         /**
54          * Set the value of this object.
55          * 
56          * @param value the value to set
57          */
58         public void setValue(double value) {
59                 this.value = value;
60         }
61         
62         
63         /**
64          * Get the value of this object in the current units.
65          * 
66          * @return      the value in the current units.
67          */
68         public double getUnitValue() {
69                 return unit.toUnit(value);
70         }
71         
72         
73         /**
74          * Set the value of this object in the current units.
75          * 
76          * @param value         the value in current units.
77          */
78         public void setUnitValue(double value) {
79                 this.value = unit.fromUnit(value);
80         }
81         
82
83         /**
84          * Get the unit of this object.
85          * 
86          * @return the unit
87          */
88         public Unit getUnit() {
89                 return unit;
90         }
91
92         /**
93          * Set the value of this object.
94          * 
95          * @param unit the unit to set (<code>null</code> not allowed)
96          */
97         public void setUnit(Unit unit) {
98                 if (unit == null) {
99                         throw new IllegalArgumentException("unit is null");
100                 }
101                 this.unit = unit;
102         }
103         
104         
105         /**
106          * Return a string formatted using the {@link Unit#toStringUnit(double)} method
107          * of the current unit.  If the unit is <code>null</code> then the UNITS_NONE
108          * group is used.
109          */
110         @Override
111         public String toString() {
112                 return unit.toStringUnit(value);
113         }
114
115         
116         
117         /**
118          * Compare this value to another value.  The comparison is performed primarily by
119          * the unit text, secondarily the value in the unit values.
120          */
121         @Override
122         public int compareTo(Value o) {
123                 int n = this.getUnit().getUnit().compareTo(o.getUnit().getUnit());
124                 if (n != 0)
125                         return n;
126                 
127                 double us = this.getUnitValue();
128                 double them = o.getUnitValue();
129                 
130                 if (Double.isNaN(us)) {
131                         if (Double.isNaN(them))
132                                 return 0;
133                         else
134                                 return 1;
135                 }
136                 if (Double.isNaN(them))
137                         return -1;
138                 
139                 if (us < them)
140                         return -1;
141                 else if (us > them)
142                         return 1;
143                 else
144                         return 0;
145         }
146
147 }