KickJava   Java API By Example, From Geeks To Geeks.

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


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 String literals
27  *
28  * @author Dan Greff
29  */

30 class JmsStringLiteral extends JmsDataType
31 {
32   // the numeric literal value for this instance
33
final String JavaDoc data;
34  
35   // all instances of JmsStringLiteral with (Identifier, JmsStringLiteral 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 JmsStringLiteral getInstance(String JavaDoc newValue)
48   {
49     JmsStringLiteral retval;
50
51     if (instances == null)
52       instances = new Hashtable JavaDoc();
53    
54     retval = (JmsStringLiteral) instances.get(newValue);
55     if (retval == null) {
56       retval = new JmsStringLiteral(newValue);
57       instances.put(newValue, retval);
58     }
59     else {
60       retval.incrementRefCount();
61     }
62
63     return retval;
64   }
65
66   /**
67    * During evaluation, JmsIdentifier needs to create JmsStringLiterals.
68    * These literals need not be singletons. This access point returns
69    * a non-singleton instance.
70    */

71   static JmsStringLiteral getUniqueInstance(String JavaDoc newValue)
72   {
73     return new JmsStringLiteral(newValue);
74   }
75
76     /////////////////////////////////////////////////////////////////////////
77
// Constructor //
78
/////////////////////////////////////////////////////////////////////////
79

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

83   protected JmsStringLiteral(String JavaDoc newValue)
84   {
85     super(false); // Make sure this instance isn't tracked.
86
this.data = newValue;
87     this.stringRepresentation = '\'' + newValue + '\'';
88   }
89     /////////////////////////////////////////////////////////////////////////
90
// Public Methods //
91
/////////////////////////////////////////////////////////////////////////
92

93   /**
94    * Create a string representation of this object.
95    */

96   public String JavaDoc toString() { return data; }
97
98     /////////////////////////////////////////////////////////////////////////
99
// Package Methods //
100
/////////////////////////////////////////////////////////////////////////
101

102
103   JmsBooleanLiteral eq(JmsDataType value) throws SelectorFalseException {
104   
105     if (! (value instanceof JmsStringLiteral))
106       throw SelectorFalseException.getInstance();
107   
108   
109     if (this.data.equals(value.toString()))
110       return JmsBooleanLiteral.TRUE;
111     else
112       return JmsBooleanLiteral.FALSE;
113   }
114
115   /**
116    * Used to identify this object in places where instanceof is
117    * inapplicable.
118    */

119   short getType() { return JmsOperand.JMS_STRING_LITERAL; }
120
121   
122     /////////////////////////////////////////////////////////////////////////
123
// Protected Methods //
124
/////////////////////////////////////////////////////////////////////////
125
protected void delete(JmsOperand parent) {
126     super.delete(parent);
127     if (refcount == 0) {
128       instances.remove(data);
129       if (instances.size() == 0) {
130         instances = null;
131       }
132     }
133   }
134
135 }
136
137     
138
Popular Tags