KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > mondrian > olap > Literal


1 /*
2 // $Id: //open/mondrian/src/main/mondrian/olap/Literal.java#22 $
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) 1998-2002 Kana Software, Inc.
7 // Copyright (C) 2001-2006 Julian Hyde and others
8 // All Rights Reserved.
9 // You must accept the terms of that agreement to use this software.
10 //
11 // jhyde, 21 January, 1999
12 */

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 JavaDoc;
21
22 /**
23  * Represents a constant value, such as a string or number, in a parse tree.
24  *
25  * <p>Symbols, such as the <code>ASC</code> keyword in
26  * <code>Order([Store].Members, [Measures].[Unit Sales], ASC)</code>, are
27  * also represented as Literals.
28  *
29  * @version $Id: //open/mondrian/src/main/mondrian/olap/Literal.java#22 $
30  * @author jhyde
31  */

32 public class Literal extends ExpBase {
33
34     // Data members.
35

36     public final int category;
37     private final Object JavaDoc o;
38
39
40     // Constants for commonly used literals.
41

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     /**
59      * Private constructor.
60      *
61      * <p>Use the creation methods {@link #createString(String)} etc.
62      */

63     private Literal(int type, Object JavaDoc o) {
64         this.category = type;
65         this.o = o;
66     }
67
68     /**
69      * Creates a string literal.
70      * @see #createSymbol
71      */

72     public static Literal createString(String JavaDoc s) {
73         return (s.equals(""))
74             ? emptyString
75             : new Literal(s, false);
76     }
77
78     /**
79      * Creates a symbol.
80      * @see #createString
81      */

82     public static Literal createSymbol(String JavaDoc s) {
83         return new Literal(s, true);
84     }
85
86     /**
87      * Creates a numeric literal.
88      */

89     public static Literal create(Double JavaDoc 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     /**
103      * Creates an integer literal.
104      */

105     public static Literal create(Integer JavaDoc 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 JavaDoc s, boolean isSymbol) {
119         this.o = s;
120         this.category = isSymbol ? Category.Symbol : Category.String;
121     }
122
123     private Literal(Double JavaDoc d) {
124         this.o = d;
125         this.category = Category.Numeric;
126     }
127     private Literal(Integer JavaDoc 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 JavaDoc 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 JavaDoc) 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 JavaDoc accept(MdxVisitor visitor) {
181         return visitor.visit(this);
182     }
183
184     public Object JavaDoc getValue() {
185         return o;
186     }
187
188     public int getIntValue() {
189         if (o instanceof Number JavaDoc) {
190             return ((Number JavaDoc) o).intValue();
191         } else {
192             throw Util.newInternal("cannot convert " + o + " to int");
193         }
194     }
195
196 }
197
198 // End Literal.java
199
Popular Tags