1 13 package com.tonbeller.jpivot.olap.mdxparse; 14 15 18 public class Literal implements Exp { 19 20 public int type; 21 private Object o; 22 23 public static final Literal emptyString = new Literal("", false); 24 public static final Literal zero = new Literal(new Integer (0)); 25 public static final Literal one = new Literal(new Integer (1)); 26 public static final Literal doubleZero = new Literal(new Double (0.0)); 27 public static final Literal doubleOne = new Literal(new Double (1.0)); 28 29 public static final int TYPE_SYMBOL = 1; 30 public static final int TYPE_STRING = 2; 31 public static final int TYPE_NUMERIC = 3; 32 33 private Literal(String s, boolean isSymbol) { 34 this.o = s; 35 this.type = isSymbol ? TYPE_SYMBOL : TYPE_STRING; 36 } 37 38 public static Literal createString(String s) { 39 if (s.equals("")) { 40 return emptyString; 41 } else { 42 return new Literal(s, false); 43 } 44 } 45 46 public static Literal createSymbol(String s) { 47 return new Literal(s, true); 48 } 49 50 private Literal(Double d) { 51 this.o = d; 52 this.type = TYPE_NUMERIC; 53 } 54 55 public static Literal create(Double d) { 56 if (d.doubleValue() == 0.0) { 57 return doubleZero; 58 } else if (d.doubleValue() == 1.0) { 59 return doubleOne; 60 } else { 61 return new Literal(d); 62 } 63 } 64 65 private Literal(Integer i) { 66 this.o = i; 67 this.type = TYPE_NUMERIC; 68 } 69 70 public static Literal create(Integer i) { 71 if (i.intValue() == 0) { 72 return zero; 73 } else if (i.intValue() == 1) { 74 return one; 75 } else { 76 return new Literal(i); 77 } 78 } 79 80 83 public String toMdx() { 84 return o.toString(); 85 } 86 87 91 public Object clone() { 92 return this; 93 } 94 95 97 public String stringValue() { 98 if (type == TYPE_STRING) { 99 String str = (String ) o; 101 return str.substring(1, str.length() - 1); 102 } else 103 return o.toString(); 104 } 105 106 109 public Object getValueObject() { 110 return o; 111 } 112 113 116 public void accept(ExpVisitor visitor) { 117 visitor.visitLiteral(this); 118 } 119 120 } | Popular Tags |