[8780cbec] | 1 | |
---|
| 2 | package edu.stanford.rt.datatype; |
---|
| 3 | |
---|
| 4 | import edu.stanford.rt.util.*; |
---|
| 5 | |
---|
| 6 | /** |
---|
| 7 | DecimalType defines the decimal type. The name for this type is "DecimalType". |
---|
| 8 | It has the following attributes: min, max, base, and step. |
---|
| 9 | |
---|
| 10 | TODO |
---|
| 11 | */ |
---|
| 12 | public class DecimalType extends OrderedType |
---|
| 13 | { |
---|
| 14 | private double min; |
---|
| 15 | private double max; |
---|
| 16 | private boolean includeMin = true; |
---|
| 17 | private boolean includeMax = true; |
---|
| 18 | private double base; |
---|
| 19 | private double step; |
---|
| 20 | |
---|
| 21 | /** |
---|
| 22 | |
---|
| 23 | */ |
---|
| 24 | public DecimalType (String name, double min, double ma, |
---|
| 25 | boolean includeMin,boolean includeMax, |
---|
| 26 | double base, double step) |
---|
| 27 | throws IllegalArgumentException |
---|
| 28 | { |
---|
| 29 | super(name); |
---|
| 30 | |
---|
| 31 | //throw new UnsupportedException(); |
---|
| 32 | |
---|
| 33 | // Check to make sure that at least one value is legal |
---|
| 34 | if (min > max) { |
---|
| 35 | throw new IllegalArgumentException("DecimalType: min > max"); |
---|
| 36 | } |
---|
| 37 | if(base < min || base > max) { |
---|
| 38 | throw new IllegalArgumentException( |
---|
| 39 | "DecimalType: base in not within min and max"); |
---|
| 40 | } |
---|
| 41 | if (step <= 0) { |
---|
| 42 | throw new IllegalArgumentException("DecimalType: step <= 0"); |
---|
| 43 | } |
---|
| 44 | |
---|
| 45 | double x = (min - base) % step; |
---|
| 46 | if (x>0 && (min + step - x) > max) { |
---|
| 47 | throw new IllegalArgumentException( |
---|
| 48 | "DecimalType: First legal value is out of bound."); |
---|
| 49 | } |
---|
| 50 | |
---|
| 51 | this.min = min; |
---|
| 52 | this.max = max; |
---|
| 53 | this.includeMin = includeMin; |
---|
| 54 | this.includeMax = includeMax; |
---|
| 55 | this.base = base; |
---|
| 56 | this.step = step; |
---|
| 57 | } |
---|
| 58 | |
---|
| 59 | public double getMin() |
---|
| 60 | { |
---|
| 61 | return min; |
---|
| 62 | } |
---|
| 63 | public double getMax() |
---|
| 64 | { |
---|
| 65 | return max; |
---|
| 66 | } |
---|
| 67 | public boolean isMinIncluded() |
---|
| 68 | { |
---|
| 69 | return includeMin; |
---|
| 70 | } |
---|
| 71 | public boolean isMaxIncluded() |
---|
| 72 | { |
---|
| 73 | return includeMax; |
---|
| 74 | } |
---|
| 75 | public double getBase() |
---|
| 76 | { |
---|
| 77 | return base; |
---|
| 78 | } |
---|
| 79 | public double getStep() |
---|
| 80 | { |
---|
| 81 | return step; |
---|
| 82 | } |
---|
| 83 | |
---|
| 84 | public boolean isValidValue(DataValue v) |
---|
| 85 | { |
---|
| 86 | if (!(v instanceof DecimalValue)) { |
---|
| 87 | return false; |
---|
| 88 | } |
---|
| 89 | |
---|
| 90 | double l = ((DecimalValue)v).getValue(); |
---|
| 91 | if (l >= min && l<= max && ((l-base) % step == 0)) { |
---|
| 92 | return true; |
---|
| 93 | } |
---|
| 94 | return false; |
---|
| 95 | } |
---|
| 96 | |
---|
| 97 | public int compares(DataValue value1, DataValue value2) |
---|
| 98 | { |
---|
| 99 | if(!(value1 instanceof DecimalValue) || |
---|
| 100 | !(value2 instanceof DecimalValue) ) |
---|
| 101 | throw new IllegalArgumentException( |
---|
| 102 | "Wrong argument type"); |
---|
| 103 | |
---|
| 104 | double val1 = ((DecimalValue)value1).getValue(); |
---|
| 105 | double val2 = ((DecimalValue)value2).getValue(); |
---|
| 106 | |
---|
| 107 | if(val1 < val2) |
---|
| 108 | return -1; |
---|
| 109 | else if(val1 > val2) |
---|
| 110 | return 1; |
---|
| 111 | else |
---|
| 112 | return 0; |
---|
| 113 | } |
---|
| 114 | |
---|
| 115 | public String toString(String indent) |
---|
| 116 | { |
---|
| 117 | String thisIndent = indent+" "; |
---|
| 118 | return thisIndent + "DecimalType: \n"; |
---|
| 119 | } |
---|
| 120 | |
---|
| 121 | |
---|
| 122 | } |
---|