KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > presumo > jms > selector > JmsFloatLiteral


1 /**
2  * This file is part of Presumo.
3  *
4  * Presumo is free software; you can redistribute it and/or modify
5  * it under the terms of the GNU General Public License as published by
6  * the Free Software Foundation; either version 2 of the License, or
7  * (at your option) any later version.
8  *
9  * Presumo is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12  * GNU General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License
15  * along with Presumo; if not, write to the Free Software
16  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
17  *
18  *
19  * Copyright 2001 Dan Greff
20  */

21 package com.presumo.jms.selector;
22
23 import java.util.Hashtable JavaDoc;
24
25 /**
26  * Expression node to represent floating point literals
27  *
28  * @author Dan Greff
29  */

30 final class JmsFloatLiteral extends JmsNumericLiteral
31 {
32   // the numeric literal value for this instance
33
private Double JavaDoc data;
34     
35   // all instances of JmsFloatLiteral with (Double, JmsFloatLiteral instance)
36
// pairs.
37
private static Hashtable JavaDoc instances = null;
38
39     /////////////////////////////////////////////////////////////////////////
40
// Static Methods //
41
/////////////////////////////////////////////////////////////////////////
42

43   /**
44    * Psuedo-singleton access point. Only one instance for each long value
45    * will ever be created and returned.
46    */

47   static JmsFloatLiteral getInstance(double newValue) {
48     JmsFloatLiteral retval;
49
50     Double JavaDoc search = new Double JavaDoc(newValue);
51     
52     if (instances == null)
53       instances = new Hashtable JavaDoc();
54      
55     retval = (JmsFloatLiteral) instances.get(search);
56     if (retval == null) {
57       retval = new JmsFloatLiteral(newValue);
58       instances.put(search, retval);
59     }
60     else {
61       retval.incrementRefCount();
62     }
63        
64     return retval;
65   }
66
67   /**
68    * During evaluation, JmsIdentifier needs to create JmsFloatLiterals.
69    * These literals need not be singletons. This access point returns
70    * a non-singleton instance.
71    */

72   static JmsFloatLiteral getUniqueInstance(double newValue) {
73     return new JmsFloatLiteral(newValue);
74   }
75
76     /////////////////////////////////////////////////////////////////////////
77
// Constructor //
78
/////////////////////////////////////////////////////////////////////////
79

80   /**
81    * Called by getInstance which controls instance creation.
82    */

83   protected JmsFloatLiteral(double newValue)
84   {
85     super(false); // Make sure this instance isn't tracked
86
this.data = new Double JavaDoc(newValue);
87     this.stringRepresentation = data.toString();
88   }
89   
90     /////////////////////////////////////////////////////////////////////////
91
// Package Methods //
92
/////////////////////////////////////////////////////////////////////////
93

94   Number JavaDoc getNumber() { return data; }
95
96   JmsBooleanLiteral eq(JmsDataType value) throws SelectorFalseException {
97     if (! (value instanceof JmsNumericLiteral))
98       throw SelectorFalseException.getInstance();
99
100     if (data.doubleValue() == ( (JmsNumericLiteral)value ).getNumber().doubleValue() )
101       return JmsBooleanLiteral.TRUE;
102     else
103       return JmsBooleanLiteral.FALSE;
104   }
105
106   JmsBooleanLiteral lt(JmsNumericLiteral value) {
107     if (data.doubleValue() < value.getNumber().doubleValue())
108       return JmsBooleanLiteral.TRUE;
109     else
110       return JmsBooleanLiteral.FALSE;
111   }
112
113   JmsBooleanLiteral gt(JmsNumericLiteral value) {
114     if (data.doubleValue() > value.getNumber().doubleValue())
115       return JmsBooleanLiteral.TRUE;
116     else
117       return JmsBooleanLiteral.FALSE;
118   }
119     
120   JmsNumericLiteral mult(JmsNumericLiteral value) {
121     double result = data.doubleValue() * value.getNumber().doubleValue();
122     return JmsFloatLiteral.getUniqueInstance(result);
123   }
124     
125   JmsNumericLiteral div(JmsNumericLiteral value) {
126     double result = data.doubleValue() / value.getNumber().doubleValue();
127     return JmsFloatLiteral.getUniqueInstance(result);
128   }
129     
130   JmsNumericLiteral add(JmsNumericLiteral value) {
131     double result = data.doubleValue() - value.getNumber().doubleValue();
132     return JmsFloatLiteral.getUniqueInstance(result);
133   }
134     
135   JmsNumericLiteral sub(JmsNumericLiteral value) {
136     double result = data.doubleValue() - value.getNumber().doubleValue();
137     return JmsFloatLiteral.getUniqueInstance(result);
138   }
139     
140   
141   /**
142    * Used to identify this object in places where instanceof is
143    * inapplicable.
144    */

145   short getType() { return JmsOperand.JMS_FLOAT_LITERAL; }
146
147
148     /////////////////////////////////////////////////////////////////////////
149
// Protected Methods //
150
/////////////////////////////////////////////////////////////////////////
151

152   protected void delete(JmsOperand parent) {
153     super.delete(parent);
154     if (refcount == 0) {
155       instances.remove(data);
156       if (instances.size() == 0) {
157           instances = null;
158       }
159     }
160   }
161
162 }
163
164     
165
Popular Tags