KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > mondrian > olap > ParameterImpl


1 /*
2 // $Id: //open/mondrian/src/main/mondrian/olap/ParameterImpl.java#5 $
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) 2000-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 // leonardk, 10 January, 2000
12 */

13
14 package mondrian.olap;
15 import mondrian.olap.type.Type;
16 import mondrian.olap.type.NumericType;
17 import mondrian.olap.type.StringType;
18 import mondrian.olap.type.MemberType;
19 import mondrian.mdx.MemberExpr;
20 import mondrian.calc.*;
21 import mondrian.calc.impl.GenericCalc;
22
23 /**
24  * Implementation of {@link Parameter}.
25  *
26  * @author jhyde
27  * @version $Id: //open/mondrian/src/main/mondrian/olap/ParameterImpl.java#5 $
28  * @since Jul 22, 2006
29  */

30 public class ParameterImpl
31     implements Parameter, ParameterCompilable {
32
33     private final String JavaDoc name;
34     private String JavaDoc description;
35     private Exp defaultExp;
36     private Type type;
37     private ParameterSlot slot = new ParameterSlot() {
38       Object JavaDoc value;
39       public Object JavaDoc getCachedDefaultValue() {
40         throw new UnsupportedOperationException JavaDoc();
41       }
42
43       public Calc getDefaultValueCalc() {
44         throw new UnsupportedOperationException JavaDoc();
45       }
46
47       public int getIndex() {
48         throw new UnsupportedOperationException JavaDoc();
49       }
50
51       public Parameter getParameter() {
52         return ParameterImpl.this;
53       }
54
55       public Object JavaDoc getParameterValue() {
56         return value;
57       }
58
59       public void setCachedDefaultValue(Object JavaDoc value) {
60         throw new UnsupportedOperationException JavaDoc();
61       }
62
63       public void setParameterValue(Object JavaDoc value) {
64         this.value = value;
65       }
66
67     };
68
69     public ParameterImpl(
70         String JavaDoc name,
71         Exp defaultExp,
72         String JavaDoc description,
73         Type type)
74     {
75         this.name = name;
76         this.defaultExp = defaultExp;
77         this.description = description;
78         this.type = type;
79         assert defaultExp != null;
80         assert type instanceof StringType ||
81             type instanceof NumericType ||
82             type instanceof MemberType;
83     }
84
85     public Scope getScope() {
86         return Scope.Statement;
87     }
88
89     public Type getType() {
90         return type;
91     }
92
93     public Exp getDefaultExp() {
94         return defaultExp;
95     }
96
97     public String JavaDoc getName() {
98         return name;
99     }
100
101     public Object JavaDoc getValue() {
102         if (slot == null) {
103             // query has not been resolved yet, so it's not possible for the
104
// parameter to have a value
105
return null;
106         } else {
107             return slot.getParameterValue();
108         }
109     }
110
111     public void setValue(Object JavaDoc value) {
112         if (value instanceof MemberExpr) {
113             slot.setParameterValue(((MemberExpr) value).getMember());
114         } else if (value instanceof Literal) {
115             slot.setParameterValue(((Literal) value).getValue());
116         } else {
117             slot.setParameterValue(value);
118         }
119     }
120
121     public String JavaDoc getDescription() {
122         return description;
123     }
124
125     // For the purposes of type inference and expression substitution, a
126
// parameter is atomic; therefore, we ignore the child member, if any.
127
public Object JavaDoc[] getChildren() {
128         return null;
129     }
130
131     /**
132      * Returns whether this parameter is equal to another, based upon name,
133      * type and value
134      */

135     public boolean equals(Object JavaDoc other) {
136         if (!(other instanceof ParameterImpl)) {
137             return false;
138         }
139         ParameterImpl that = (ParameterImpl) other;
140         return that.getName().equals(this.getName()) &&
141             that.defaultExp.equals(this.defaultExp);
142     }
143
144     /**
145      * Returns whether the parameter can be modified.
146      */

147     public boolean isModifiable() {
148         return true;
149     }
150
151     public void setDescription(String JavaDoc description) {
152         this.description = description;
153     }
154
155     public void setType(Type type) {
156         assert type instanceof StringType ||
157             type instanceof NumericType ||
158             type instanceof MemberType;
159         this.type = type;
160     }
161
162     public void setDefaultExp(Exp defaultExp) {
163         assert defaultExp != null;
164         this.defaultExp = defaultExp;
165     }
166
167     public Calc compile(ExpCompiler compiler) {
168         final ParameterSlot slot = compiler.registerParameter(this);
169         if (this.slot != null) {
170             // save previous value
171
slot.setParameterValue(this.slot.getParameterValue());
172         }
173         this.slot = slot;
174         return new ParameterCalc(slot);
175     }
176
177     /**
178      * Compiled expression which yields the value of a parameter.
179      * It uses a slot which has a unique id within the execution environment.
180      */

181     private static class ParameterCalc
182         extends GenericCalc {
183         private final ParameterSlot slot;
184
185         public ParameterCalc(ParameterSlot slot) {
186             super(new DummyExp(slot.getParameter().getType()));
187             this.slot = slot;
188         }
189
190         public Calc[] getCalcs() {
191             return new Calc[0];
192         }
193
194         public Object JavaDoc evaluate(Evaluator evaluator) {
195             Object JavaDoc value = evaluator.getParameterValue(slot);
196             if (slot.getParameterValue() == null) {
197                 // save value if not set (setting the default value)
198
slot.setParameterValue(value);
199             }
200             return value;
201         }
202     }
203 }
204
205 // End ParameterImpl.java
206

207
Popular Tags