KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > versant > core > jdo > query > Node


1
2 /*
3  * Copyright (c) 1998 - 2005 Versant Corporation
4  * All rights reserved. This program and the accompanying materials
5  * are made available under the terms of the Eclipse Public License v1.0
6  * which accompanies this distribution, and is available at
7  * http://www.eclipse.org/legal/epl-v10.html
8  *
9  * Contributors:
10  * Versant Corporation - initial API and implementation
11  */

12 package com.versant.core.jdo.query;
13
14 import com.versant.core.metadata.ClassMetaData;
15 import com.versant.core.common.Debug;
16 import com.versant.core.common.CmdBitSet;
17
18 import com.versant.core.common.BindingSupportImpl;
19
20 /**
21  * A node in the parse tree for the query.
22  */

23 public abstract class Node {
24
25     /**
26      * Our parent node (null if this is the root node).
27      */

28     public Node parent;
29     /**
30      * This makes it easy to form linked lists of nodes. This is
31      * faster and uses less memory than arrays of nodes.
32      */

33     public Node next;
34     /**
35      * Linked list of children formed using their next fields.
36      */

37     public Node childList;
38
39     public String JavaDoc asValue;
40
41     public Node() {
42     }
43
44     public String JavaDoc toString() {
45         return toStr();
46     }
47
48     private String JavaDoc toStr() {
49         String JavaDoc n = getClass().getName();
50         int i = n.lastIndexOf('.');
51         if (i >= 0) n = n.substring(i + 1);
52         return n + "@" + Integer.toHexString(System.identityHashCode(this));
53     }
54
55     /**
56      * Dump debugging info to System.out.
57      */

58     public void dump(String JavaDoc indent) {
59         dumpThis(indent);
60         indent = indent + " ";
61         for (Node c = childList; c != null; c = c.next) c.dump(indent);
62     }
63
64     /**
65      * Dump without children.
66      */

67     protected void dumpThis(String JavaDoc indent) {
68         Debug.OUT.println(indent + this + " parent " +
69             (parent == null ? "(null)" : parent.toStr()));
70     }
71
72     /**
73      * Dump as a list.
74      */

75     public void dumpList() {
76         dumpThis("");
77         for (Node c = next; c != null; c = c.next) c.dumpThis(" -> ");
78     }
79
80     /**
81      * Resolve field refs and so on relative to the compiler. This must
82      * recursively resolve any child nodes.
83      */

84     public void resolve(QueryParser comp, ClassMetaData cmd, boolean ordering) {
85         if (Debug.DEBUG) System.out.println("### Node.resolve " + this);
86         for (Node c = childList; c != null; c = c.next) c.resolve(comp, cmd, ordering);
87     }
88
89     /**
90      * Set the parent link on all our children.
91      */

92     public void setParentOnChildren() {
93         for (Node c = childList; c != null; c = c.next) c.parent = this;
94     }
95
96     /**
97      * Replace one node with another.
98      */

99     public void replaceChild(Node old, Node nw) {
100         if (childList == old) {
101             nw.next = childList.next;
102             childList = nw;
103             nw.parent = this;
104             return;
105         }
106         for (Node c = childList; ; ) {
107             Node next = c.next;
108             if (next == old) {
109                 nw.next = next.next;
110                 c.next = nw;
111                 nw.parent = this;
112                 return;
113             }
114             if (next == null) break;
115             c = next;
116         }
117         throw BindingSupportImpl.getInstance().internal("no such Node: " + old);
118     }
119
120     /**
121      * Insert one node (nw) before another (pos).
122      */

123     public void insertChildBefore(Node pos, Node nw) {
124         if (childList == pos) {
125             nw.next = pos;
126             childList = nw;
127             nw.parent = this;
128             return;
129         }
130         for (Node c = childList; ; ) {
131             Node next = c.next;
132             if (next == pos) {
133                 nw.next = pos;
134                 c.next = nw;
135                 nw.parent = this;
136                 return;
137             }
138             if (next == null) break;
139             c = next;
140         }
141         throw BindingSupportImpl.getInstance().internal("no such Node: " + pos);
142     }
143
144     /**
145      * Simplify this node tree as much as possible.
146      */

147     public final void normalize() {
148         normalizeImp();
149         if (Debug.DEBUG) checkIntegrity();
150     }
151
152     /**
153      * Simplify this node tree as much as possible.
154      */

155     protected void normalizeImp() {
156         for (Node c = childList; c != null; c = c.next) {
157             c.normalizeImp();
158         }
159     }
160
161     /**
162      * Abstract method to force all nodes to implement visitor pattern
163      */

164     public abstract Field visit(MemVisitor visitor, Object JavaDoc obj);
165
166     public Object JavaDoc accept(NodeVisitor visitor, Object JavaDoc[] results) {
167         throw BindingSupportImpl.getInstance().internal("Not supported for node " +
168                 this.getClass().getName());
169     }
170
171     /**
172      * Implement this in nodes to udpate the ClassMetaData depency of the graph.
173      * This is used for query eviction.
174      *
175      * @param bitSet
176      */

177     public void updateEvictionDependency(CmdBitSet bitSet) {
178     }
179
180     /**
181      * Check the integrity of this node. This is used during debugging to
182      * check that all the pointers work out. Currently it just makes sure
183      * that all our children list us as their parents.
184      */

185     public void checkIntegrity() {
186         for (Node i = childList; i != null; i = i.next) {
187             if (i.parent != this) {
188                 throw new IllegalStateException JavaDoc(
189                         "Bad child node parent reference:\n" +
190                         "Parent: " + this + "\n" +
191                         "Child: " + i);
192             }
193         }
194         for (Node i = childList; i != null; i = i.next) i.checkIntegrity();
195     }
196
197     /**
198      * Invoke v's arriveXXX method for the node.
199      */

200     public abstract Object JavaDoc arrive(NodeVisitor v, Object JavaDoc msg);
201
202 }
203
Popular Tags