All Downloads are FREE. Search and download functionalities are using the official Maven repository.

at.newmedialab.ldpath.model.functions.math.RoundFunction Maven / Gradle / Ivy

package at.newmedialab.ldpath.model.functions.math;

import java.net.URI;
import java.util.ArrayList;
import java.util.Collection;

import at.newmedialab.ldpath.api.backend.RDFBackend;
import at.newmedialab.ldpath.model.Constants;
import at.newmedialab.ldpath.model.transformers.DoubleTransformer;

public class RoundFunction implements MathFunction {

	protected final DoubleTransformer doubleTransformer = new DoubleTransformer();
	protected final URI intType = URI.create(Constants.NS_XSD + "integer");

	public Collection apply(RDFBackend backend, Node context,
			Collection... args) throws IllegalArgumentException {
		if (args.length != 1) {
			throw new IllegalArgumentException("round takes only one argument");
		}

		ArrayList result = new ArrayList();
		for (Node node : args[0]) {
			Node res = calc(backend, node);
			if (res != null) {
				result.add(res);
			}
		}
		return result;
	}

	protected Node calc(RDFBackend backend, Node node) {
		/* SUM */
		try {
			Double val = doubleTransformer.transform(backend, node);
			return backend.createLiteral(String.valueOf(Math.round(val)), null,
					intType);
		} catch (IllegalArgumentException e) {
			return null;
		}

	}

	public String getDescription() {
		return "Round each argument to the closest int/long value";
	}

	public String getSignature() {
		return "fn:round(LiteralList l) :: IntegerLiteralList";
	}

	public String getPathExpression(RDFBackend backend) {
		return "round";
	}

}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy