create changelog entry
[debian/openrocket] / core / src / net / sf / openrocket / unit / Value.java
1 package net.sf.openrocket.unit;
2
3 import net.sf.openrocket.util.MathUtil;
4
5 /**
6  * An immutable 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 final double value;
16         private final 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 (in SI units).
48          * 
49          * @return the value
50          */
51         public double getValue() {
52                 return value;
53         }
54         
55         
56
57         /**
58          * Get the value of this object in the current units.
59          * 
60          * @return      the value in the current units.
61          */
62         public double getUnitValue() {
63                 return unit.toUnit(value);
64         }
65         
66         
67         /**
68          * Get the unit of this object.
69          * 
70          * @return the unit
71          */
72         public Unit getUnit() {
73                 return unit;
74         }
75         
76         
77         /**
78          * Return a string formatted using the {@link Unit#toStringUnit(double)} method
79          * of the current unit.  If the unit is <code>null</code> then the UNITS_NONE
80          * group is used.
81          */
82         @Override
83         public String toString() {
84                 return unit.toStringUnit(value);
85         }
86         
87         
88
89         @Override
90         public boolean equals(Object obj) {
91                 if (this == obj)
92                         return true;
93                 if (obj == null)
94                         return false;
95                 if (getClass() != obj.getClass())
96                         return false;
97                 
98                 Value other = (Value) obj;
99                 if (this.unit != other.unit) {
100                         return false;
101                 }
102                 
103                 if (!MathUtil.equals(this.value, other.value)) {
104                         return false;
105                 }
106                 
107                 return true;
108         }
109         
110         
111         @Override
112         public int hashCode() {
113                 final int prime = 31;
114                 int result = 1;
115                 result = prime * result + ((unit == null) ? 0 : unit.hashCode());
116                 long temp;
117                 temp = Double.doubleToLongBits(value);
118                 result = prime * result + (int) (temp ^ (temp >>> 32));
119                 return result;
120         }
121         
122         
123         /**
124          * Compare this value to another value.  The comparison is performed primarily by
125          * the unit text, secondarily the value in the unit values.
126          */
127         @Override
128         public int compareTo(Value o) {
129                 int n = this.getUnit().getUnit().compareTo(o.getUnit().getUnit());
130                 if (n != 0)
131                         return n;
132                 
133                 double us = this.getUnitValue();
134                 double them = o.getUnitValue();
135                 
136                 if (Double.isNaN(us)) {
137                         if (Double.isNaN(them))
138                                 return 0;
139                         else
140                                 return 1;
141                 }
142                 if (Double.isNaN(them))
143                         return -1;
144                 
145                 if (us < them)
146                         return -1;
147                 else if (us > them)
148                         return 1;
149                 else
150                         return 0;
151         }
152         
153 }