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 SQL operator with one argument.
015: *
016: * @author James Leigh
017: *
018: */
019: public abstract class UnarySqlOperator extends RdbmsQueryModelNodeBase
020: implements SqlExpr {
021: private SqlExpr arg;
022:
023: public UnarySqlOperator() {
024: super ();
025: }
026:
027: public UnarySqlOperator(SqlExpr arg) {
028: super ();
029: setArg(arg);
030: }
031:
032: public SqlExpr getArg() {
033: return arg;
034: }
035:
036: public void setArg(SqlExpr arg) {
037: this .arg = arg;
038: arg.setParentNode(this );
039: }
040:
041: @Override
042: public <X extends Exception> void visitChildren(
043: QueryModelVisitor<X> visitor) throws X {
044: arg.visit(visitor);
045: }
046:
047: @Override
048: public void replaceChildNode(QueryModelNode current,
049: QueryModelNode replacement) {
050: if (arg == current) {
051: setArg((SqlExpr) replacement);
052: } else {
053: super .replaceChildNode(current, replacement);
054: }
055: }
056:
057: @Override
058: public UnarySqlOperator clone() {
059: UnarySqlOperator clone = (UnarySqlOperator) super .clone();
060: clone.setArg(arg.clone());
061: return clone;
062: }
063:
064: @Override
065: public int hashCode() {
066: final int prime = 31;
067: int result = 1;
068: result = prime * result + ((arg == null) ? 0 : arg.hashCode());
069: return result;
070: }
071:
072: @Override
073: public boolean equals(Object obj) {
074: if (this == obj)
075: return true;
076: if (obj == null)
077: return false;
078: if (getClass() != obj.getClass())
079: return false;
080: final UnarySqlOperator other = (UnarySqlOperator) obj;
081: if (arg == null) {
082: if (other.arg != null)
083: return false;
084: } else if (!arg.equals(other.arg))
085: return false;
086: return true;
087: }
088:
089: @Override
090: public String toString() {
091: QueryModelTreePrinter treePrinter = new QueryModelTreePrinter();
092: UnarySqlOperator clone = this .clone();
093: UnarySqlOperator parent = new UnarySqlOperator(clone) {
094: @Override
095: public <X extends Exception> void visit(
096: RdbmsQueryModelVisitorBase<X> visitor) throws X {
097: visitor.meetOther(this );
098: }
099: };
100: new SqlConstantOptimizer().optimize(clone);
101: parent.getArg().visit(treePrinter);
102: return treePrinter.getTreeString();
103: }
104:
105: }
|