001 /* 002 // $Id: //open/mondrian/src/main/mondrian/calc/impl/ConstantCalc.java#9 $ 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-2007 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.olap.type.*; 015 import mondrian.olap.type.DimensionType; 016 import mondrian.olap.type.LevelType; 017 import mondrian.calc.*; 018 019 /** 020 * Calculator which always returns the same value. 021 * 022 * @author jhyde 023 * @version $Id: //open/mondrian/src/main/mondrian/calc/impl/ConstantCalc.java#9 $ 024 * @since Sep 27, 2005 025 */ 026 public class ConstantCalc extends GenericCalc { 027 private final Object o; 028 private final int i; 029 private final double d; 030 031 public ConstantCalc(Type type, Object o) { 032 super(new DummyExp(type)); 033 this.o = o; 034 this.i = initializeInteger(o); 035 this.d = initializeDouble(o); 036 } 037 038 public ResultStyle getResultStyle() { 039 return o == null 040 ? ResultStyle.VALUE 041 : ResultStyle.VALUE_NOT_NULL; 042 } 043 044 private double initializeDouble(Object o) { 045 double value; 046 if (o instanceof Number) { 047 value = ((Number) o).doubleValue(); 048 } else { 049 if (o == null) { 050 value = FunUtil.DoubleNull; 051 } else { 052 value = 0; 053 } 054 } 055 return value; 056 } 057 058 private int initializeInteger(Object o) { 059 int value; 060 if (o instanceof Number) { 061 value = ((Number) o).intValue(); 062 } else { 063 if (o == null) { 064 value = FunUtil.IntegerNull; 065 } else { 066 value = 0; 067 } 068 } 069 return value; 070 } 071 072 public void accept(CalcWriter calcWriter) { 073 calcWriter.getWriter().print(o); 074 } 075 076 public Object evaluate(Evaluator evaluator) { 077 return o; 078 } 079 080 public int evaluateInteger(Evaluator evaluator) { 081 return i; 082 } 083 084 public double evaluateDouble(Evaluator evaluator) { 085 return d; 086 } 087 088 public boolean dependsOn(Dimension dimension) { 089 // A constant -- including a catalog element -- will evaluate to the 090 // same result regardless of the evaluation context. For example, the 091 // member [Gender].[M] does not 'depend on' the [Gender] dimension. 092 return false; 093 } 094 095 public Calc[] getCalcs() { 096 return new Calc[0]; 097 } 098 099 /** 100 * Creates an expression which evaluates to a given integer. 101 * 102 * @param i Integer value 103 * @return Constant integer expression 104 */ 105 public static ConstantCalc constantInteger(int i) { 106 return new ConstantCalc(new DecimalType(Integer.MAX_VALUE, 0), i); 107 } 108 109 /** 110 * Creates an expression which evaluates to a given double. 111 * 112 * @param v Double value 113 * @return Constant double expression 114 */ 115 public static DoubleCalc constantDouble(double v) { 116 return new ConstantCalc(new NumericType(), v); 117 } 118 119 /** 120 * Creates an expression which evaluates to a given string. 121 * 122 * @param s String value 123 * @return Constant string expression 124 */ 125 public static StringCalc constantString(String s) { 126 return new ConstantCalc(new StringType(), s); 127 } 128 129 /** 130 * Creates an expression which evaluates to a given boolean. 131 * 132 * @param b Boolean value 133 * @return Constant boolean expression 134 */ 135 public static BooleanCalc constantBoolean(boolean b) { 136 return new ConstantCalc(new BooleanType(), b); 137 } 138 139 /** 140 * Creates an expression which evaluates to null. 141 * 142 * @param type Type 143 * @return Constant null expression 144 */ 145 public static ConstantCalc constantNull(Type type) { 146 return new ConstantCalc(type, null); 147 } 148 149 /** 150 * Creates an expression which evaluates to a given member. 151 * 152 * @param member Member 153 * @return Constant member expression 154 */ 155 public static Calc constantMember(Member member) { 156 return new ConstantCalc( 157 MemberType.forMember(member), 158 member); 159 } 160 161 /** 162 * Creates an expression which evaluates to a given level. 163 * 164 * @param level Level 165 * @return Constant level expression 166 */ 167 public static Calc constantLevel(Level level) { 168 return new ConstantCalc( 169 LevelType.forLevel(level), 170 level); 171 } 172 173 /** 174 * Creates an expression which evaluates to a given hierarchy. 175 * 176 * @param hierarchy Hierarchy 177 * @return Constant hierarchy expression 178 */ 179 public static Calc constantHierarchy(Hierarchy hierarchy) { 180 return new ConstantCalc( 181 HierarchyType.forHierarchy(hierarchy), 182 hierarchy); 183 } 184 185 /** 186 * Creates an expression which evaluates to a given dimension. 187 * 188 * @param dimension Dimension 189 * @return Constant dimension expression 190 */ 191 public static Calc constantDimension(Dimension dimension) { 192 return new ConstantCalc( 193 DimensionType.forDimension(dimension), 194 dimension); 195 } 196 } 197 198 // End ConstantCalc.java