@Override TemplateModel _eval(Environment env) throws TemplateException { TemplateNumberModel targetModel = null; TemplateModel tm = target.eval(env); try { targetModel = (TemplateNumberModel) tm; } catch (ClassCastException cce) { throw new NonNumericalException(target, tm, env); } if (!isMinus) { return targetModel; } target.assertNonNull(targetModel, env); Number n = targetModel.getAsNumber(); n = ArithmeticEngine.CONSERVATIVE_ENGINE.multiply(MINUS_ONE, n); return new SimpleNumber(n); }
static Number getNumber(TemplateNumberModel model, Expression expr, Environment env) throws TemplateModelException, TemplateException { Number value = model.getAsNumber(); if (value == null) { throw new TemplateException(expr + " evaluated to null number.", env); } return value; }
/** * Override the <code>Object.equals</code> method. * * @param o the object we're comparing against * @return <code>true</code> if the two objects are equal, otherwise <code>false</code> */ public boolean equals(Object o) { if (this == o) { return true; } if (!(o instanceof NumberLiteral)) { return false; } final NumberLiteral numberLiteral = (NumberLiteral) o; if (value == null) { return numberLiteral.value == null; } else { return value.equals(numberLiteral.value); } }
/** * Override the <code>Object.hashCode</code> method. * * @return a hash code corresponding to the value of this object */ public int hashCode() { return value.hashCode() + 17; }
/** * Override the <code>Object.toString</code> method. * * @return a <code>String</code> representation of this expression */ public String toString() { return value.toString(); }