001: /*
002: * Copyright Aduna (http://www.aduna-software.com/) (c) 2007.
003: *
004: * Licensed under the Aduna BSD-style license.
005: */
006: package org.openrdf.query.algebra;
007:
008: import java.util.ArrayList;
009: import java.util.List;
010:
011: /**
012: * A call to an (external) function that operates on zero or more arguments.
013: *
014: * @author Arjohn Kampman
015: */
016: public class FunctionCall extends QueryModelNodeBase implements
017: ValueExpr {
018:
019: /*-----------*
020: * Variables *
021: *-----------*/
022:
023: protected String uri;
024:
025: /**
026: * The operator's argument.
027: */
028: protected List<ValueExpr> args = new ArrayList<ValueExpr>();
029:
030: /*--------------*
031: * Constructors *
032: *--------------*/
033:
034: public FunctionCall() {
035: }
036:
037: /**
038: * Creates a new unary value operator.
039: *
040: * @param arg
041: * The operator's argument, must not be <tt>null</tt>.
042: */
043: public FunctionCall(String uri, ValueExpr... args) {
044: setURI(uri);
045: addArgs(args);
046: }
047:
048: public FunctionCall(String uri, Iterable<ValueExpr> args) {
049: setURI(uri);
050: addArgs(args);
051: }
052:
053: /*---------*
054: * Methods *
055: *---------*/
056:
057: public String getURI() {
058: return uri;
059: }
060:
061: public void setURI(String uri) {
062: this .uri = uri;
063: }
064:
065: public List<ValueExpr> getArgs() {
066: return args;
067: }
068:
069: public void setArgs(Iterable<ValueExpr> args) {
070: this .args.clear();
071: addArgs(args);
072: }
073:
074: public void addArgs(ValueExpr... args) {
075: for (ValueExpr arg : args) {
076: addArg(arg);
077: }
078: }
079:
080: public void addArgs(Iterable<ValueExpr> args) {
081: for (ValueExpr arg : args) {
082: addArg(arg);
083: }
084: }
085:
086: public void addArg(ValueExpr arg) {
087: assert arg != null : "arg must not be null";
088: args.add(arg);
089: arg.setParentNode(this );
090: }
091:
092: public <X extends Exception> void visit(QueryModelVisitor<X> visitor)
093: throws X {
094: visitor.meet(this );
095: }
096:
097: @Override
098: public <X extends Exception> void visitChildren(
099: QueryModelVisitor<X> visitor) throws X {
100: for (ValueExpr arg : args) {
101: arg.visit(visitor);
102: }
103:
104: super .visitChildren(visitor);
105: }
106:
107: @Override
108: public void replaceChildNode(QueryModelNode current,
109: QueryModelNode replacement) {
110: int index = args.indexOf(current);
111: if (index >= 0) {
112: args.set(index, (ValueExpr) replacement);
113: replacement.setParentNode(this );
114: } else {
115: super .replaceChildNode(current, replacement);
116: }
117: }
118:
119: @Override
120: public FunctionCall clone() {
121: FunctionCall clone = (FunctionCall) super .clone();
122:
123: List<ValueExpr> argsClone = new ArrayList<ValueExpr>(getArgs()
124: .size());
125: for (ValueExpr arg : getArgs()) {
126: argsClone.add(arg.clone());
127: }
128:
129: clone.setArgs(argsClone);
130:
131: return clone;
132: }
133: }
|