KickJava   Java API By Example, From Geeks To Geeks.

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


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
16 import com.versant.core.common.BindingSupportImpl;
17
18 /**
19  * This is a node with exactly two children. It is optimized to take
20  * advantage of this.
21  */

22 public class BinaryNode extends Node {
23
24     public BinaryNode() {
25     }
26
27     public BinaryNode(Node left, Node right) {
28         childList = left;
29         if (left != null) {
30             left.next = right;
31             left.parent = this;
32         }
33         if (right != null) {
34             right.parent = this;
35         }
36     }
37
38     public final Node getLeft() {
39         return childList;
40     }
41
42     public final Node getRight() {
43         return childList.next;
44     }
45
46     /**
47      * Resolve field refs and so on relative to the compiler. This must
48      * recursively resolve any child nodes.
49      */

50     public void resolve(QueryParser comp, ClassMetaData cmd, boolean ordering) {
51         childList.resolve(comp, cmd, false);
52         childList.next.resolve(comp, cmd, false);
53     }
54
55     /**
56      * Replace one node with another.
57      */

58     public void replaceChild(Node old, Node nw) {
59         if (childList == old) {
60             nw.next = childList.next;
61             childList = nw;
62         } else if (childList.next == old) {
63             childList.next = nw;
64             nw.next = null;
65         } else {
66             throw BindingSupportImpl.getInstance().internal("no such Node: " + old);
67         }
68         nw.parent = this;
69     }
70
71     /**
72      * Simplify this node tree as much as possible.
73      */

74     public void normalizeImp() {
75         childList.normalizeImp();
76         childList.next.normalizeImp();
77
78         // swap left and right nodes if a literal or param is on the left
79
if (childList instanceof LiteralNode
80                 || childList instanceof ParamNode
81                 || childList instanceof ParamNodeProxy) {
82             swapLeftAndRight();
83         }
84     }
85
86     /**
87      * Swap left and right nodes.
88      */

89     protected void swapLeftAndRight() {
90         Node t = childList;
91         childList = childList.next;
92         childList.next = t;
93         t.next = null;
94     }
95
96     public Field visit(MemVisitor visitor, Object JavaDoc obj) {
97         return visitor.visitBinaryNode(this, obj);
98     }
99
100     public Object JavaDoc arrive(NodeVisitor v, Object JavaDoc msg) {
101         return v.arriveBinaryNode(this, msg);
102     }
103 }
104
Popular Tags