KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > db4o > QConJoin


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
25 /**
26  *
27  * Join constraint on queries
28  *
29  * @exclude
30  */

31 public class QConJoin extends QCon {
32     
33     // FIELDS MUST BE PUBLIC TO BE REFLECTED ON UNDER JDK <= 1.1
34

35     public boolean i_and;
36     public QCon i_constraint1;
37     public QCon i_constraint2;
38     
39     
40     public QConJoin(){
41         // C/S
42
}
43
44     QConJoin(Transaction a_trans, QCon a_c1, QCon a_c2, boolean a_and) {
45         super(a_trans);
46         i_constraint1 = a_c1;
47         i_constraint2 = a_c2;
48         i_and = a_and;
49     }
50
51     void doNotInclude(QCandidate a_root) {
52         i_constraint1.doNotInclude(a_root);
53         i_constraint2.doNotInclude(a_root);
54     }
55
56     void exchangeConstraint(QCon a_exchange, QCon a_with) {
57         super.exchangeConstraint(a_exchange, a_with);
58         if (a_exchange == i_constraint1) {
59             i_constraint1 = a_with;
60         }
61         if (a_exchange == i_constraint2) {
62             i_constraint2 = a_with;
63         }
64     }
65
66     void evaluatePending(
67         QCandidate a_root,
68         QPending a_pending,
69         int a_secondResult) {
70
71         boolean res =
72             i_evaluator.not(
73                 i_and
74                     ? ((a_pending._result + a_secondResult) > 0)
75                     : (a_pending._result + a_secondResult) > -4);
76                     
77         if (hasJoins()) {
78             Iterator4 i = iterateJoins();
79             while (i.moveNext()) {
80                 QConJoin qcj = (QConJoin) i.current();
81                 if (Debug.queries) {
82                     System.out.println(
83                         "QConJoin creates pending this:"
84                             + i_id
85                             + " Join:"
86                             + qcj.i_id
87                             + " res:"
88                             + res);
89                 }
90                 a_root.evaluate(new QPending(qcj, this, res));
91             }
92         } else {
93             if (!res) {
94                 if (Debug.queries) {
95                     System.out.println(
96                         "QConJoin evaluatePending FALSE "
97                             + i_id
98                             + " Calling: "
99                             + i_constraint1.i_id
100                             + ", "
101                             + i_constraint2.i_id);
102                 }
103                 i_constraint1.doNotInclude(a_root);
104                 i_constraint2.doNotInclude(a_root);
105             }else{
106                 if (Debug.queries) {
107                     System.out.println(
108                         "QConJoin evaluatePending TRUE "
109                             + i_id
110                             + " NOT calling: "
111                             + i_constraint1.i_id
112                             + ", "
113                             + i_constraint2.i_id);
114                 }
115             }
116
117         }
118     }
119
120     public QCon getOtherConstraint(QCon a_constraint) {
121         if (a_constraint == i_constraint1) {
122             return i_constraint2;
123         } else if (a_constraint == i_constraint2) {
124             return i_constraint1;
125         }
126         throw new IllegalArgumentException JavaDoc();
127     }
128     
129     String JavaDoc logObject(){
130         if (Debug.queries) {
131             String JavaDoc msg = i_and ? "&" : "|";
132             return " " + i_constraint1.i_id + msg + i_constraint2.i_id;
133         }
134         return "";
135     }
136     
137     
138     boolean removeForParent(QCon a_constraint) {
139         if (i_and) {
140             QCon other = getOtherConstraint(a_constraint);
141             other.removeJoin(this); // prevents circular call
142
other.remove();
143             return true;
144         }
145         return false;
146     }
147     
148     public String JavaDoc toString(){
149         if(! Debug4.prettyToStrings){
150             return super.toString();
151         }
152         String JavaDoc str = "QConJoin " + (i_and ? "AND ": "OR");
153         if(i_constraint1 != null){
154             str += "\n " + i_constraint1;
155         }
156         if(i_constraint2 != null){
157             str += "\n " + i_constraint2;
158         }
159         return str;
160     }
161
162     public boolean isOr() {
163         return !i_and;
164     }
165     
166
167 }
168
Popular Tags