KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > tonbeller > jpivot > olap > mdxparse > Literal


1 /*
2  * ====================================================================
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) 2003-2004 TONBELLER AG.
7  * All Rights Reserved.
8  * You must accept the terms of that agreement to use this software.
9  * ====================================================================
10  *
11  *
12  */

13 package com.tonbeller.jpivot.olap.mdxparse;
14
15 /**
16  * MDX parser Literal Expressions
17  */

18 public class Literal implements Exp {
19
20   public int type;
21   private Object JavaDoc o;
22
23   public static final Literal emptyString = new Literal("", false);
24   public static final Literal zero = new Literal(new Integer JavaDoc(0));
25   public static final Literal one = new Literal(new Integer JavaDoc(1));
26   public static final Literal doubleZero = new Literal(new Double JavaDoc(0.0));
27   public static final Literal doubleOne = new Literal(new Double JavaDoc(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 JavaDoc s, boolean isSymbol) {
34     this.o = s;
35     this.type = isSymbol ? TYPE_SYMBOL : TYPE_STRING;
36   }
37
38   public static Literal createString(String JavaDoc 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 JavaDoc s) {
47     return new Literal(s, true);
48   }
49
50   private Literal(Double JavaDoc d) {
51     this.o = d;
52     this.type = TYPE_NUMERIC;
53   }
54
55   public static Literal create(Double JavaDoc 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 JavaDoc i) {
66     this.o = i;
67     this.type = TYPE_NUMERIC;
68   }
69
70   public static Literal create(Integer JavaDoc 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   /**
81    * format to MDX
82    */

83   public String JavaDoc toMdx() {
84     return o.toString();
85   }
86
87   /**
88    * Literal is immutable
89    * @see java.lang.Object#clone()
90    */

91   public Object JavaDoc clone() {
92     return this;
93   }
94
95   /**
96    */

97   public String JavaDoc stringValue() {
98     if (type == TYPE_STRING) {
99       // must remove enclosing double quotes
100
String JavaDoc str = (String JavaDoc) o;
101       return str.substring(1, str.length() - 1);
102     } else
103       return o.toString();
104   }
105
106   /**
107    * @return
108    */

109   public Object JavaDoc getValueObject() {
110     return o;
111   }
112
113   /**
114    * @see com.tonbeller.jpivot.olap.mdxparse.Exp#accept
115    */

116   public void accept(ExpVisitor visitor) {
117     visitor.visitLiteral(this);
118   }
119   
120 } // End Literal.java
121
Popular Tags