KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > mondrian > olap > fun > CacheFunDef


1 /*
2 // $Id: //open/mondrian/src/main/mondrian/olap/fun/CacheFunDef.java#7 $
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) 2005-2007 Julian Hyde and others
7 // All Rights Reserved.
8 // You must accept the terms of that agreement to use this software.
9 */

10 package mondrian.olap.fun;
11
12 import mondrian.olap.*;
13 import mondrian.olap.type.Type;
14 import mondrian.calc.*;
15 import mondrian.calc.impl.GenericCalc;
16 import mondrian.mdx.ResolvedFunCall;
17
18 import java.io.PrintWriter JavaDoc;
19
20 /**
21  * Definition of the <code>$Cache</code> system function, which is smart enough
22  * to evaluate its argument only once.
23  *
24  * @author jhyde
25  * @since 2005/8/14
26  * @version $Id: //open/mondrian/src/main/mondrian/olap/fun/CacheFunDef.java#7 $
27  */

28 public class CacheFunDef extends FunDefBase {
29     private final Type type;
30     private static final String JavaDoc NAME = "$Cache";
31     private static final String JavaDoc SIGNATURE = "$Cache(<<Exp>>)";
32     private static final String JavaDoc DESCRIPTION = "Evaluates and returns its sole argument, applying statement-level caching";
33     private static final Syntax SYNTAX = Syntax.Internal;
34     private ExpCacheDescriptor cacheDescriptor;
35     static final CacheFunResolver Resolver = new CacheFunResolver();
36
37     CacheFunDef(
38             String JavaDoc name,
39             String JavaDoc signature,
40             String JavaDoc description,
41             Syntax syntax,
42             int category,
43             Type type) {
44         super(name, signature, description, syntax,
45                 category, new int[] {category});
46         this.type = type;
47     }
48
49     public void unparse(Exp[] args, PrintWriter JavaDoc pw) {
50         args[0].unparse(pw);
51     }
52
53     public Calc compileCall(ResolvedFunCall call, ExpCompiler compiler) {
54         final Exp exp = call.getArg(0);
55         final ExpCacheDescriptor cacheDescriptor =
56                 new ExpCacheDescriptor(exp, compiler);
57         return new GenericCalc(call) {
58             public Object JavaDoc evaluate(Evaluator evaluator) {
59                 return evaluator.getCachedResult(cacheDescriptor);
60             }
61
62             public Calc[] getCalcs() {
63                 return new Calc[] {cacheDescriptor.getCalc()};
64             }
65         };
66     }
67
68     public static class CacheFunResolver extends ResolverBase {
69         CacheFunResolver() {
70             super(NAME, SIGNATURE, DESCRIPTION, SYNTAX);
71         }
72
73         public FunDef resolve(
74                 Exp[] args, Validator validator, int[] conversionCount) {
75             if (args.length != 1) {
76                 return null;
77             }
78             final Exp exp = args[0];
79             final int category = exp.getCategory();
80             final Type type = exp.getType();
81             return new CacheFunDef(NAME, SIGNATURE, DESCRIPTION, SYNTAX,
82                     category, type);
83         }
84
85         public boolean requiresExpression(int k) {
86             return false;
87         }
88     }
89 }
90
91 // End CacheFunDef.java
92
Popular Tags