KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > mondrian > mdx > ResolvedFunCall


1 /*
2 // $Id: //open/mondrian/src/main/mondrian/mdx/ResolvedFunCall.java#6 $
3 // This software is subject to the terms of the Common Public License
4 // Agreement, available at the following URL:
5 // http://www.opensource.org/licenses/cpl.html.
6 // Copyright (C) 1998-2002 Kana Software, Inc.
7 // Copyright (C) 2001-2006 Julian Hyde and others
8 // All Rights Reserved.
9 // You must accept the terms of that agreement to use this software.
10 */

11
12 package mondrian.mdx;
13 import mondrian.calc.Calc;
14 import mondrian.calc.ExpCompiler;
15 import mondrian.olap.fun.*;
16 import mondrian.olap.type.Type;
17 import mondrian.olap.*;
18
19 import java.io.PrintWriter JavaDoc;
20
21 /**
22  * A <code>ResolvedFunCall</code> is a function applied to a list of operands,
23  * which has been validated and resolved to a
24  * {@link FunDef function definition}.
25  *
26  * @author jhyde
27  * @version $Id: //open/mondrian/src/main/mondrian/mdx/ResolvedFunCall.java#6 $
28  * @since Jan 6, 2006
29  */

30 public class ResolvedFunCall extends ExpBase implements FunCall {
31
32     /**
33      * The arguments to the function call. Note that for methods, 0-th arg is
34      * 'this'.
35      */

36     private final Exp[] args;
37
38     /**
39      * Return type of this function call.
40      */

41     private final Type returnType;
42
43     /**
44      * Function definition.
45      */

46     private final FunDef funDef;
47
48     /**
49      * Creates a function call.
50      *
51      * @param funDef Function definition
52      * @param args Arguments
53      * @param returnType Return type
54      */

55     public ResolvedFunCall(FunDef funDef, Exp[] args, Type returnType) {
56         assert funDef != null;
57         assert args != null;
58         assert returnType != null;
59         this.funDef = funDef;
60         this.args = args;
61         this.returnType = returnType;
62     }
63
64     public String JavaDoc toString() {
65         return Util.unparse(this);
66     }
67
68     public ResolvedFunCall clone() {
69         return new ResolvedFunCall(funDef, ExpBase.cloneArray(args), returnType);
70     }
71
72     /**
73      * Returns the Exp argument at the specified index.
74      *
75      * @param index the index of the Exp.
76      * @return the Exp at the specified index of this array of Exp.
77      * The first Exp is at index <code>0</code>.
78      * @see #getArgs()
79      */

80     public Exp getArg(int index) {
81         return args[index];
82     }
83
84     /**
85      * Returns the internal array of Exp arguments.
86      *
87      * <p>Note: this does NOT do a copy.
88      *
89      * @return the array of expressions
90      */

91     public Exp[] getArgs() {
92         return args;
93     }
94
95     /**
96      * Returns the number of arguments.
97      *
98      * @return number of arguments.
99      * @see #getArgs()
100      */

101     public final int getArgCount() {
102         return args.length;
103     }
104
105     public String JavaDoc getFunName() {
106         return funDef.getName();
107     }
108
109     public Syntax getSyntax() {
110         return funDef.getSyntax();
111     }
112
113     public Object JavaDoc[] getChildren() {
114         return args;
115     }
116
117     /**
118      * Returns the definition of the function which is being called.
119      *
120      * @return function definition
121      */

122     public FunDef getFunDef() {
123         return funDef;
124     }
125
126     public final int getCategory() {
127         return funDef.getReturnCategory();
128     }
129
130     public final Type getType() {
131         return returnType;
132     }
133
134     public Exp accept(Validator validator) {
135         // even though the function has already been validated, we need
136
// to walk through the arguments to determine which measures are
137
// referenced
138
Exp[] newArgs = new Exp[args.length];
139         FunUtil.resolveFunArgs(
140             validator, args, newArgs, getFunName(), getSyntax());
141
142         return this;
143     }
144
145     public void unparse(PrintWriter JavaDoc pw) {
146         funDef.unparse(args, pw);
147     }
148
149     public Calc accept(ExpCompiler compiler) {
150         return funDef.compileCall(this, compiler);
151     }
152
153     public Object JavaDoc accept(MdxVisitor visitor) {
154         final Object JavaDoc o = visitor.visit(this);
155         // visit the call's arguments
156
for (Exp arg : args) {
157             arg.accept(visitor);
158         }
159         return o;
160     }
161 }
162
163 // End ResolvedFunCall.java
164
Popular Tags