1 13 14 package mondrian.olap; 15 import mondrian.olap.type.*; 16 import mondrian.calc.*; 17 import mondrian.calc.impl.ConstantCalc; 18 import mondrian.mdx.MdxVisitor; 19 20 import java.io.PrintWriter ; 21 22 32 public class Literal extends ExpBase { 33 34 36 public final int category; 37 private final Object o; 38 39 40 42 public static final Literal nullValue = new Literal(Category.Null, null); 43 44 public static final Literal emptyString = new Literal("", false); 45 46 public static final Literal zero = new Literal(0); 47 48 public static final Literal one = new Literal(1); 49 50 public static final Literal negativeOne = new Literal(-1); 51 52 public static final Literal doubleZero = new Literal(0.0); 53 54 public static final Literal doubleOne = new Literal(1.0); 55 56 public static final Literal doubleNegativeOne = new Literal(-1.0); 57 58 63 private Literal(int type, Object o) { 64 this.category = type; 65 this.o = o; 66 } 67 68 72 public static Literal createString(String s) { 73 return (s.equals("")) 74 ? emptyString 75 : new Literal(s, false); 76 } 77 78 82 public static Literal createSymbol(String s) { 83 return new Literal(s, true); 84 } 85 86 89 public static Literal create(Double d) { 90 double dv = d.doubleValue(); 91 if (dv == 0.0) { 92 return doubleZero; 93 } else if (dv == 1.0) { 94 return doubleOne; 95 } else if (dv == -1.0) { 96 return doubleNegativeOne; 97 } else { 98 return new Literal(d); 99 } 100 } 101 102 105 public static Literal create(Integer i) { 106 switch (i) { 107 case -1: 108 return negativeOne; 109 case 0: 110 return zero; 111 case 1: 112 return one; 113 default: 114 return new Literal(i); 115 } 116 } 117 118 private Literal(String s, boolean isSymbol) { 119 this.o = s; 120 this.category = isSymbol ? Category.Symbol : Category.String; 121 } 122 123 private Literal(Double d) { 124 this.o = d; 125 this.category = Category.Numeric; 126 } 127 private Literal(Integer i) { 128 this.o = i; 129 this.category = Category.Numeric; 130 } 131 132 public Literal clone() { 133 return this; 134 } 135 136 public void unparse(PrintWriter pw) { 137 switch (category) { 138 case Category.Symbol: 139 case Category.Numeric: 140 pw.print(o); 141 break; 142 case Category.String: 143 pw.print(Util.quoteForMdx((String ) o)); 144 break; 145 case Category.Null: 146 pw.print("NULL"); 147 break; 148 default: 149 throw Util.newInternal("bad literal type " + category); 150 } 151 } 152 153 public int getCategory() { 154 return category; 155 } 156 157 public Type getType() { 158 switch (category) { 159 case Category.Symbol: 160 return new SymbolType(); 161 case Category.Numeric: 162 return new NumericType(); 163 case Category.String: 164 return new StringType(); 165 case Category.Null: 166 return new NullType(); 167 default: 168 throw Category.instance.badValue(category); 169 } 170 } 171 172 public Exp accept(Validator validator) { 173 return this; 174 } 175 176 public Calc accept(ExpCompiler compiler) { 177 return new ConstantCalc(getType(), o); 178 } 179 180 public Object accept(MdxVisitor visitor) { 181 return visitor.visit(this); 182 } 183 184 public Object getValue() { 185 return o; 186 } 187 188 public int getIntValue() { 189 if (o instanceof Number ) { 190 return ((Number ) o).intValue(); 191 } else { 192 throw Util.newInternal("cannot convert " + o + " to int"); 193 } 194 } 195 196 } 197 198 | Popular Tags |