001: /*
002: * Copyright Aduna (http://www.aduna-software.com/) (c) 1997-2006.
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.LinkedHashSet;
010: import java.util.List;
011: import java.util.Set;
012:
013: /**
014: * An extension operator that can be used to add bindings to solutions whose
015: * values are defined by {@link ValueExpr value expressions}.
016: */
017: public class Extension extends UnaryTupleOperator {
018:
019: /*-----------*
020: * Variables *
021: *-----------*/
022:
023: private List<ExtensionElem> elements = new ArrayList<ExtensionElem>();
024:
025: /*--------------*
026: * Constructors *
027: *--------------*/
028:
029: public Extension() {
030: }
031:
032: public Extension(TupleExpr arg) {
033: super (arg);
034: }
035:
036: public Extension(TupleExpr arg, ExtensionElem... elements) {
037: this (arg);
038: addElements(elements);
039: }
040:
041: public Extension(TupleExpr arg, Iterable<ExtensionElem> elements) {
042: this (arg);
043: addElements(elements);
044: }
045:
046: /*---------*
047: * Methods *
048: *---------*/
049:
050: public List<ExtensionElem> getElements() {
051: return elements;
052: }
053:
054: public void setElements(Iterable<ExtensionElem> elements) {
055: this .elements.clear();
056: addElements(elements);
057: }
058:
059: public void addElements(ExtensionElem... elements) {
060: for (ExtensionElem pe : elements) {
061: addElement(pe);
062: }
063: }
064:
065: public void addElements(Iterable<ExtensionElem> elements) {
066: for (ExtensionElem pe : elements) {
067: addElement(pe);
068: }
069: }
070:
071: public void addElement(ExtensionElem pe) {
072: elements.add(pe);
073: pe.setParentNode(this );
074: }
075:
076: @Override
077: public Set<String> getBindingNames() {
078: Set<String> bindingNames = new LinkedHashSet<String>(arg
079: .getBindingNames());
080:
081: for (ExtensionElem pe : elements) {
082: bindingNames.add(pe.getName());
083: }
084:
085: return bindingNames;
086: }
087:
088: public <X extends Exception> void visit(QueryModelVisitor<X> visitor)
089: throws X {
090: visitor.meet(this );
091: }
092:
093: @Override
094: public <X extends Exception> void visitChildren(
095: QueryModelVisitor<X> visitor) throws X {
096: for (ExtensionElem elem : elements) {
097: elem.visit(visitor);
098: }
099:
100: super .visitChildren(visitor);
101: }
102:
103: @Override
104: public void replaceChildNode(QueryModelNode current,
105: QueryModelNode replacement) {
106: int index = elements.indexOf(current);
107: if (index >= 0) {
108: elements.set(index, (ExtensionElem) replacement);
109: replacement.setParentNode(this );
110: } else {
111: super .replaceChildNode(current, replacement);
112: }
113: }
114:
115: @Override
116: public Extension clone() {
117: Extension clone = (Extension) super .clone();
118:
119: List<ExtensionElem> elementsClone = new ArrayList<ExtensionElem>(
120: getElements().size());
121: for (ExtensionElem elem : getElements()) {
122: elementsClone.add(elem.clone());
123: }
124:
125: clone.setElements(elementsClone);
126:
127: return clone;
128: }
129: }
|