KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > mondrian > calc > impl > ConstantCalc


1 /*
2 // $Id: //open/mondrian/src/main/mondrian/calc/impl/ConstantCalc.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) 2006-2006 Julian Hyde
7 // All Rights Reserved.
8 // You must accept the terms of that agreement to use this software.
9 */

10 package mondrian.calc.impl;
11
12 import mondrian.olap.*;
13 import mondrian.olap.type.*;
14 import mondrian.olap.type.DimensionType;
15 import mondrian.olap.type.LevelType;
16 import mondrian.calc.*;
17
18 /**
19  * Calculator which always returns the same value.
20  *
21  * @author jhyde
22  * @version $Id: //open/mondrian/src/main/mondrian/calc/impl/ConstantCalc.java#6 $
23  * @since Sep 27, 2005
24  */

25 public class ConstantCalc extends GenericCalc {
26     private final Object JavaDoc o;
27     private final int i;
28     private final double d;
29
30     public ConstantCalc(Type type, Object JavaDoc o) {
31         super(new DummyExp(type));
32         this.o = o;
33         this.i = (o instanceof Number JavaDoc) ? ((Number JavaDoc) o).intValue() : 0;
34         this.d = (o instanceof Number JavaDoc) ? ((Number JavaDoc) o).doubleValue() : 0;
35     }
36
37     public void accept(CalcWriter calcWriter) {
38         calcWriter.getWriter().print(o);
39     }
40
41     public Object JavaDoc evaluate(Evaluator evaluator) {
42         return o;
43     }
44
45     public int evaluateInteger(Evaluator evaluator) {
46         return i;
47     }
48
49     public double evaluateDouble(Evaluator evaluator) {
50         return d;
51     }
52
53     public boolean dependsOn(Dimension dimension) {
54         // A constant -- including a catalog element -- will evaluate to the
55
// same result regardless of the evaluation context. For example, the
56
// member [Gender].[M] does not 'depend on' the [Gender] dimension.
57
return false;
58     }
59
60     public Calc[] getCalcs() {
61         return new Calc[0];
62     }
63
64     /**
65      * Creates an expression which evaluates to an integer.
66      */

67     public static ConstantCalc constantInteger(int i) {
68         return new ConstantCalc(new NumericType(), i);
69     }
70
71     /**
72      * Creates an expression which evaluates to a string.
73      */

74     public static StringCalc constantString(String JavaDoc s) {
75         return new ConstantCalc(new StringType(), s);
76     }
77
78     /**
79      * Creates an expression which evaluates to null.
80      */

81     public static ConstantCalc constantNull(Type type) {
82         return new ConstantCalc(type, null);
83     }
84
85     /**
86      * Creates an expression which evaluates to a member.
87      */

88     public static Calc constantMember(Member member) {
89         return new ConstantCalc(
90                 MemberType.forMember(member),
91                 member);
92     }
93
94     /**
95      * Creates an expression which evaluates to a level.
96      */

97     public static Calc constantLevel(Level level) {
98         return new ConstantCalc(
99                 LevelType.forLevel(level),
100                 level);
101     }
102
103     /**
104      * Creates an expression which evaluates to a hierarchy.
105      */

106     public static Calc constantHierarchy(Hierarchy hierarchy) {
107         return new ConstantCalc(
108                 HierarchyType.forHierarchy(hierarchy),
109                 hierarchy);
110     }
111
112     /**
113      * Creates an expression which evaluates to a dimension.
114      */

115     public static Calc constantDimension(Dimension dimension) {
116         return new ConstantCalc(
117                 DimensionType.forDimension(dimension),
118                 dimension);
119     }
120 }
121
122 // End ConstantCalc.java
123
Popular Tags