package edu.stanford.rt.datatype; import edu.stanford.rt.util.*; /** DecimalType defines the decimal type. The name for this type is "DecimalType". It has the following attributes: min, max, base, and step. TODO */ public class DecimalType extends OrderedType { private double min; private double max; private boolean includeMin = true; private boolean includeMax = true; private double base; private double step; /** */ public DecimalType (String name, double min, double ma, boolean includeMin,boolean includeMax, double base, double step) throws IllegalArgumentException { super(name); //throw new UnsupportedException(); // Check to make sure that at least one value is legal if (min > max) { throw new IllegalArgumentException("DecimalType: min > max"); } if(base < min || base > max) { throw new IllegalArgumentException( "DecimalType: base in not within min and max"); } if (step <= 0) { throw new IllegalArgumentException("DecimalType: step <= 0"); } double x = (min - base) % step; if (x>0 && (min + step - x) > max) { throw new IllegalArgumentException( "DecimalType: First legal value is out of bound."); } this.min = min; this.max = max; this.includeMin = includeMin; this.includeMax = includeMax; this.base = base; this.step = step; } public double getMin() { return min; } public double getMax() { return max; } public boolean isMinIncluded() { return includeMin; } public boolean isMaxIncluded() { return includeMax; } public double getBase() { return base; } public double getStep() { return step; } public boolean isValidValue(DataValue v) { if (!(v instanceof DecimalValue)) { return false; } double l = ((DecimalValue)v).getValue(); if (l >= min && l<= max && ((l-base) % step == 0)) { return true; } return false; } public int compares(DataValue value1, DataValue value2) { if(!(value1 instanceof DecimalValue) || !(value2 instanceof DecimalValue) ) throw new IllegalArgumentException( "Wrong argument type"); double val1 = ((DecimalValue)value1).getValue(); double val2 = ((DecimalValue)value2).getValue(); if(val1 < val2) return -1; else if(val1 > val2) return 1; else return 0; } public String toString(String indent) { String thisIndent = indent+" "; return thisIndent + "DecimalType: \n"; } }