1 package net.sf.openrocket.unit;
3 import net.sf.openrocket.util.MathUtil;
6 * A 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.
11 * @author Sampo Niskanen <sampo.niskanen@iki.fi>
13 public class Value implements Comparable<Value> {
20 * Create a new Value object.
22 * @param value the value to set.
23 * @param unit the unit to set (<code>null</code> not allowed)
25 public Value(double value, Unit unit) {
27 throw new IllegalArgumentException("unit is null");
35 * Creates a new Value object using unit group. Currently it simply uses the default
36 * unit of the group, but may later change.
38 * @param value the value to set.
39 * @param group the group the value belongs to.
41 public Value(double value, UnitGroup group) {
42 this(value, group.getDefaultUnit());
47 * Get the value of this object.
51 public double getValue() {
56 * Set the value of this object.
58 * @param value the value to set
60 public void setValue(double value) {
66 * Get the value of this object in the current units.
68 * @return the value in the current units.
70 public double getUnitValue() {
71 return unit.toUnit(value);
76 * Set the value of this object in the current units.
78 * @param value the value in current units.
80 public void setUnitValue(double value) {
81 this.value = unit.fromUnit(value);
86 * Get the unit of this object.
90 public Unit getUnit() {
95 * Set the value of this object.
97 * @param unit the unit to set (<code>null</code> not allowed)
99 public void setUnit(Unit unit) {
101 throw new IllegalArgumentException("unit is null");
108 * Return a string formatted using the {@link Unit#toStringUnit(double)} method
109 * of the current unit. If the unit is <code>null</code> then the UNITS_NONE
113 public String toString() {
114 return unit.toStringUnit(value);
120 public boolean equals(Object obj) {
125 if (getClass() != obj.getClass())
128 Value other = (Value) obj;
129 if (this.unit != other.unit) {
133 if (!MathUtil.equals(this.value, other.value)) {
142 public int hashCode() {
143 final int prime = 31;
145 result = prime * result + ((unit == null) ? 0 : unit.hashCode());
147 temp = Double.doubleToLongBits(value);
148 result = prime * result + (int) (temp ^ (temp >>> 32));
154 * Compare this value to another value. The comparison is performed primarily by
155 * the unit text, secondarily the value in the unit values.
158 public int compareTo(Value o) {
159 int n = this.getUnit().getUnit().compareTo(o.getUnit().getUnit());
163 double us = this.getUnitValue();
164 double them = o.getUnitValue();
166 if (Double.isNaN(us)) {
167 if (Double.isNaN(them))
172 if (Double.isNaN(them))