KickJava   Java API By Example, From Geeks To Geeks.

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


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
30 public final class UnOpRestrict extends UnaryAlgebra implements PredicateHolder
31 {
32     private static final String JavaDoc RCSRevision = "$Revision: 1.6 $";
33     private static final String JavaDoc RCSName = "$Name: $";
34
35
36     protected List _predicateList;
37     
38     /**
39      * True if the restriction is a having clause. This distinction is made
40      * necessary by the fact that having clauses may exist without group or where
41      * clauses, to allow SQL generation to make the difference
42      */

43     protected boolean _postResult = false;
44
45     public UnOpRestrict(Expression operand) { super(operand);}
46
47     public UnOpRestrict(Expression operand, Expression predicate)
48     {
49         super ( operand ) ;
50         addPredicate ( predicate ) ;
51     }
52
53     public UnOpRestrict(Expression operand, List predicateList)
54     {
55         super ( operand ) ;
56         setPredicateList ( predicateList ) ;
57     }
58
59     public UnOpRestrict(Expression operand, List predicateList, boolean postResult)
60     {
61         this(operand, predicateList) ;
62         setPostResult(postResult) ;
63     }
64
65     synchronized Object JavaDoc clone(Map clonedExprs) throws CloneNotSupportedException JavaDoc
66     {
67         UnOpRestrict retVal = (UnOpRestrict)super.clone(clonedExprs);
68         retVal.setPredicateList(AlgebraTools.clone(_predicateList, clonedExprs));
69
70         clonedExprs.put(this, retVal);
71         return retVal;
72     }
73
74     public List getPredicateList()
75     {
76         return _predicateList;
77     }
78
79     public List getParameterList()
80     {
81         return getPredicateList();
82     }
83
84     public void setPredicateList(List predicateList)
85     {
86         _predicateList = predicateList;
87
88         if ( null != _predicateList )
89         {
90             Iterator itr ;
91             itr = _predicateList.iterator() ;
92             while ( itr.hasNext () ) {
93                 ( (Expression)itr.next() ).setFather(this) ;
94             }
95         }
96     }
97
98
99     public void addPredicate(Expression predicate)
100     {
101         if ( null == predicate )
102         {
103            return ;
104         }
105
106         if (null == _predicateList) {
107             _predicateList = new ArrayList ();
108         }
109
110         _predicateList.add(predicate);
111         predicate.setFather(this);
112     }
113
114
115     public void removePredicate(Expression predicate)
116     {
117         _predicateList.remove(predicate);
118     }
119
120
121     public List getKeys() {
122         return ((Relation)getOperand()).getKeys();
123     }
124
125     private void splitPredicate(Expression predicate, List predicateList)
126     {
127         Expression p1 , p2 ;
128         BinOpBoolean binOpBoolean ;
129
130         p1 = predicate ;
131
132         if ( p1 instanceof BinOpBoolean )
133         {
134             binOpBoolean = (BinOpBoolean) p1 ;
135             if ( AND == binOpBoolean.getOperator() )
136             {
137                 splitPredicate (binOpBoolean.getLeftOperand() , predicateList) ;
138                 splitPredicate (binOpBoolean.getRightOperand() , predicateList) ;
139             }
140             else
141             {
142                 predicateList .add( predicate ) ;
143             }
144         }
145         else
146         {
147             predicateList .add( predicate ) ;
148         }
149     }
150
151     public boolean replaceChild(Expression oldChild, Expression newChild)
152     {
153         boolean retVal = false;
154         List list = getPredicateList();
155
156         Expression expr = null ;
157         for (int i = 0; i < list.size(); i++) {
158             expr = (Expression)list.get(i);
159             if (expr.equals(oldChild)) {
160                 list.set(i, newChild);
161                 newChild.setFather(this);
162                 retVal = true;
163                 break;
164             }
165         }
166
167         if (getOperand().equals(oldChild)) {
168             setOperand(newChild);
169             retVal = true;
170         }
171
172         return retVal;
173     }
174
175
176     public SqlExpression accept (GenSqlVisitor visitor) throws SqlWrapperException
177     {
178         return visitor.visit(this);
179     }
180
181     public void accept (AlgebraVisitor visitor) throws SqlWrapperException
182     {
183         visitor.visit(this);
184     }
185
186     /**
187      * @see Expression#deepEquals(Object)
188      */

189     public boolean deepEquals(Object JavaDoc o)
190     {
191         if (o instanceof UnOpRestrict)
192         {
193             UnOpRestrict cast = (UnOpRestrict)o;
194             return super.deepEquals(o)
195                     && AlgebraTools.areExprListEquivalent(_predicateList, cast.getPredicateList());
196         }
197         return false;
198     }
199
200     public boolean isPostResult() {
201         return _postResult;
202     }
203
204     public void setPostResult(boolean b) {
205         _postResult = b;
206     }
207
208 }
209
Popular Tags