KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > xquark > extractor > algebra > Join


1 /*
2  * This file belongs to the XQuark distribution.
3  * Copyright (C) 2003 Universite de Versailles Saint-Quentin.
4  *
5  * This program is free software; you can redistribute it and/or
6  * modify it under the terms of the GNU Lesser General Public
7  * License as published by the Free Software Foundation; either
8  * version 2.1 of the License, or (at your option) any later version.
9  *
10  * This program is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13  * Lesser General Public License for more details.
14  *
15  * You should have received a copy of the GNU Lesser General Public
16  * License along with this program; if not, write to the Free Software
17  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307.
18  * You can also get it at http://www.gnu.org/licenses/lgpl.html
19  *
20  * For more information on this software, see http://www.xquark.org.
21  */

22
23 package org.xquark.extractor.algebra;
24
25 import java.util.*;
26
27 import org.xquark.extractor.common.SqlWrapperException;
28 import org.xquark.extractor.sql.SqlExpression;
29 import org.apache.commons.collections.set.ListOrderedSet;;
30
31 public final class Join extends Expression implements Relation, PredicateHolder {
32     private static final String JavaDoc RCSRevision = "$Revision: 1.6 $";
33     private static final String JavaDoc RCSName = "$Name: $";
34     
35     protected List _predicateList;
36     protected List _operandList;
37     protected List _keys;
38     
39     private Set _visibleTableInstance = ListOrderedSet.decorate(new HashSet());
40
41
42     public Join(List operandList, List predicateList) {
43         setOperandList(operandList);
44         setPredicateList(predicateList);
45     }
46
47     public Join() {
48         _operandList = new ArrayList();
49     }
50
51     synchronized Object JavaDoc clone(Map clonedExprs) throws CloneNotSupportedException JavaDoc {
52         Join retVal = (Join) super.clone(clonedExprs);
53         retVal.setOperandList(AlgebraTools.clone(getOperandList(), clonedExprs));
54         retVal.setPredicateList(AlgebraTools.clone(getPredicateList(), clonedExprs));
55
56         clonedExprs.put(this, retVal);
57         return retVal;
58     }
59
60     public List getPredicateList() {
61         return _predicateList;
62     }
63
64     public void setPredicateList(List predicateList) {
65         _predicateList = predicateList;
66
67         if (null != _predicateList) {
68             Iterator itr;
69             itr = _predicateList.iterator();
70             while (itr.hasNext()) {
71                 ((Expression) itr.next()).setFather(this);
72             }
73         }
74     }
75
76     public void addPredicate(Expression predicate) {
77         if (null == predicate) {
78             return;
79         }
80
81         if (null == _predicateList) {
82             _predicateList = new ArrayList();
83         }
84
85         _predicateList.add(predicate);
86         predicate.setFather(this);
87     }
88
89     public void addPredicateList(List predicateList) {
90         if (null == predicateList)
91             return;
92
93         if (null == _predicateList) {
94             _predicateList = new ArrayList();
95         }
96
97         _predicateList.addAll(predicateList);
98
99         Expression predicate = null;
100         for (int i = 0; i < predicateList.size(); i++) {
101             predicate = (Expression) predicateList.get(i);
102             predicate.setFather(this);
103         }
104
105     }
106
107     public void removePredicate(Expression predicate) {
108         _predicateList.remove(predicate);
109     }
110
111     /**
112      * Access method for the _operandList property.
113      * @return the current value of the _operandList property
114      */

115     public List getOperandList() {
116         return _operandList;
117     }
118
119     /**
120      * Sets the value of the _operandList property.
121      *
122      * @param aOperandList
123      * the new value of the _operandList property
124      */

125     public void setOperandList(List aOperandList) {
126         _operandList = aOperandList;
127         if (null != _operandList) {
128             Iterator itr;
129             itr = _operandList.iterator();
130             while (itr.hasNext()) {
131                 ((Expression) itr.next()).setFather(this);
132             }
133         }
134         _visibleTableInstance.clear();
135     }
136
137     public void addOperandList(List aOperandList) {
138         if (null != aOperandList) {
139             if (null == _operandList) {
140                 _operandList = new ArrayList();
141             }
142             _operandList.addAll(aOperandList);
143
144             Iterator itr;
145             itr = _operandList.iterator();
146             while (itr.hasNext()) {
147                 ((Expression) itr.next()).setFather(this);
148             }
149         }
150         _visibleTableInstance.clear();
151     }
152
153     public List getOperands() {
154         return _operandList;
155     }
156
157     public void addOperand(Expression relation) {
158         if (null == relation)
159             return;
160         if (null == _operandList)
161             _operandList = new ArrayList();
162         _operandList.add(relation);
163         relation.setFather(this);
164         _visibleTableInstance.clear();
165     }
166
167     public boolean predicateUp(Expression predicate) {
168         return false;
169     }
170
171     public Set providedTableInstances() {
172         return visibleTableInstances();
173     };
174
175     public Set visibleTableInstances() {
176         if (_visibleTableInstance.isEmpty()) {
177             Relation operand = null;
178             for (int i = 0; i < _operandList.size(); i++) {
179                 operand = (Relation) _operandList.get(i);
180                 _visibleTableInstance.addAll(operand.providedTableInstances());
181             }
182         }
183         return _visibleTableInstance;
184     }
185
186     public AttributeExpression findNonNullAttribute() {
187         Iterator it = _operandList.iterator();
188         AttributeExpression ret = null;
189
190         while (it.hasNext() && ret == null) {
191             ret = ((Relation) it.next()).findNonNullAttribute();
192         }
193         return ret;
194     }
195
196     public List getItems() {
197         return null;
198     }
199
200     public void setKeys(List keys) {
201         _keys = keys;
202     }
203
204     public List getKeys() {
205         return _keys;
206     }
207
208     public List nameTest(String JavaDoc name) { return null;}
209
210     public boolean replaceChild(Expression oldChild, Expression newChild) {
211         boolean retVal = false;
212
213         List list = getPredicateList();
214         Expression expr = null;
215
216         if (null != list) {
217             for (int i = 0; i < list.size(); i++) {
218                 expr = (Expression) list.get(i);
219                 if (expr.equals(oldChild)) {
220                     list.set(i, newChild);
221                     newChild.setFather(this);
222                     retVal = true;
223                     break;
224                 }
225             }
226         }
227
228         list = getOperandList();
229
230         expr = null;
231         for (int i = 0; i < list.size(); i++) {
232             expr = (Expression) list.get(i);
233             if (expr.equals(oldChild)) {
234                 list.set(i, newChild);
235                 newChild.setFather(this);
236                 retVal = true;
237                 break;
238             }
239         }
240
241         return retVal;
242     }
243
244     public SqlExpression accept(GenSqlVisitor visitor) throws SqlWrapperException {
245         return visitor.visit(this);
246     }
247
248     public void accept(AlgebraVisitor visitor) throws SqlWrapperException {
249         visitor.visit(this);
250     }
251
252     /**
253      * @see Expression#deepEquals(Object)
254      */

255     public boolean deepEquals(Object JavaDoc o) {
256         if (o instanceof Join) {
257             Join cast = (Join) o;
258             return AlgebraTools.areExprListEquivalent(_operandList, cast.getOperandList()) && AlgebraTools.areExprListEquivalent(_predicateList, cast.getPredicateList());
259         }
260         return false;
261     }
262 }
263
Popular Tags