package edu.stanford.rt.datatype;
/**
* @author Ninghui Li, Sandra Qiu
*
* Implementation of IntegerType
element
*/
public class IntegerType extends OrderedType
{
private long min;
private long max;
private boolean includeMin = true;
private boolean includeMax = true;
private long base;
private long step;
/**
* Constructor for IntegerType.
*/
public IntegerType(
String name,
long min,
long max,
boolean includeMin,
boolean includeMax,
long base,
long step)
throws IllegalArgumentException
{
super(name);
// Check to make sure that at least one value is legal
if (min > max)
{
throw new IllegalArgumentException("IntegerType: min > max");
}
if (base < min || base > max)
{
throw new IllegalArgumentException("IntegerType: base in not within min and max");
}
if (step <= 0)
{
throw new IllegalArgumentException("IntegerType: step <= 0");
}
long x = (min - base) % step;
if (x > 0 && (min + step - x) > max)
{
throw new IllegalArgumentException("IntegerType: 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;
}
/**
* Method getMin.
* @return long
*/
public long getMin()
{
return min;
}
/**
* Method getMax.
* @return long
*/
public long getMax()
{
return max;
}
/**
* Method isMinIncluded.
* @return boolean
*/
public boolean isMinIncluded()
{
return includeMin;
}
/**
* Method isMaxIncluded.
* @return boolean
*/
public boolean isMaxIncluded()
{
return includeMax;
}
/**
* Method getBase.
* @return long
*/
public long getBase()
{
return base;
}
/**
* Method getStep.
* @return long
*/
public long getStep()
{
return step;
}
/* (non-Javadoc)
* @see edu.stanford.rt.datatype.SimpleType#isValidValue(DataValue)
*/
public boolean isValidValue(DataValue v)
{
if (!(v instanceof IntegerValue))
{
return false;
}
long l = ((IntegerValue) v).getValue();
if (l >= min && l <= max && ((l - base) % step == 0))
{
return true;
}
return false;
}
/* (non-Javadoc)
* @see edu.stanford.rt.datatype.OrderedType#compares(DataValue, DataValue)
*/
public int compares(DataValue value1, DataValue value2)
{
if (!(value1 instanceof IntegerValue)
|| !(value2 instanceof IntegerValue))
throw new IllegalArgumentException("Wrong argument type");
long val1 = ((IntegerValue) value1).getValue();
long val2 = ((IntegerValue) value2).getValue();
if (val1 < val2)
return -1;
else if (val1 > val2)
return 1;
else
return 0;
}
/* (non-Javadoc)
* @see edu.stanford.rt.datatype.DataType#toString(String)
*/
public String toString(String indent)
{
String thisIndent = indent + " ";
StringBuffer sb = new StringBuffer();
sb.append(thisIndent).append("IntegerType: ").append(
getName());
if (includeMin)
sb.append(" [");
else
sb.append(" (");
sb.append(min);
sb.append(", ");
sb.append(max);
if (includeMax)
sb.append("]");
else
sb.append(") ");
if (base != 0)
sb.append("base=" + base);
if (step != 1)
sb.append(", step=" + step);
sb.append("\n");
return sb.toString();
}
}