KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > xquark > extractor > xfunctions > XFunction


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.xfunctions;
24
25 import java.util.ArrayList JavaDoc;
26 import java.util.List JavaDoc;
27
28 import org.xquark.extractor.algebra.Expression;
29 import org.xquark.extractor.common.Debug;
30
31 public abstract class XFunction extends Expression {
32     private String JavaDoc _functionName;
33     protected List JavaDoc _arguments;
34
35     /**
36      * @roseuid 3BD1465C02E6
37      */

38     public XFunction() {
39     }
40     public XFunction(String JavaDoc functionName) {
41         setFunctionName(functionName);
42     }
43     /**
44      * @param name
45      * @param arguments
46      * @roseuid 3BD143B8028A
47      */

48     public XFunction(String JavaDoc functionName, List JavaDoc arguments) {
49         setFunctionName(functionName);
50         setArguments(arguments);
51     }
52
53     /**
54      * Access method for the _name property.
55      *
56      * @return the current value of the _name property
57      */

58     public String JavaDoc getFunctionName() {
59         return _functionName;
60     }
61
62     /**
63      * Sets the value of the _functionName property.
64      *
65      * @param aName the new value of the _functionName property
66      */

67     public void setFunctionName(String JavaDoc functionName) {
68         _functionName = functionName;
69     }
70
71     /**
72      * Access method for the _arguments property.
73      *
74      * @return the current value of the _arguments property
75      */

76     public List JavaDoc getArguments() {
77         return _arguments;
78     }
79
80     public List JavaDoc getOperands()
81     {
82         return _arguments;
83     }
84     /**
85      * Sets the value of the _arguments property.
86      *
87      * @param aArguments the new value of the _arguments property
88      */

89     public void setArguments(List JavaDoc aArguments) {
90         _arguments = aArguments;
91         if (null != _arguments) {
92             for (int i = 0; i < _arguments.size(); i++) {
93                 ((Expression)_arguments.get(i)).setFather(this);
94             }
95         }
96     }
97
98     public int getArgumentNumber() {
99         if (null != _arguments) {
100             return _arguments.size();
101         }
102         else {
103             return 0;
104         }
105     }
106
107     public Expression getArgument(int index ) {
108         if (null != _arguments) {
109             if (_arguments.size()> index) {
110                 return (Expression)_arguments.get(index);
111             }
112             else {
113                 return null;
114             }
115         }
116         else {
117             return null;
118         }
119     }
120
121     public void setArgument(int index, Expression argument) {
122         Debug.assertTrue( null!=argument, "null!=argument");
123         if (null == _arguments ) {
124             _arguments = new ArrayList JavaDoc();
125         }
126         while( index >_arguments.size()-1) {
127             _arguments.add(null);
128         }
129         _arguments.set(index,argument);
130         argument.setFather(this);
131     }
132
133     public boolean replaceChild(Expression oldChild, Expression newChild)
134     {
135         boolean retVal = false;
136
137         if (null != _arguments) {
138             Object JavaDoc child = null;
139             for (int i = 0; i < _arguments.size(); i++) {
140                 child = _arguments.get(i);
141                 if ( oldChild.equals(child)) {
142                     _arguments.set(i,newChild);
143                     retVal = true;
144                     break;
145                 }
146             }
147         }
148         else {
149             /* return false */
150         }
151
152         return retVal;
153     }
154
155     public String JavaDoc pprint()
156     {
157         StringBuffer JavaDoc retVal = new StringBuffer JavaDoc();
158         retVal.append(getFunctionName());
159         retVal.append("(");
160         if ( null != _arguments) {
161             Expression argument = null;
162             for (int i = 0; i < _arguments.size(); i++) {
163                 argument = (Expression)_arguments.get(i);
164                 retVal.append(argument.pprint());
165                 retVal.append(",");
166             }
167             retVal.delete(retVal.length(),retVal.length());
168         }
169         retVal.append(")");
170         return retVal.toString();
171     }
172
173     /**
174      * @see Expression#deepEquals(Object)
175      */

176     public boolean deepEquals(Object JavaDoc o)
177     {
178         if (o instanceof XFunction)
179         {
180             XFunction cast = (XFunction)o;
181             int len = _arguments.size(), i;
182             boolean ret = _functionName.equalsIgnoreCase(cast.getFunctionName())
183                     && (len == cast.getArguments().size());
184             
185             for (i = 0; ret && i < len; i++)
186                 ret &= ((Expression)_arguments.get(i)).deepEquals(cast.getArguments().get(i));
187             return ret;
188         }
189         return false;
190     }
191 }
192
193
Popular Tags