Browse Source

Type: provide castFromBigRational conversion to Java data types

master
Joachim Klein 8 years ago
committed by Dave Parker
parent
commit
1af1a6f414
  1. 15
      prism/src/parser/type/Type.java
  2. 8
      prism/src/parser/type/TypeBool.java
  3. 7
      prism/src/parser/type/TypeDouble.java
  4. 8
      prism/src/parser/type/TypeInt.java

15
prism/src/parser/type/Type.java

@ -26,6 +26,7 @@
package parser.type; package parser.type;
import param.BigRational;
import prism.PrismLangException; import prism.PrismLangException;
public abstract class Type public abstract class Type
@ -65,6 +66,20 @@ public abstract class Type
throw new PrismLangException("Cannot cast a value to type " + getTypeString()); throw new PrismLangException("Cannot cast a value to type " + getTypeString());
} }
/**
* Cast a BigRational value to the Java data type (Boolean, Integer, Double, ...)
* corresponding to this type.
* <br>
* For boolean and integer, this throws an exception if the value can not be
* precisely represented by the Java data type; for double, loss of precision
* is expected and does not raise an exception.
*/
public Object castFromBigRational(BigRational value) throws PrismLangException
{
// Play safe: assume error unless explicitly overridden.
throw new PrismLangException("Cannot cast rational number to type " + getTypeString());
}
@Override @Override
public String toString() public String toString()
{ {

8
prism/src/parser/type/TypeBool.java

@ -26,6 +26,7 @@
package parser.type; package parser.type;
import param.BigRational;
import prism.PrismLangException; import prism.PrismLangException;
public class TypeBool extends Type public class TypeBool extends Type
@ -77,4 +78,11 @@ public class TypeBool extends Type
else else
throw new PrismLangException("Can't convert " + value.getClass() + " to type " + getTypeString()); throw new PrismLangException("Can't convert " + value.getClass() + " to type " + getTypeString());
} }
@Override
public Object castFromBigRational(BigRational value) throws PrismLangException
{
return value.toBoolean();
}
} }

7
prism/src/parser/type/TypeDouble.java

@ -82,4 +82,11 @@ public class TypeDouble extends Type
else else
throw new PrismLangException("Can't convert " + value.getClass() + " to type " + getTypeString()); throw new PrismLangException("Can't convert " + value.getClass() + " to type " + getTypeString());
} }
@Override
public Object castFromBigRational(BigRational value) throws PrismLangException
{
return value.doubleValue();
}
} }

8
prism/src/parser/type/TypeInt.java

@ -26,6 +26,7 @@
package parser.type; package parser.type;
import param.BigRational;
import prism.PrismLangException; import prism.PrismLangException;
public class TypeInt extends Type public class TypeInt extends Type
@ -77,4 +78,11 @@ public class TypeInt extends Type
else else
throw new PrismLangException("Can't convert " + value.getClass() + " to type " + getTypeString()); throw new PrismLangException("Can't convert " + value.getClass() + " to type " + getTypeString());
} }
@Override
public Object castFromBigRational(BigRational value) throws PrismLangException
{
return value.toInt();
}
} }
Loading…
Cancel
Save