001 /* 002 // $Id: //open/mondrian/src/main/mondrian/calc/impl/GenericCalc.java#12 $ 003 // This software is subject to the terms of the Common Public License 004 // Agreement, available at the following URL: 005 // http://www.opensource.org/licenses/cpl.html. 006 // Copyright (C) 2006-2008 Julian Hyde 007 // All Rights Reserved. 008 // You must accept the terms of that agreement to use this software. 009 */ 010 package mondrian.calc.impl; 011 012 import mondrian.olap.*; 013 import mondrian.olap.fun.FunUtil; 014 import mondrian.calc.*; 015 016 import java.util.*; 017 018 /** 019 * Adapter which computes an expression and converts it to any required type. 020 * 021 * @author jhyde 022 * @version $Id: //open/mondrian/src/main/mondrian/calc/impl/GenericCalc.java#12 $ 023 * @since Sep 26, 2005 024 */ 025 public abstract class GenericCalc 026 extends AbstractCalc 027 implements ListCalc, MemberListCalc, TupleListCalc, IterCalc, TupleCalc, 028 StringCalc, IntegerCalc, DoubleCalc, BooleanCalc, DateTimeCalc, 029 VoidCalc, MemberCalc, LevelCalc, HierarchyCalc, DimensionCalc 030 { 031 protected GenericCalc(Exp exp) { 032 super(exp); 033 } 034 035 public List evaluateList(Evaluator evaluator) { 036 Object o = evaluate(evaluator); 037 if (o instanceof List) { 038 return (List) o; 039 } else { 040 // Iterable 041 final Iterable iter = (Iterable) o; 042 Iterator it = iter.iterator(); 043 List<Object> list = new ArrayList<Object>(); 044 while (it.hasNext()) { 045 list.add(it.next()); 046 } 047 return list; 048 } 049 } 050 @SuppressWarnings({"unchecked"}) 051 public final List<Member> evaluateMemberList(Evaluator evaluator) { 052 return (List<Member>) evaluateList(evaluator); 053 } 054 055 @SuppressWarnings({"unchecked"}) 056 public final List<Member[]> evaluateTupleList(Evaluator evaluator) { 057 return (List<Member[]>) evaluateList(evaluator); 058 } 059 060 061 062 public Iterable evaluateIterable(Evaluator evaluator) { 063 Object o = evaluate(evaluator); 064 if (o instanceof Iterable) { 065 return (Iterable) o; 066 } else { 067 final List list = (List) o; 068 // for java4 must convert List into an Iterable 069 return new Iterable() { 070 public Iterator iterator() { 071 return list.iterator(); 072 } 073 }; 074 } 075 } 076 077 @SuppressWarnings({"unchecked"}) 078 public Iterable<Member> evaluateMemberIterable(Evaluator evaluator) { 079 return (Iterable<Member>) evaluateIterable(evaluator); 080 } 081 082 @SuppressWarnings({"unchecked"}) 083 public Iterable<Member[]> evaluateTupleIterable(Evaluator evaluator) { 084 return (Iterable<Member[]>) evaluateIterable(evaluator); 085 } 086 087 088 public Member[] evaluateTuple(Evaluator evaluator) { 089 return (Member[]) evaluate(evaluator); 090 } 091 092 public String evaluateString(Evaluator evaluator) { 093 return (String) evaluate(evaluator); 094 } 095 096 public int evaluateInteger(Evaluator evaluator) { 097 Object o = evaluate(evaluator); 098 final Number number = (Number) o; 099 return number == null ? 100 FunUtil.IntegerNull : 101 number.intValue(); 102 } 103 104 public double evaluateDouble(Evaluator evaluator) { 105 final Object o = evaluate(evaluator); 106 final Number number = (Number) o; 107 return numberToDouble(number); 108 } 109 110 public static double numberToDouble(Number number) { 111 return number == null ? 112 FunUtil.DoubleNull : 113 number.doubleValue(); 114 } 115 116 public boolean evaluateBoolean(Evaluator evaluator) { 117 return (Boolean) evaluate(evaluator); 118 } 119 120 public Date evaluateDateTime(Evaluator evaluator) { 121 return (Date) evaluate(evaluator); 122 } 123 124 public void evaluateVoid(Evaluator evaluator) { 125 final Object result = evaluate(evaluator); 126 assert result == null; 127 } 128 129 public Member evaluateMember(Evaluator evaluator) { 130 return (Member) evaluate(evaluator); 131 } 132 133 public Level evaluateLevel(Evaluator evaluator) { 134 return (Level) evaluate(evaluator); 135 } 136 137 public Hierarchy evaluateHierarchy(Evaluator evaluator) { 138 return (Hierarchy) evaluate(evaluator); 139 } 140 141 public Dimension evaluateDimension(Evaluator evaluator) { 142 return (Dimension) evaluate(evaluator); 143 } 144 } 145 146 // End GenericCalc.java