KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > mckoi > database > AbstractFunction


1 /**
2  * com.mckoi.database.AbstractFunction 12 Jul 2000
3  *
4  * Mckoi SQL Database ( http://www.mckoi.com/database )
5  * Copyright (C) 2000, 2001, 2002 Diehl and Associates, Inc.
6  *
7  * This program is free software; you can redistribute it and/or
8  * modify it under the terms of the GNU General Public License
9  * Version 2 as published by the Free Software Foundation.
10  *
11  * This program is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14  * GNU General Public License Version 2 for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * Version 2 along with this program; if not, write to the Free Software
18  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
19  *
20  * Change Log:
21  *
22  *
23  */

24
25 package com.mckoi.database;
26
27 import java.math.BigDecimal JavaDoc;
28 import java.util.ArrayList JavaDoc;
29 import java.util.List JavaDoc;
30
31 /**
32  * An abstract implementation of Function.
33  *
34  * @author Tobias Downer
35  */

36
37 public abstract class AbstractFunction implements Function {
38
39   /**
40    * The name of the function.
41    */

42   private String JavaDoc name;
43
44   /**
45    * The list of expressions this function has as parameters.
46    */

47   private Expression[] params;
48
49   /**
50    * Set to true if this is an aggregate function (requires a group). It is
51    * false by default.
52    */

53   private boolean is_aggregate;
54
55   /**
56    * Constructs the Function with the given expression array as parameters.
57    */

58   public AbstractFunction(String JavaDoc name, Expression[] params) {
59     this.name = name;
60     this.params = params;
61
62     is_aggregate = false;
63   }
64
65   /**
66    * Call this from the constructor if the function is an aggregate.
67    */

68   protected void setAggregate(boolean status) {
69     is_aggregate = status;
70   }
71
72   /**
73    * Returns the number of parameters for this function.
74    */

75   public int parameterCount() {
76     return params.length;
77   }
78
79   /**
80    * Returns the parameter at the given index in the parameters list.
81    */

82   public Expression getParameter(int n) {
83     return params[n];
84   }
85
86   /**
87    * Returns true if the param is the special case glob parameter (*).
88    */

89   public boolean isGlob() {
90     if (params == FunctionFactory.GLOB_LIST) {
91       return true;
92     }
93     if (params.length == 1) {
94       Expression exp = params[0];
95       return (exp.size() == 1 &&
96               new String JavaDoc(exp.text()).equals("*"));
97     }
98     return false;
99   }
100
101
102
103
104   // ---------- Implemented from Function ----------
105

106   /**
107    * Returns the name of the function. The name is a unique identifier that
108    * can be used to recreate this function. This identifier can be used to
109    * easily serialize the function when grouped with its parameters.
110    */

111   public String JavaDoc getName() {
112     return name;
113   }
114
115   /**
116    * Returns the list of all Variable's that are used by this function. This
117    * looks up each expression in the list of parameters. This will cascade
118    * if the expressions have a Function, etc.
119    */

120   public List JavaDoc allVariables() {
121     ArrayList JavaDoc result_list = new ArrayList JavaDoc();
122     for (int i = 0; i < params.length; ++i) {
123       List JavaDoc l = params[i].allVariables();
124       result_list.addAll(l);
125     }
126     return result_list;
127   }
128
129   /**
130    * Returns the list of all elements that are used by this function. This
131    * looks up each expression in the list of parameters. This will cascade
132    * if the expressions have a Function, etc.
133    */

134   public List JavaDoc allElements() {
135     ArrayList JavaDoc result_list = new ArrayList JavaDoc();
136     for (int i = 0; i < params.length; ++i) {
137       List JavaDoc l = params[i].allElements();
138       result_list.addAll(l);
139     }
140     return result_list;
141   }
142
143   /**
144    * Returns whether the function is an aggregate function or not.
145    */

146   public final boolean isAggregate(QueryContext context) {
147     if (is_aggregate) {
148       return true;
149     }
150     else {
151       // Check if arguments are aggregates
152
for (int i = 0; i < params.length; ++i) {
153         Expression exp = params[i];
154         if (exp.hasAggregateFunction(context)) {
155           return true;
156         }
157       }
158     }
159     return false;
160   }
161
162   /**
163    * Prepares the parameters of the function.
164    */

165   public void prepareParameters(ExpressionPreparer preparer)
166                                                    throws DatabaseException {
167     for (int i = 0; i < params.length; ++i) {
168       params[i].prepare(preparer);
169     }
170   }
171
172   /**
173    * The init function. By default, we don't do anything however this should
174    * be overwritten if we need to check the parameter arguments.
175    */

176   public void init(VariableResolver resolver) {
177   }
178
179
180   /**
181    * By Default, we assume a function returns a Numeric object.
182    */

183   public TType returnTType(VariableResolver resolver, QueryContext context) {
184     return returnTType();
185   }
186
187   public TType returnTType() {
188     return TType.NUMERIC_TYPE;
189   }
190
191   // ---------- Convenience methods ----------
192

193
194   public String JavaDoc toString() {
195     StringBuffer JavaDoc buf = new StringBuffer JavaDoc();
196     buf.append(name);
197     buf.append('(');
198     for (int i = 0; i < params.length; ++i) {
199       buf.append(params[i].text().toString());
200       if (i < params.length - 1) {
201         buf.append(',');
202       }
203     }
204     buf.append(')');
205     return new String JavaDoc(buf);
206   }
207
208
209 }
210
Popular Tags