/** * 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; } }
private static Literal operationsBetweenDurationAndDecimal(Literal durationLit, Literal decimalLit, MathOp op) { Duration duration = XMLDatatypeUtil.parseDuration(durationLit.getLabel()); try { if(op == MathOp.MULTIPLY) { //op:multiply-dayTimeDuration and op:multiply-yearMonthDuration return buildLiteral(duration.multiply(decimalLit.decimalValue())); } else { throw new ValueExprEvaluationException("Only multiplication is defined between xsd:decimal and xsd:duration."); } } catch (IllegalStateException e) { throw new ValueExprEvaluationException(e); } }
private static Literal operationsBetweenDurationAndDecimal(Literal durationLit, Literal decimalLit, MathOp op) { Duration duration = XMLDatatypeUtil.parseDuration(durationLit.getLabel()); try { if(op == MathOp.MULTIPLY) { //op:multiply-dayTimeDuration and op:multiply-yearMonthDuration return buildLiteral(duration.multiply(decimalLit.decimalValue())); } else { throw new ValueExprEvaluationException("Only multiplication is defined between xsd:decimal and xsd:duration."); } } catch (IllegalStateException e) { throw new ValueExprEvaluationException(e); } }
BigDecimal absoluteValue = literal.decimalValue().abs();
BigDecimal absoluteValue = literal.decimalValue().abs();
BigDecimal ceilingValue = literal.decimalValue().setScale(0, RoundingMode.CEILING); return valueFactory.createLiteral(ceilingValue.toPlainString(), datatype);
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);
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);
BigDecimal ceilingValue = literal.decimalValue().setScale(0, RoundingMode.CEILING); return valueFactory.createLiteral(ceilingValue.toPlainString(), datatype);
BigDecimal decimalValue = literal.decimalValue(); booleanValue = !decimalValue.equals(BigDecimal.ZERO);
BigDecimal decimalValue = literal.decimalValue(); booleanValue = !decimalValue.equals(BigDecimal.ZERO);
@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); } }
protected Literal convert(ValueFactory valueFactory, Value value) throws ValueExprEvaluationException { if (value instanceof Literal) { Literal literal = (Literal)value; IRI datatype = literal.getDatatype(); if (XMLDatatypeUtil.isNumericDatatype(datatype)) { // FIXME: floats and doubles must be processed separately, see // http://www.w3.org/TR/xpath-functions/#casting-from-primitive-to-primitive try { BigDecimal decimalValue = literal.decimalValue(); return valueFactory.createLiteral(decimalValue.toPlainString(), XMLSchema.DECIMAL); } catch (NumberFormatException e) { throw typeError(literal, e); } } else if (datatype.equals(XMLSchema.BOOLEAN)) { try { return valueFactory.createLiteral(literal.booleanValue() ? "1.0" : "0.0", XMLSchema.DECIMAL); } catch (IllegalArgumentException e) { throw typeError(literal, e); } } } throw typeError(value, null); }
protected Literal convert(ValueFactory valueFactory, Value value) throws ValueExprEvaluationException { if (value instanceof Literal) { Literal literal = (Literal)value; IRI datatype = literal.getDatatype(); if (XMLDatatypeUtil.isNumericDatatype(datatype)) { // FIXME: floats and doubles must be processed separately, see // http://www.w3.org/TR/xpath-functions/#casting-from-primitive-to-primitive try { BigDecimal decimalValue = literal.decimalValue(); return valueFactory.createLiteral(decimalValue.toPlainString(), XMLSchema.DECIMAL); } catch (NumberFormatException e) { throw typeError(literal, e); } } else if (datatype.equals(XMLSchema.BOOLEAN)) { try { return valueFactory.createLiteral(literal.booleanValue() ? "1.0" : "0.0", XMLSchema.DECIMAL); } catch (IllegalArgumentException e) { throw typeError(literal, e); } } } throw typeError(value, null); }
final BigDecimal sum = MathUtil.compute(oldSum, childLiteral, MathOp.PLUS).decimalValue();
integerValue = literal.decimalValue().toBigInteger();
@Override public Value evaluate(final ValueFactory valueFactory, final Value... args) throws ValueExprEvaluationException { if (args.length != 1) { throw new ValueExprEvaluationException("isTeen() requires exactly 1 argument, got " + args.length); } if (args[0] instanceof Literal) { final Literal literal = (Literal) args[0]; final IRI datatype = literal.getDatatype(); // ABS function accepts only numeric literals if (datatype != null && XMLDatatypeUtil.isNumericDatatype(datatype)) { if (XMLDatatypeUtil.isDecimalDatatype(datatype)) { final BigDecimal bigValue = literal.decimalValue(); return BooleanLiteral.valueOf(bigValue.compareTo(new BigDecimal(TEEN_THRESHOLD)) < 0); } else if (XMLDatatypeUtil.isFloatingPointDatatype(datatype)) { final double doubleValue = literal.doubleValue(); return BooleanLiteral.valueOf(doubleValue < TEEN_THRESHOLD); } else { throw new ValueExprEvaluationException( "unexpected datatype (expect decimal/int or floating) for function operand: " + args[0]); } } else { throw new ValueExprEvaluationException( "unexpected input value (expect non-null and numeric) for function: " + args[0]); } } else { throw new ValueExprEvaluationException("unexpected input value (expect literal) for function: " + args[0]); } } };