1
2 package edu.iris.Fissures.model;
3
4 import java.io.Serializable;
5 import java.text.NumberFormat;
6 import edu.iris.Fissures.Quantity;
7 import edu.iris.Fissures.Unit;
8
9 /***
10 * QuantityImpl.java
11 *
12 *
13 * Created: Wed Sep 1 17:43:50 1999
14 *
15 * @author Philip Crotwell
16 * @version
17 */
18
19 public class QuantityImpl extends Quantity {
20 protected QuantityImpl() {}
21
22 public static Serializable createEmpty() {
23 return new QuantityImpl();
24 }
25
26 public QuantityImpl(double f, Unit the_unit ) {
27 value = f;
28 the_units = the_unit;
29 }
30
31 public static QuantityImpl createQuantityImpl(Quantity q) {
32 if (q instanceof QuantityImpl) {
33 return (QuantityImpl)q;
34 }
35 return new QuantityImpl(q.value,
36 UnitImpl.createUnitImpl(q.the_units));
37 }
38
39 public void setFormat(NumberFormat format) {
40 this.format = format;
41 }
42
43 public double getValue() {
44 return value;
45 }
46
47 public double getValue(UnitImpl unit){
48 return convertTo(unit).getValue();
49 }
50
51 public double get_value() {
52 return getValue();
53 }
54
55 public UnitImpl getUnit() {
56 return (UnitImpl)the_units;
57 }
58
59 public UnitImpl get_unit() {
60 return getUnit();
61 }
62
63 /*** converts this Quantity into the given units.
64 * @returns a new quantity with the given units and its value
65 * correspondingly adjusted.
66 * @throws IllegalArgumentException if the given units are
67 * not compatible.
68 */
69 public QuantityImpl convertTo(UnitImpl newUnit) {
70 UnitImpl currUnit = getUnit();
71 if (currUnit.equals(newUnit)) {
72 return this;
73 } else if ( ! currUnit.isConvertableTo(newUnit)) {
74 throw new IllegalArgumentException("Cannot convert, units are not compatible, "+
75 currUnit+" and "+newUnit);
76 } else {
77 double mulfac = currUnit.getTotalMultiFactor() /
78 newUnit.getTotalMultiFactor();
79 int powerDiff = currUnit.getTotalPower() -
80 newUnit.getTotalPower();
81
82
83
84
85 double newValue = getValue() *mulfac;
86 if (powerDiff != 0) {
87 newValue *= Math.pow(10, powerDiff);
88 }
89 return new QuantityImpl(newValue, newUnit);
90 }
91 }
92
93 public QuantityImpl add(QuantityImpl q) {
94 double val = q.convertTo(getUnit()).getValue();
95 return new QuantityImpl(getValue() + val, getUnit());
96 }
97
98 public QuantityImpl subtract(QuantityImpl q) {
99 double val = q.convertTo(getUnit()).getValue();
100 return new QuantityImpl(getValue() - val, getUnit());
101 }
102
103 public QuantityImpl multiplyBy(QuantityImpl q) {
104 double val = q.getValue();
105 return new QuantityImpl(getValue() * val,
106 UnitImpl.multiply(getUnit(), q.getUnit()));
107 }
108
109 public QuantityImpl divideBy(QuantityImpl q) {
110 double val = q.getValue();
111 return new QuantityImpl(getValue() / val,
112 UnitImpl.divide(getUnit(), q.getUnit()));
113 }
114
115 public QuantityImpl multiplyBy(double f) {
116 return new QuantityImpl(getValue() * f,
117 getUnit());
118 }
119
120 public QuantityImpl divideBy(double f) {
121 return new QuantityImpl(getValue() / f,
122 getUnit());
123 }
124
125 public QuantityImpl inverse() {
126 return new QuantityImpl(1.0 / getValue(),
127 getUnit().inverse());
128 }
129
130 public String toString() {
131 if (format == null) {
132 return getValue()+" "+getUnit();
133 } else {
134 return format.format(getValue())+" "+getUnit();
135 }
136 }
137
138 public boolean greaterThan(QuantityImpl q) {
139 double val = q.convertTo(getUnit()).getValue();
140 return (getValue() > val);
141 }
142
143 public boolean greaterThanEqual(QuantityImpl q) {
144 double val = q.convertTo(getUnit()).getValue();
145 return (getValue() >= val);
146 }
147
148 public boolean lessThan(QuantityImpl q) {
149 double val = q.convertTo(getUnit()).getValue();
150 return (getValue() < val);
151 }
152
153 public boolean lessThanEqual(QuantityImpl q) {
154 double val = q.convertTo(getUnit()).getValue();
155 return (getValue() <= val);
156 }
157 public int hashCode(){
158 int result = 22;
159 long valBits = Double.doubleToLongBits(value);
160 result = 37*result + (int)(valBits^(valBits>>>32));
161 result = 37*result + the_units.hashCode();
162 return result;
163 }
164
165 public boolean equals(Object o) {
166 if (super.equals(o)) return true;
167 if (o instanceof QuantityImpl) {
168 QuantityImpl q = (QuantityImpl)o;
169 if (getUnit().isConvertableTo(q.getUnit()))
170 if(q.convertTo(getUnit()).getValue() == getValue())
171 return true;
172 }
173 return false;
174 }
175
176 protected NumberFormat format = null;
177
178 public static void main(String[] args) {
179
180 QuantityImpl q = new QuantityImpl(5, UnitImpl.HOUR);
181 QuantityImpl halfq = new QuantityImpl(2.5, UnitImpl.HOUR);
182 System.out.println("q="+q);
183 System.out.println("q.convertTo(UnitImpl.SECOND)="+q.convertTo(UnitImpl.SECOND));
184 System.out.println("q.inverse="+q.inverse());
185 System.out.println("q.add(q)="+q.add(q));
186 System.out.println("q.subtract(halfq)="+q.subtract(halfq));
187
188 System.out.println("q.convertTo(MICROSECOND)="+q.convertTo(UnitImpl.MICROSECOND));
189 QuantityImpl p = new QuantityImpl(10, UnitImpl.METER);
190 System.out.println("p="+p);
191 System.out.println("p.divideBy(q)="+p.divideBy(q));
192 System.out.println("p.multiplyBy(q)="+p.multiplyBy(q));
193 }
194
195 }