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.LinkedHashSet;
010: import java.util.List;
011: import java.util.Set;
012:
013: /**
014: */
015: public class ProjectionElemList extends QueryModelNodeBase {
016:
017: /*-----------*
018: * Variables *
019: *-----------*/
020:
021: private List<ProjectionElem> elements = new ArrayList<ProjectionElem>();
022:
023: /*--------------*
024: * Constructors *
025: *--------------*/
026:
027: public ProjectionElemList() {
028: }
029:
030: public ProjectionElemList(ProjectionElem... elements) {
031: addElements(elements);
032: }
033:
034: public ProjectionElemList(Iterable<ProjectionElem> elements) {
035: addElements(elements);
036: }
037:
038: /*---------*
039: * Methods *
040: *---------*/
041:
042: public List<ProjectionElem> getElements() {
043: return elements;
044: }
045:
046: public void setElements(List<ProjectionElem> elements) {
047: this .elements = elements;
048: }
049:
050: public void addElements(ProjectionElem... elements) {
051: for (ProjectionElem pe : elements) {
052: addElement(pe);
053: }
054: }
055:
056: public void addElements(Iterable<ProjectionElem> elements) {
057: for (ProjectionElem pe : elements) {
058: addElement(pe);
059: }
060: }
061:
062: public void addElement(ProjectionElem pe) {
063: assert pe != null : "pe must not be null";
064: elements.add(pe);
065: pe.setParentNode(this );
066: }
067:
068: public Set<String> getTargetNames() {
069: Set<String> targetNames = new LinkedHashSet<String>(elements
070: .size());
071:
072: for (ProjectionElem pe : elements) {
073: targetNames.add(pe.getTargetName());
074: }
075:
076: return targetNames;
077: }
078:
079: public <X extends Exception> void visit(QueryModelVisitor<X> visitor)
080: throws X {
081: visitor.meet(this );
082: }
083:
084: @Override
085: public <X extends Exception> void visitChildren(
086: QueryModelVisitor<X> visitor) throws X {
087: for (ProjectionElem pe : elements) {
088: pe.visit(visitor);
089: }
090:
091: super .visitChildren(visitor);
092: }
093:
094: @Override
095: public void replaceChildNode(QueryModelNode current,
096: QueryModelNode replacement) {
097: int index = elements.indexOf(current);
098: if (index >= 0) {
099: elements.set(index, (ProjectionElem) replacement);
100: replacement.setParentNode(this );
101: } else {
102: super .replaceChildNode(current, replacement);
103: }
104: }
105:
106: @Override
107: public ProjectionElemList clone() {
108: ProjectionElemList clone = (ProjectionElemList) super .clone();
109:
110: List<ProjectionElem> elementsClone = new ArrayList<ProjectionElem>(
111: getElements().size());
112: for (ProjectionElem pe : getElements()) {
113: elementsClone.add(pe.clone());
114: }
115:
116: clone.setElements(elementsClone);
117:
118: return clone;
119: }
120: }
|