KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > josql > expressions > ConstantExpression


1 /*
2  * Copyright 2004-2005 Gary Bentley
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License"); you may
5  * not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  * http://www.apache.org/licenses/LICENSE-2.0
8  *
9  * Unless required by applicable law or agreed to in writing, software
10  * distributed under the License is distributed on an "AS IS" BASIS,
11  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12  * See the License for the specific language governing permissions and
13  * limitations under the License.
14  */

15 package org.josql.expressions;
16
17 import org.josql.Query;
18 import org.josql.QueryExecutionException;
19 import org.josql.QueryParseException;
20
21 /**
22  * This class represents a constant String or number.
23  * ALL numbers in JoSQL are represented by <b>double</b>.
24  * <p>
25  * Last Modified By: $Author: barrygently $<br />
26  * Last Modified On: $Date: 2004/12/20 16:22:43 $<br />
27  * Current Revision: $Revision: 1.2 $<br />
28  */

29 public class ConstantExpression extends ValueExpression
30 {
31
32     private Object JavaDoc val = null;
33
34     /**
35      * Get the expected return type.
36      * Will be either: <code>java.lang.String.class</code>
37      * or: <code>java.lang.Double.class</code>.
38      *
39      * @param q The Query object.
40      * @return The expected class.
41      */

42     public Class JavaDoc getExpectedReturnType (Query q)
43     {
44
45     if (this.val == null)
46     {
47
48         return null;
49
50     }
51
52     return this.val.getClass ();
53
54     }
55
56     /**
57      * Inits the expression, in reality does nothing here, can't init a constant!
58      *
59      * @param q The Query object.
60      */

61     public void init (Query q)
62     {
63
64     // Nothing to do...
65

66     }
67
68     public void setValue (Object JavaDoc v)
69     {
70
71     this.val = v;
72
73     }
74
75     /**
76      * Returns whether the value of this constant represents a <code>true</code>
77      * value. See: {@link ArithmeticExpression#isTrue(Object,Query)} for details of how
78      * the return value is determined.
79      *
80      * @param o The current object. Not used in this method.
81      * @param q The Query object.
82      * @return <code>true</code> if the constant evaluates to <code>true</code>.
83      */

84     public boolean isTrue (Object JavaDoc o,
85                Query q)
86     {
87
88     if (this.val == null)
89     {
90
91         return false;
92
93     }
94
95     if (this.val instanceof Number JavaDoc)
96     {
97
98         return ((Number JavaDoc) this.val).doubleValue () > 0;
99
100     }
101
102     return true;
103
104     }
105
106     /**
107      * Get the value of this constant.
108      *
109      * @param o The current object, not used in this method.
110      * @param q The Query object, not used in this method.
111      * @return The constant value.
112      */

113     public Object JavaDoc getValue (Object JavaDoc o,
114                 Query q)
115     {
116
117     return this.val;
118
119     }
120
121     /**
122      * Get the value of this constant.
123      *
124      * @param o The current object, not used in this method.
125      * @param q The Query object, not used in this method.
126      * @return The constant value.
127      */

128     public Object JavaDoc evaluate (Object JavaDoc o,
129                 Query q)
130     {
131
132     return this.val;
133
134     }
135
136     /**
137      * Always returns <code>true</code>, well duh!
138      *
139      * @param q The Query object.
140      * @return <code>true</code> always.
141      */

142     public boolean hasFixedResult (Query q)
143     {
144
145     // Well duh...
146
return true;
147
148     }
149
150     /**
151      * Returns a string representation of this constant.
152      * If the constant is a String then it should be noted that the original
153      * quoting character is not kept in this class and the output of this method
154      * will contain the character "'" instead.
155      * If the constant is a number then "toString" is called on it, so any ","
156      * or other number formatting will have been lost.
157      *
158      * @return A string representation of this constant.
159      */

160     public String JavaDoc toString ()
161     {
162
163     String JavaDoc v = null;
164
165     if (this.val == null)
166     {
167
168         v = val + "";
169
170     } else {
171
172         if (this.val instanceof String JavaDoc)
173         {
174
175         v = "'" + val + "'";
176
177         } else {
178
179         v = this.val.toString ();
180
181         }
182
183     }
184
185     if (this.isBracketed ())
186     {
187
188         v = "(" + v + ")";
189
190     }
191
192     return v;
193
194     }
195
196 }
197
Free Books   Free Magazines  
Popular Tags