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 * Get the value of this object.
37 public double getValue() {
42 * Set the value of this object.
44 * @param value the value to set
46 public void setValue(double value) {
52 * Get the value of this object in the current units.
54 * @return the value in the current units.
56 public double getUnitValue() {
57 return unit.toUnit(value);
62 * Set the value of this object in the current units.
64 * @param value the value in current units.
66 public void setUnitValue(double value) {
67 this.value = unit.fromUnit(value);
72 * Get the unit of this object.
76 public Unit getUnit() {
81 * Set the value of this object.
83 * @param unit the unit to set (<code>null</code> not allowed)
85 public void setUnit(Unit unit) {
87 throw new IllegalArgumentException("unit is null");
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
99 public String toString() {
100 return unit.toStringUnit(value);
106 * Compare this value to another value. The comparison is performed primarily by
107 * the unit text, secondarily the value in the unit values.
110 public int compareTo(Value o) {
111 int n = this.getUnit().getUnit().compareTo(o.getUnit().getUnit());
115 double us = this.getUnitValue();
116 double them = o.getUnitValue();
118 if (Double.isNaN(us)) {
119 if (Double.isNaN(them))
124 if (Double.isNaN(them))