001: /*
002: * Copyright Aduna (http://www.aduna-software.com/) (c) 2008.
003: *
004: * Licensed under the Aduna BSD-style license.
005: */
006: package org.openrdf.sail.rdbms.algebra.base;
007:
008: import org.openrdf.query.algebra.QueryModelNode;
009: import org.openrdf.query.algebra.QueryModelVisitor;
010: import org.openrdf.query.algebra.helpers.QueryModelTreePrinter;
011: import org.openrdf.sail.rdbms.optimizers.SqlConstantOptimizer;
012:
013: /**
014: * An abstract binary sql operator with two arguments.
015: *
016: * @author James Leigh
017: *
018: */
019: public abstract class BinarySqlOperator extends RdbmsQueryModelNodeBase
020: implements SqlExpr {
021: private SqlExpr leftArg;
022: private SqlExpr rightArg;
023:
024: public BinarySqlOperator() {
025: super ();
026: }
027:
028: public BinarySqlOperator(SqlExpr leftArg, SqlExpr rightArg) {
029: super ();
030: setLeftArg(leftArg);
031: setRightArg(rightArg);
032: }
033:
034: public SqlExpr getLeftArg() {
035: return leftArg;
036: }
037:
038: public void setLeftArg(SqlExpr leftArg) {
039: this .leftArg = leftArg;
040: leftArg.setParentNode(this );
041: }
042:
043: public SqlExpr getRightArg() {
044: return rightArg;
045: }
046:
047: public void setRightArg(SqlExpr rightArg) {
048: this .rightArg = rightArg;
049: rightArg.setParentNode(this );
050: }
051:
052: @Override
053: public <X extends Exception> void visitChildren(
054: QueryModelVisitor<X> visitor) throws X {
055: leftArg.visit(visitor);
056: rightArg.visit(visitor);
057: }
058:
059: @Override
060: public void replaceChildNode(QueryModelNode current,
061: QueryModelNode replacement) {
062: if (leftArg == current) {
063: setLeftArg((SqlExpr) replacement);
064: } else if (rightArg == current) {
065: setRightArg((SqlExpr) replacement);
066: } else {
067: super .replaceChildNode(current, replacement);
068: }
069: }
070:
071: @Override
072: public BinarySqlOperator clone() {
073: BinarySqlOperator clone = (BinarySqlOperator) super .clone();
074: clone.setLeftArg(leftArg.clone());
075: clone.setRightArg(rightArg.clone());
076: return clone;
077: }
078:
079: @Override
080: public int hashCode() {
081: final int prime = 31;
082: int result = 1;
083: result = prime * result
084: + ((leftArg == null) ? 0 : leftArg.hashCode());
085: result = prime * result
086: + ((rightArg == null) ? 0 : rightArg.hashCode());
087: return result;
088: }
089:
090: @Override
091: public boolean equals(Object obj) {
092: if (this == obj)
093: return true;
094: if (obj == null)
095: return false;
096: if (getClass() != obj.getClass())
097: return false;
098: final BinarySqlOperator other = (BinarySqlOperator) obj;
099: if (leftArg == null) {
100: if (other.leftArg != null)
101: return false;
102: } else if (!leftArg.equals(other.leftArg))
103: return false;
104: if (rightArg == null) {
105: if (other.rightArg != null)
106: return false;
107: } else if (!rightArg.equals(other.rightArg))
108: return false;
109: return true;
110: }
111:
112: @Override
113: public String toString() {
114: QueryModelTreePrinter treePrinter = new QueryModelTreePrinter();
115: BinarySqlOperator clone = this .clone();
116: UnarySqlOperator parent = new UnarySqlOperator(clone) {
117: @Override
118: public <X extends Exception> void visit(
119: RdbmsQueryModelVisitorBase<X> visitor) throws X {
120: visitor.meetOther(this );
121: }
122: };
123: new SqlConstantOptimizer().optimize(clone);
124: parent.getArg().visit(treePrinter);
125: return treePrinter.getTreeString();
126: }
127: }
|