Round.java

/*******************************************************************************
 * Copyright (c) 2015 Eclipse RDF4J contributors, Aduna, and others.
 *
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Distribution License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/org/documents/edl-v10.php.
 *
 * SPDX-License-Identifier: BSD-3-Clause
 *******************************************************************************/
package org.eclipse.rdf4j.query.algebra.evaluation.function.numeric;

import java.math.BigDecimal;
import java.math.RoundingMode;

import org.eclipse.rdf4j.model.Literal;
import org.eclipse.rdf4j.model.Value;
import org.eclipse.rdf4j.model.ValueFactory;
import org.eclipse.rdf4j.model.base.CoreDatatype;
import org.eclipse.rdf4j.model.vocabulary.FN;
import org.eclipse.rdf4j.query.algebra.evaluation.ValueExprEvaluationException;
import org.eclipse.rdf4j.query.algebra.evaluation.function.Function;

/**
 * The SPARQL built-in {@link Function} ROUND, as defined in
 * <a href="http://www.w3.org/TR/sparql11-query/#func-round">SPARQL Query Language for RDF</a>
 *
 * @author Jeen Broekstra
 */
public class Round implements Function {

	@Override
	public String getURI() {
		return FN.NUMERIC_ROUND.toString();
	}

	@Override
	public Literal evaluate(ValueFactory valueFactory, Value... args) throws ValueExprEvaluationException {
		if (args.length != 1) {
			throw new ValueExprEvaluationException("ROUND requires exactly 1 argument, got " + args.length);
		}

		if (args[0] instanceof Literal) {
			Literal literal = (Literal) args[0];

			CoreDatatype.XSD datatype = literal.getCoreDatatype().asXSDDatatypeOrNull();

			// function accepts only numeric literals
			if (datatype != null && datatype.isNumericDatatype()) {
				if (datatype.isIntegerDatatype()) {
					return literal;
				} else if (datatype.isDecimalDatatype()) {
					BigDecimal rounded = literal.decimalValue().setScale(0, RoundingMode.HALF_UP);
					return valueFactory.createLiteral(rounded.toPlainString(), datatype);
				} else if (datatype.isFloatingPointDatatype()) {
					double ceilingValue = Math.round(literal.doubleValue());
					return valueFactory.createLiteral(Double.toString(ceilingValue), datatype);
				} else {
					throw new ValueExprEvaluationException("unexpected datatype for function operand: " + args[0]);
				}
			} else {
				throw new ValueExprEvaluationException("unexpected input value for function: " + args[0]);
			}
		} else {
			throw new ValueExprEvaluationException("unexpected input value for function: " + args[0]);
		}

	}

}