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: * An order operator that can be used to order bindings as specified by a set of
013: * value expressions.
014: *
015: * @author Arjohn Kampman
016: */
017: public class Order extends UnaryTupleOperator {
018:
019: /*-----------*
020: * Variables *
021: *-----------*/
022:
023: private List<OrderElem> elements = new ArrayList<OrderElem>();
024:
025: /*--------------*
026: * Constructors *
027: *--------------*/
028:
029: public Order() {
030: }
031:
032: public Order(TupleExpr arg) {
033: super (arg);
034: }
035:
036: public Order(TupleExpr arg, OrderElem... elements) {
037: this (arg);
038: addElements(elements);
039: }
040:
041: public Order(TupleExpr arg, Iterable<OrderElem> elements) {
042: this (arg);
043: addElements(elements);
044: }
045:
046: /*---------*
047: * Methods *
048: *---------*/
049:
050: public List<OrderElem> getElements() {
051: return elements;
052: }
053:
054: public void setElements(List<OrderElem> elements) {
055: this .elements = elements;
056: }
057:
058: public void addElements(OrderElem... elements) {
059: for (OrderElem pe : elements) {
060: addElement(pe);
061: }
062: }
063:
064: public void addElements(Iterable<OrderElem> elements) {
065: for (OrderElem pe : elements) {
066: addElement(pe);
067: }
068: }
069:
070: public void addElement(OrderElem pe) {
071: elements.add(pe);
072: pe.setParentNode(this );
073: }
074:
075: public <X extends Exception> void visit(QueryModelVisitor<X> visitor)
076: throws X {
077: visitor.meet(this );
078: }
079:
080: @Override
081: public <X extends Exception> void visitChildren(
082: QueryModelVisitor<X> visitor) throws X {
083: for (OrderElem elem : elements) {
084: elem.visit(visitor);
085: }
086:
087: super .visitChildren(visitor);
088: }
089:
090: @Override
091: public void replaceChildNode(QueryModelNode current,
092: QueryModelNode replacement) {
093: int index = elements.indexOf(current);
094: if (index >= 0) {
095: elements.set(index, (OrderElem) replacement);
096: replacement.setParentNode(this );
097: } else {
098: super .replaceChildNode(current, replacement);
099: }
100: }
101:
102: @Override
103: public Order clone() {
104: Order clone = (Order) super .clone();
105:
106: List<OrderElem> elementsClone = new ArrayList<OrderElem>(
107: getElements().size());
108: for (OrderElem elem : getElements()) {
109: elementsClone.add(elem.clone());
110: }
111:
112: clone.setElements(elementsClone);
113:
114: return clone;
115: }
116: }
|