/* * Copyright Aduna (http://www.aduna-software.com/) (c) 2007. * * Licensed under the Aduna BSD-style license. */ package org.openrdf.query.algebra; import java.util.ArrayList; import java.util.List; /** * A call to an (external) function that operates on zero or more arguments. * * @author Arjohn Kampman */ public class FunctionCall extends QueryModelNodeBase implements ValueExpr { /*-----------* * Variables * *-----------*/ protected String uri; /** * The operator's argument. */ protected List<ValueExpr> args = new ArrayList<ValueExpr>(); /*--------------* * Constructors * *--------------*/ public FunctionCall() { } /** * Creates a new unary value operator. * * @param arg * The operator's argument, must not be <tt>null</tt>. */ public FunctionCall(String uri, ValueExpr... args) { setURI(uri); addArgs(args); } public FunctionCall(String uri, Iterable<ValueExpr> args) { setURI(uri); addArgs(args); } /*---------* * Methods * *---------*/ public String getURI() { return uri; } public void setURI(String uri) { this.uri = uri; } public List<ValueExpr> getArgs() { return args; } public void setArgs(Iterable<ValueExpr> args) { this.args.clear(); addArgs(args); } public void addArgs(ValueExpr... args) { for (ValueExpr arg : args) { addArg(arg); } } public void addArgs(Iterable<ValueExpr> args) { for (ValueExpr arg : args) { addArg(arg); } } public void addArg(ValueExpr arg) { assert arg != null : "arg must not be null"; args.add(arg); arg.setParentNode(this); } public <X extends Exception> void visit(QueryModelVisitor<X> visitor) throws X { visitor.meet(this); } @Override public <X extends Exception> void visitChildren(QueryModelVisitor<X> visitor) throws X { for (ValueExpr arg : args) { arg.visit(visitor); } super.visitChildren(visitor); } @Override public void replaceChildNode(QueryModelNode current, QueryModelNode replacement) { int index = args.indexOf(current); if (index >= 0) { args.set(index, (ValueExpr)replacement); replacement.setParentNode(this); } else { super.replaceChildNode(current, replacement); } } @Override public FunctionCall clone() { FunctionCall clone = (FunctionCall)super.clone(); List<ValueExpr> argsClone = new ArrayList<ValueExpr>(getArgs().size()); for (ValueExpr arg : getArgs()) { argsClone.add(arg.clone()); } clone.setArgs(argsClone); return clone; } }