1
2 package edu.iris.Fissures.model;
3
4 import java.io.Serializable;
5 import edu.iris.Fissures.Unit;
6 import edu.iris.Fissures.UnitRange;
7
8 /***
9 * UnitRangeImpl.java
10 *
11 *
12 * Created: Mon Sep 6 16:04:23 1999
13 *
14 * @author Philip Crotwell
15 * @version
16 */
17
18 public class UnitRangeImpl extends edu.iris.Fissures.UnitRange {
19
20 protected UnitRangeImpl() {}
21
22 public static Serializable createEmpty() { return new UnitRangeImpl(); }
23
24 public UnitRangeImpl(double min, double max, Unit the_units) {
25 if (the_units==null) {
26 throw new IllegalArgumentException("Unit must not be null.");
27 }
28 if (min > max) {
29 throw new IllegalArgumentException("min > max");
30 }
31 this.min_value = min;
32 this.max_value = max;
33 this.the_units = the_units;
34 }
35
36 public double getMinValue() { return min_value; }
37
38 public double getMaxValue() { return max_value; }
39
40 public UnitImpl getUnit() { return (UnitImpl)the_units; }
41
42 /*** converts this UnitRange into the given units.
43 * @returns a new quantity with the given units and its value
44 * correspondingly adjusted.
45 * @throws IllegalArgumentException if the given units are
46 * not compatible.
47 */
48 public UnitRangeImpl convertTo(UnitImpl newUnit) {
49 UnitImpl currUnit = getUnit();
50
51 if (newUnit.equals(currUnit)) {
52 return this;
53 } else if ( ! currUnit.isConvertableTo(newUnit)) {
54 throw new IllegalArgumentException("Cannot convert, units are not compatible");
55 }
56
57 double mulfac = currUnit.getTotalMultiFactor() /
58 newUnit.getTotalMultiFactor();
59 int powerDiff = currUnit.getTotalPower() -
60 newUnit.getTotalPower();
61 double newMinValue =
62 getMinValue() * mulfac * Math.pow(10, powerDiff);
63 double newMaxValue =
64 getMaxValue() * mulfac * Math.pow(10, powerDiff);
65 return new UnitRangeImpl(newMinValue, newMaxValue, newUnit);
66 }
67
68 public String toString() {
69 return "("+getMinValue()+","+getMaxValue()+") "+getUnit();
70 }
71
72 public boolean equals(Object o){
73 if(o == this){ return true; }
74 if(o instanceof UnitRangeImpl){
75 UnitRange oRange = (UnitRange)o;
76 return oRange.min_value == min_value && oRange.max_value == max_value &&
77 the_units.equals(oRange.the_units);
78 }
79 return false;
80 }
81
82 public int hashCode(){
83 int result = 123;
84 long minBits = Double.doubleToLongBits(min_value);
85 result = (int)(minBits^(minBits>>>32)) + 37 * result;
86 long maxBits = Double.doubleToLongBits(max_value);
87 result = (int)(maxBits^(maxBits>>>32)) + 37 * result;
88 return getUnit().hashCode() + result * 37;
89 }
90 }