KickJava   Java API By Example, From Geeks To Geeks.

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


1 /*
2 // $Id: //open/mondrian/src/main/mondrian/calc/impl/GenericCalc.java#8 $
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-2007 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.fun.FunUtil;
14 import mondrian.calc.*;
15 import mondrian.util.UnsupportedList;
16
17 import java.util.Iterator JavaDoc;
18 import java.util.ArrayList JavaDoc;
19 import java.util.List JavaDoc;
20
21 /**
22  * Adapter which computes an expression and converts it to any required type.
23  *
24  * @author jhyde
25  * @version $Id: //open/mondrian/src/main/mondrian/calc/impl/GenericCalc.java#8 $
26  * @since Sep 26, 2005
27  */

28 public abstract class GenericCalc
29         extends AbstractCalc
30         implements ListCalc, IterCalc,
31             StringCalc, IntegerCalc, DoubleCalc, BooleanCalc,
32             VoidCalc, MemberCalc, LevelCalc, HierarchyCalc, DimensionCalc
33 {
34
35     protected GenericCalc(Exp exp) {
36         super(exp);
37     }
38
39     public List evaluateList(Evaluator evaluator) {
40         Object JavaDoc o = evaluate(evaluator);
41         if (o instanceof List) {
42             return (List) o;
43         } else {
44             // Iterable
45
final Iterable JavaDoc iter = (Iterable JavaDoc) o;
46             Iterator JavaDoc it = iter.iterator();
47             List list = new ArrayList JavaDoc<Object JavaDoc>();
48             while (it.hasNext()) {
49                 list.add(it.next());
50             }
51             return list;
52         }
53         //return (List) evaluate(evaluator);
54
}
55
56     public Iterable JavaDoc evaluateIterable(Evaluator evaluator) {
57         Object JavaDoc o = evaluate(evaluator);
58         if (o instanceof Iterable JavaDoc) {
59             return (Iterable JavaDoc) o;
60         } else {
61             final List list = (List) o;
62             // for java4 must convert List into an Iterable
63
return new Iterable JavaDoc() {
64                 public Iterator JavaDoc iterator() {
65                     return list.iterator();
66                 }
67             };
68         }
69     }
70
71     public String JavaDoc evaluateString(Evaluator evaluator) {
72         return (String JavaDoc) evaluate(evaluator);
73     }
74
75     public int evaluateInteger(Evaluator evaluator) {
76         Object JavaDoc o = evaluate(evaluator);
77         final Number JavaDoc number = (Number JavaDoc) o;
78         return number == null ?
79                 FunUtil.IntegerNull :
80                 number.intValue();
81     }
82
83     public double evaluateDouble(Evaluator evaluator) {
84         final Object JavaDoc o = evaluate(evaluator);
85         final Number JavaDoc number = (Number JavaDoc) o;
86         return number == null ?
87                 FunUtil.DoubleNull :
88                 number.doubleValue();
89     }
90
91     public boolean evaluateBoolean(Evaluator evaluator) {
92         return (Boolean JavaDoc) evaluate(evaluator);
93     }
94
95     public void evaluateVoid(Evaluator evaluator) {
96         final Object JavaDoc result = evaluate(evaluator);
97         assert result == null;
98     }
99
100     public Member evaluateMember(Evaluator evaluator) {
101         return (Member) evaluate(evaluator);
102     }
103
104     public Level evaluateLevel(Evaluator evaluator) {
105         return (Level) evaluate(evaluator);
106     }
107
108     public Hierarchy evaluateHierarchy(Evaluator evaluator) {
109         return (Hierarchy) evaluate(evaluator);
110     }
111
112     public Dimension evaluateDimension(Evaluator evaluator) {
113         return (Dimension) evaluate(evaluator);
114     }
115 }
116
117 // End GenericCalc.java
118
Popular Tags