1 package net.sf.openrocket.unit;
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.
9 * @author Sampo Niskanen <sampo.niskanen@iki.fi>
11 public class Value implements Comparable<Value> {
18 * Create a new Value object.
20 * @param value the value to set.
21 * @param unit the unit to set (<code>null</code> not allowed)
23 public Value(double value, Unit unit) {
25 throw new IllegalArgumentException("unit is null");
33 * Creates a new Value object using unit group. Currently it simply uses the default
34 * unit of the group, but may later change.
36 * @param value the value to set.
37 * @param group the group the value belongs to.
39 public Value(double value, UnitGroup group) {
40 this(value, group.getDefaultUnit());
45 * Get the value of this object.
49 public double getValue() {
54 * Set the value of this object.
56 * @param value the value to set
58 public void setValue(double value) {
64 * Get the value of this object in the current units.
66 * @return the value in the current units.
68 public double getUnitValue() {
69 return unit.toUnit(value);
74 * Set the value of this object in the current units.
76 * @param value the value in current units.
78 public void setUnitValue(double value) {
79 this.value = unit.fromUnit(value);
84 * Get the unit of this object.
88 public Unit getUnit() {
93 * Set the value of this object.
95 * @param unit the unit to set (<code>null</code> not allowed)
97 public void setUnit(Unit unit) {
99 throw new IllegalArgumentException("unit is null");
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
111 public String toString() {
112 return unit.toStringUnit(value);
118 * Compare this value to another value. The comparison is performed primarily by
119 * the unit text, secondarily the value in the unit values.
122 public int compareTo(Value o) {
123 int n = this.getUnit().getUnit().compareTo(o.getUnit().getUnit());
127 double us = this.getUnitValue();
128 double them = o.getUnitValue();
130 if (Double.isNaN(us)) {
131 if (Double.isNaN(them))
136 if (Double.isNaN(them))