KickJava   Java API By Example, From Geeks To Geeks.

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


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.List JavaDoc;
26 import java.util.Map JavaDoc;
27
28 import org.xquark.extractor.common.SqlWrapperException;
29 import org.xquark.extractor.sql.SqlExpression;
30
31 public class AttributeExpression extends Expression
32 {
33
34     private static final String JavaDoc RCSRevision = "$Revision: 1.5 $";
35     private static final String JavaDoc RCSName = "$Name: $";
36
37     protected String JavaDoc _attribute;
38     protected RenameRelation _tableInstance;
39     protected Expression _underlyingExpr;
40
41     protected AttributeExpression()
42     {
43     }
44     /**
45     @param tableInstance
46     @param column
47     @param attribute
48     @roseuid 3B2787BD02B1
49      */

50
51     public AttributeExpression(RenameRelation tableInstance, String JavaDoc attribute)
52     {
53         setTableInstance (tableInstance);
54         setAttribute(attribute);
55     }
56
57     synchronized Object JavaDoc clone(Map JavaDoc clonedExprs) throws CloneNotSupportedException JavaDoc
58     {
59         //Trace.enter(this, "clone(Map clonedExprs)");
60
AttributeExpression retVal = (AttributeExpression)super.clone(clonedExprs);
61         Expression clonedExpr = null;
62
63         retVal.setAttribute(getAttribute());
64
65         if (_tableInstance != null) {
66             clonedExpr = (Expression)clonedExprs.get(_tableInstance);
67             if (null == clonedExpr) {
68                 clonedExpr = (Expression)_tableInstance.clone(clonedExprs);
69             }
70             retVal.setTableInstance((RenameRelation)clonedExpr);
71         }
72
73         if (_underlyingExpr != null) {
74             clonedExpr = (Expression)clonedExprs.get(_underlyingExpr);
75             if (null == clonedExpr) {
76                 clonedExpr = (Expression)_underlyingExpr.clone(clonedExprs);
77             }
78             retVal.setUnderlyingExpr(clonedExpr);
79         }
80
81         clonedExprs.put(this, retVal);
82         //Trace.exit(this, "clone(Map clonedExprs)");
83
return retVal;
84     }
85
86     public boolean equals (Object JavaDoc o)
87     {
88         if (o == this)
89             return true;
90         if (!(o instanceof AttributeExpression))
91             return false;
92         AttributeExpression p = (AttributeExpression) o;
93
94         RenameRelation pTi = p.getTableInstance();
95
96         if (null == getTableInstance() && null == pTi) {
97         }
98         else if (null == getTableInstance() || null == pTi) {
99             return false;
100         }
101         else if ( ! getTableInstance().equals(p.getTableInstance()) ) {
102             return false;
103         }
104
105         if ( ! getName().equalsIgnoreCase(p.getName()) )
106             return false;
107         return true;
108     }
109
110     public int hashCode()
111     {
112         if (null != _tableInstance) {
113             String JavaDoc str = _tableInstance.getName()+ "." + _attribute;
114             return str.hashCode();
115         }
116         else {
117             return _attribute.hashCode();
118         }
119     }
120
121     /**
122     Access method for the _attribute property.
123
124     @return the current value of the _attribute property
125      */

126     public String JavaDoc getAttribute()
127     {
128         return _attribute;
129     }
130
131     /**
132     Sets the value of the _attribute property.
133
134     @param aAttribute the new value of the _attribute property
135      */

136     public void setAttribute(String JavaDoc aAttribute)
137     {
138         _attribute = aAttribute;
139     }
140
141     /**
142     Access method for the _tableInstance property.
143
144     @return the current value of the _tableInstance property
145      */

146     public RenameRelation getTableInstance()
147     {
148         return _tableInstance;
149     }
150
151     public List JavaDoc getOperands()
152     {
153         return null;
154     }
155
156     /**
157     Sets the value of the _tableInstance property.
158
159     @param aTableInstance the new value of the _tableInstance property
160      */

161     public void setTableInstance(RenameRelation aTableInstance)
162     {
163         _tableInstance = aTableInstance;
164     }
165
166     public String JavaDoc getName()
167     {
168         return _attribute;
169     }
170     /**
171     Access method for the _underlyinExpr property.
172
173     @return the current value of the _underlyinExpr property
174      */

175     public Expression getUnderlyinExpr()
176     {
177         return _underlyingExpr;
178     }
179
180     /**
181     Sets the value of the _underlyinExpr property.
182
183     @param aUnderlyinExpr the new value of the _underlyinExpr property
184      */

185     public void setUnderlyingExpr(Expression aUnderlyinExpr)
186     {
187         _underlyingExpr = aUnderlyinExpr;
188         while (_underlyingExpr instanceof AttributeExpression
189                     || _underlyingExpr instanceof RenameItem) {
190             if (_underlyingExpr instanceof AttributeExpression)
191                 _underlyingExpr = ((AttributeExpression)_underlyingExpr).getUnderlyinExpr();
192             else
193                 _underlyingExpr = ((RenameItem)_underlyingExpr).getOperand();
194         }
195     }
196
197     public String JavaDoc pprint ()
198     {
199         StringBuffer JavaDoc retVal = new StringBuffer JavaDoc();
200         if (null != _tableInstance) {
201             retVal.append(getTableInstance().getName());
202             retVal.append(".");
203         }
204         retVal.append(_attribute);
205         return retVal.toString();
206     }
207
208     public SqlExpression accept (GenSqlVisitor visitor) throws SqlWrapperException
209     {
210         return visitor.visit(this);
211     }
212
213     public void accept (AlgebraVisitor visitor) throws SqlWrapperException
214     {
215         visitor.visit(this);
216     }
217     
218     /**
219      * The semantics chosen for equals of algebra nodes is "the same algebra operator
220      * hierarchy referring to the same database objects".
221      * @param o the expression to be compared with this.
222      */

223     public boolean deepEquals(Object JavaDoc o)
224     {
225         if (o instanceof AttributeExpression)
226         {
227             AttributeExpression cast = (AttributeExpression)o;
228             return _attribute.equalsIgnoreCase(cast.getAttribute())
229                         && (_tableInstance.getName().equalsIgnoreCase(cast.getTableInstance().getName())
230                             || _tableInstance.deepEquals(cast.getTableInstance())); // if alias name is <> check real relation
231
// NOTE: assuming underlying expression is browsed somewhere else.
232
}
233         return false;
234     }
235 }
236
Popular Tags