KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > db4o > QConPath


1 /* Copyright (C) 2004 - 2006 db4objects Inc. http://www.db4o.com
2
3 This file is part of the db4o open source object database.
4
5 db4o is free software; you can redistribute it and/or modify it under
6 the terms of version 2 of the GNU General Public License as published
7 by the Free Software Foundation and as clarified by db4objects' GPL
8 interpretation policy, available at
9 http://www.db4o.com/about/company/legalpolicies/gplinterpretation/
10 Alternatively you can write to db4objects, Inc., 1900 S Norfolk Street,
11 Suite 350, San Mateo, CA 94403, USA.
12
13 db4o is distributed in the hope that it will be useful, but WITHOUT ANY
14 WARRANTY; without even the implied warranty of MERCHANTABILITY or
15 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
16 for more details.
17
18 You should have received a copy of the GNU General Public License along
19 with this program; if not, write to the Free Software Foundation, Inc.,
20 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */

21 package com.db4o;
22
23 import com.db4o.foundation.*;
24 import com.db4o.reflect.*;
25
26 /**
27  * Placeholder for a constraint, only necessary to attach children
28  * to the query graph.
29  *
30  * Added upon a call to Query#descend(), if there is no
31  * other place to hook up a new constraint.
32  *
33  * @exclude
34  */

35 public class QConPath extends QConClass {
36     
37     public QConPath(){
38         
39     }
40
41     QConPath(Transaction a_trans, QCon a_parent, QField a_field) {
42         super(a_trans, a_parent, a_field, null);
43         if(a_field != null){
44             i_yapClass = a_field.getYapClass();
45         }
46     }
47     
48     public boolean canLoadByIndex() {
49         return false;
50     }
51     
52     boolean evaluate(QCandidate a_candidate) {
53         if (a_candidate.classReflector() == null) {
54             visitOnNull(a_candidate.getRoot());
55         }
56         return true;
57     }
58     
59     void evaluateSelf() {
60         // do nothing
61
}
62
63     boolean isNullConstraint() {
64         return ! hasChildren();
65     }
66
67     QConClass shareParentForClass(ReflectClass a_class, boolean[] removeExisting) {
68         if (i_parent == null) {
69             return null;
70         }
71         if (! i_field.canHold(a_class)) {
72             return null;
73         }
74         QConClass newConstraint = new QConClass(i_trans, i_parent, i_field, a_class);
75         morph(removeExisting,newConstraint, a_class);
76         return newConstraint;
77     }
78
79
80     QCon shareParent(Object JavaDoc a_object, boolean[] removeExisting) {
81         if (i_parent == null) {
82             return null;
83         }
84         Object JavaDoc obj = i_field.coerce(a_object);
85         if(obj == No4.INSTANCE){
86             QConObject falseConstraint = new QConFalse(i_trans, i_parent, i_field);
87             morph(removeExisting, falseConstraint, reflectClassForObject(obj));
88             return falseConstraint;
89         }
90         QConObject newConstraint = new QConObject(i_trans, i_parent, i_field, obj);
91         morph(removeExisting, newConstraint, reflectClassForObject(obj));
92         return newConstraint;
93     }
94
95     private ReflectClass reflectClassForObject(Object JavaDoc obj) {
96         return i_trans.reflector().forObject(obj);
97     }
98
99     // Our QConPath objects are just placeholders to fields,
100
// so the parents are reachable.
101
// If we find a "real" constraint, we throw the QPath
102
// out and replace it with the other constraint.
103
private void morph(boolean[] removeExisting, QConObject newConstraint, ReflectClass claxx) {
104         boolean mayMorph = true;
105         if (claxx != null) {
106             YapClass yc = i_trans.stream().produceYapClass(claxx);
107             if (yc != null) {
108                 Iterator4 i = iterateChildren();
109                 while (i.moveNext()) {
110                     QField qf = ((QCon) i.current()).getField();
111                     if (!yc.hasField(i_trans.stream(), qf.i_name)) {
112                         mayMorph = false;
113                         break;
114                     }
115                 }
116             }
117         }
118         
119         // }
120

121         if (mayMorph) {
122             Iterator4 j = iterateChildren();
123             while (j.moveNext()) {
124                 newConstraint.addConstraint((QCon) j.current());
125             }
126             if(hasJoins()){
127                 Iterator4 k = iterateJoins();
128                 while (k.moveNext()) {
129                     QConJoin qcj = (QConJoin)k.current();
130                     qcj.exchangeConstraint(this, newConstraint);
131                     newConstraint.addJoin(qcj);
132                 }
133             }
134             i_parent.exchangeConstraint(this, newConstraint);
135             removeExisting[0] = true;
136             
137         } else {
138             i_parent.addConstraint(newConstraint);
139         }
140     }
141
142     final boolean visitSelfOnNull() {
143         return false;
144     }
145     
146     public String JavaDoc toString(){
147         if(! Debug4.prettyToStrings){
148             return super.toString();
149         }
150         return "QConPath " + super.toString();
151     }
152
153
154 }
155
Popular Tags