/** * Returns the decimal representation of a given literal. * If the literal cannot be converted in a decimal number (e.g. null or NaN) then a default value is returned. * * @param literal the literal. * @param defaultValue the default value that will be returned in case of errors. * @return the decimal representation of a given literal. */ BigDecimal asDecimal(final Literal literal, final BigDecimal defaultValue) { try { return literal.decimalValue(); } catch (final Exception exception) { return defaultValue; } }
public BigDecimal deserialize(Literal literal) { return literal.decimalValue(); }
public BigDecimal decimalValue() { return lit.decimalValue(); }
public BigDecimal deserialize(Literal literal) { return literal.decimalValue(); }
public BigDecimal deserialize(Literal literal) { return literal.decimalValue(); }
/** * Returns the decimal representation of a given literal. * If the literal cannot be converted in a decimal number (e.g. null or NaN) then a default value is returned. * * @param literal the literal. * @param defaultValue the default value that will be returned in case of errors. * @return the decimal representation of a given literal. */ BigDecimal asDecimal(final Literal literal, final BigDecimal defaultValue) { try { return literal.decimalValue(); } catch (final Exception exception) { return defaultValue; } }
/** * Gets the decimal value of the supplied literal. The fallback value is * returned in case {@link Literal#decimalValue()} throws a * {@link NumberFormatException}. * * @param l * The literal to get the decimal value for. * @param fallback * The value to fall back to in case no decimal value could gotten * from the literal. * @return Either the literal's decimal value, or the fallback value. */ public static BigDecimal getDecimalValue(Literal l, BigDecimal fallback) { try { return l.decimalValue(); } catch (NumberFormatException e) { return fallback; } }
/** * Gets the decimal value of the supplied literal. The fallback value is * returned in case {@link Literal#decimalValue()} throws a * {@link NumberFormatException}. * * @param l * The literal to get the decimal value for. * @param fallback * The value to fall back to in case no decimal value could gotten * from the literal. * @return Either the literal's decimal value, or the fallback value. */ public static BigDecimal getDecimalValue(Literal l, BigDecimal fallback) { try { return l.decimalValue(); } catch (NumberFormatException e) { return fallback; } }
/** * Implements {@link Literal#decimalValue()}. */ @Override public BigDecimal decimalValue() { if (!isLiteral()) throw new ClassCastException(); return ((Literal) getValue()).decimalValue(); }
@Override public BigDecimal decimalValue(Value node) { try { return ((Literal)node).decimalValue(); } catch (ClassCastException e) { throw new IllegalArgumentException("Value "+node.stringValue()+" is not a literal" + "but of type "+debugType(node)); } }
@Override public BigDecimal decimalValue(Value node) { try { return ((Literal)node).decimalValue(); } catch (ClassCastException e) { throw new IllegalArgumentException("Value "+node.stringValue()+" is not a literal" + "but of type "+debugType(node)); } }
/** * Implements {@link Literal#decimalValue()}. */ @Override public BigDecimal decimalValue() { if (!isLiteral()) throw new ClassCastException(); return ((Literal) getValue()).decimalValue(); }
@Override public BigDecimal decimalValue(Value node) { try { return ((Literal)node).decimalValue(); } catch (ClassCastException e) { throw new IllegalArgumentException("Value "+node.stringValue()+" is not a literal" + "but of type "+debugType(node)); } }
@Override public BigDecimal decimalValue(Value node) { try { return ((Literal)node).decimalValue(); } catch (ClassCastException e) { throw new IllegalArgumentException("Value "+node.stringValue()+" is not a literal" + "but of type "+debugType(node)); } }
public static BigDecimal dec(final Object arg) { return ((Literal) evaluate(new DecimalCast(), arg)).decimalValue(); }
@Override public Number findNumber(final Literal instance, final Datatype datatype) { if (null == datatype) { throw new IllegalArgumentException("numeric value is of unknown class: " + instance.getClass()); } switch (datatype) { case DECIMAL: return instance.decimalValue(); case DOUBLE: return doubleValue(instance); case FLOAT: return instance.floatValue(); case INTEGER: return instance.intValue(); case LONG: return instance.longValue(); default: throw new IllegalStateException(); } } }
BigDecimal absoluteValue = literal.decimalValue().abs();
BigDecimal floor = literal.decimalValue().setScale(0, RoundingMode.FLOOR); return valueFactory.createLiteral(floor.toPlainString(), datatype);
BigDecimal rounded = literal.decimalValue().setScale(0, RoundingMode.HALF_UP); return valueFactory.createLiteral(rounded.toPlainString(), datatype);
@Override protected Value evaluate(ValueFactory valueFactory, Value arg1, Value arg2) throws ValueExprEvaluationException { if (!(arg1 instanceof Literal) || !(arg2 instanceof Literal)) { throw new ValueExprEvaluationException("Both arguments must be literals"); } Literal number = (Literal) arg1; Literal format = (Literal) arg2; java.text.DecimalFormat formatter = new java.text.DecimalFormat(format.getLabel()); String value; if (XMLSchema.INT.equals(number.getDatatype()) || XMLSchema.LONG.equals(number.getDatatype()) || XMLSchema.SHORT.equals(number.getDatatype()) || XMLSchema.BYTE.equals(number.getDatatype())) { value = formatter.format(number.longValue()); } else if (XMLSchema.DECIMAL.equals(number.getDatatype())) { value = formatter.format(number.decimalValue()); } else if (XMLSchema.INTEGER.equals(number.getDatatype())) { value = formatter.format(number.integerValue()); } else { value = formatter.format(number.doubleValue()); } return valueFactory.createLiteral(value); } }