From 0bbeebb7dabc0b77ce78278f341800c10fa2d8f1 Mon Sep 17 00:00:00 2001 From: Dave Parker Date: Thu, 2 Aug 2018 10:34:14 -0400 Subject: [PATCH] Utility method Expression.evaluateObjectAsInt. --- prism/src/parser/ast/Expression.java | 24 ++++++++++++++++-------- 1 file changed, 16 insertions(+), 8 deletions(-) diff --git a/prism/src/parser/ast/Expression.java b/prism/src/parser/ast/Expression.java index 576d6dff..8fbe7e2d 100644 --- a/prism/src/parser/ast/Expression.java +++ b/prism/src/parser/ast/Expression.java @@ -251,14 +251,7 @@ public abstract class Expression extends ASTElement */ public int evaluateInt(EvaluateContext ec) throws PrismLangException { - Object o = evaluate(ec); - if (o instanceof Integer) { - return ((Integer) o).intValue(); - } - if (o instanceof Boolean) { - return ((Boolean) o).booleanValue() ? 1 : 0; - } - throw new PrismLangException("Cannot evaluate to an integer", this); + return evaluateObjectAsInt(evaluate(ec)); } /** @@ -343,6 +336,21 @@ public abstract class Expression extends ASTElement return evaluateInt(new EvaluateContextSubstate(constantValues, substate, varMap)); } + /** + * Evaluate this object as an integer. + * Any typing issues cause an exception (but: we do allow conversion of boolean to 0/1). + */ + public static int evaluateObjectAsInt(Object o) throws PrismLangException + { + if (o instanceof Integer) { + return ((Integer) o).intValue(); + } + if (o instanceof Boolean) { + return ((Boolean) o).booleanValue() ? 1 : 0; + } + throw new PrismLangException("Cannot evaluate " + o + " to an integer"); + } + /** * Evaluate this expression as a double. * Any typing issues cause an exception (but: we do allow conversion of boolean to 0.0/1.0).