KickJava   Java API By Example, From Geeks To Geeks.

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


1 /**
2  * com.mckoi.database.FunctionDef 07 Sep 2001
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 /**
28  * A definition of a function including its name and parameters. A FunctionDef
29  * can easily be transformed into a Function object via a set of
30  * FunctionFactory instances.
31  * <p>
32  * NOTE: This object is NOT immutable or thread-safe. A FunctionDef should not
33  * be shared among different threads.
34  *
35  * @author Tobias Downer
36  */

37
38 public final class FunctionDef implements java.io.Serializable JavaDoc, Cloneable JavaDoc {
39
40   static final long serialVersionUID = 3339781003247956829L;
41
42   /**
43    * The name of the function.
44    */

45   private String JavaDoc name;
46
47   /**
48    * The list of parameters for the function.
49    */

50   private Expression[] params;
51
52   /**
53    * A cached Function object that was generated when this FunctionDef was
54    * looked up. Note that the Function object is transient.
55    */

56   private transient Function cached_function;
57   
58   
59   /**
60    * Constructs the FunctionDef.
61    */

62   public FunctionDef(String JavaDoc name, Expression[] params) {
63     this.name = name;
64     this.params = params;
65   }
66
67   /**
68    * The name of the function. For example, 'MIN' or 'CONCAT'.
69    */

70   public String JavaDoc getName() {
71     return name;
72   }
73   
74   /**
75    * The list of parameters that are passed to the function. For example,
76    * a concat function may have 7 parameters ('There', ' ', 'are', ' ', 10,
77    * ' ', 'bottles.')
78    */

79   public Expression[] getParameters() {
80     return params;
81   }
82
83   /**
84    * Returns true if this function is an aggregate, or the parameters are
85    * aggregates. It requires a QueryContext object to lookup the function in
86    * the function factory database.
87    */

88   public boolean isAggregate(QueryContext context) {
89     FunctionLookup fun_lookup = context.getFunctionLookup();
90     boolean is_aggregate = fun_lookup.isAggregate(this);
91     if (is_aggregate) {
92       return true;
93     }
94     // Look at params
95
Expression[] params = getParameters();
96     for (int i = 0; i < params.length; ++i) {
97       is_aggregate = params[i].hasAggregateFunction(context);
98       if (is_aggregate) {
99         return true;
100       }
101     }
102     // No
103
return false;
104   }
105
106   /**
107    * Returns a Function object from this FunctionDef. Note that two calls to
108    * this method will produce the same Function object, however the same
109    * Function object will not be produced over multiple instances of
110    * FunctionDef even when they represent the same thing.
111    */

112   public Function getFunction(QueryContext context) {
113     if (cached_function != null) {
114       return cached_function;
115     }
116     else {
117       FunctionLookup lookup = context.getFunctionLookup();
118       cached_function = lookup.generateFunction(this);
119       if (cached_function == null) {
120         throw new StatementException("Function '" + getName() +
121                                      "' doesn't exist.");
122       }
123       return cached_function;
124     }
125   }
126   
127   /**
128    * Performs a deep clone of this object.
129    */

130   public Object JavaDoc clone() throws CloneNotSupportedException JavaDoc {
131     FunctionDef v = (FunctionDef) super.clone();
132     // Deep clone the parameters
133
Expression[] exps = (Expression[]) ((Expression[]) v.params).clone();
134     // Clone each element of the array
135
for (int n = 0; n < exps.length; ++n) {
136       exps[n] = (Expression) exps[n].clone();
137     }
138     v.params = exps;
139     v.cached_function = null;
140     return v;
141   }
142
143   /**
144    * Human understandable string, used for the column title.
145    */

146   public String JavaDoc toString() {
147     StringBuffer JavaDoc buf = new StringBuffer JavaDoc();
148     buf.append(name);
149     buf.append('(');
150     for (int i = 0; i < params.length; ++i) {
151       buf.append(params[i].text().toString());
152       if (i < params.length - 1) {
153         buf.append(',');
154       }
155     }
156     buf.append(')');
157     return new String JavaDoc(buf);
158   }
159
160 }
161
Popular Tags