01: /*
02: * Copyright Aduna (http://www.aduna-software.com/) (c) 1997-2006.
03: *
04: * Licensed under the Aduna BSD-style license.
05: */
06: package org.openrdf.query.algebra;
07:
08: import org.openrdf.query.algebra.helpers.QueryModelTreePrinter;
09:
10: /**
11: * Base implementation of {@link QueryModelNode}.
12: */
13: public abstract class QueryModelNodeBase implements QueryModelNode {
14:
15: /*-----------*
16: * Variables *
17: *-----------*/
18:
19: private QueryModelNode parent;
20:
21: /*---------*
22: * Methods *
23: *---------*/
24:
25: public QueryModelNode getParentNode() {
26: return parent;
27: }
28:
29: public void setParentNode(QueryModelNode parent) {
30: this .parent = parent;
31: }
32:
33: /**
34: * Dummy implementation of {@link QueryModelNode#visitChildren} that does
35: * nothing. Subclasses should override this method when they have child
36: * nodes.
37: */
38: public <X extends Exception> void visitChildren(
39: QueryModelVisitor<X> visitor) throws X {
40: }
41:
42: /**
43: * Default implementation of
44: * {@link QueryModelNode#replaceChildNode(QueryModelNode, QueryModelNode)}
45: * that throws an {@link IllegalArgumentException} indicating that
46: * <tt>current</tt> is not a child node of this node.
47: */
48: public void replaceChildNode(QueryModelNode current,
49: QueryModelNode replacement) {
50: throw new IllegalArgumentException("Node is not a child node: "
51: + current);
52: }
53:
54: /**
55: * Default implementation of
56: * {@link QueryModelNode#replaceChildNode(QueryModelNode, QueryModelNode)}
57: * that throws an {@link IllegalArgumentException} indicating that
58: * <tt>current</tt> is not a child node of this node.
59: */
60: public void replaceWith(QueryModelNode replacement) {
61: if (parent == null) {
62: throw new IllegalStateException("Node has no parent");
63: }
64:
65: parent.replaceChildNode(this , replacement);
66: }
67:
68: /**
69: * Default implementation of {@link QueryModelNode#getSignature()} that
70: * prints the name of the node's class.
71: */
72: public String getSignature() {
73: return this .getClass().getSimpleName();
74: }
75:
76: @Override
77: public String toString() {
78: QueryModelTreePrinter treePrinter = new QueryModelTreePrinter();
79: this .visit(treePrinter);
80: return treePrinter.getTreeString();
81: }
82:
83: @Override
84: public QueryModelNodeBase clone() {
85: try {
86: return (QueryModelNodeBase) super .clone();
87: } catch (CloneNotSupportedException e) {
88: throw new RuntimeException(
89: "Query model nodes are required to be cloneable", e);
90: }
91: }
92: }
|