KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > net > walend > somnifugi > sql92 > SelectorToken


1 /*
2  * The contents of this file are subject to the terms
3  * of the Common Development and Distribution License
4  * (the "License"). You may not use this file except
5  * in compliance with the License.
6  *
7  * You can obtain a copy of the license at
8  * https://glassfish.dev.java.net/public/CDDLv1.0.html.
9  * See the License for the specific language governing
10  * permissions and limitations under the License.
11  *
12  * When distributing Covered Code, include this CDDL
13  * HEADER in each file and include the License file at
14  * https://glassfish.dev.java.net/public/CDDLv1.0.html.
15  *
16  * If applicable add the following below this CDDL HEADER,
17  * with the fields enclosed by brackets "[]" replaced with
18  * your own identifying information: Portions Copyright
19  * [year] [name of copyright owner]
20  */

21
22 /*
23  * @(#)SelectorToken.java 1.5 05/02/06
24  *
25  * Copyright 2006 Sun Microsystems, Inc. All Rights Reserved.
26  */

27
28 package net.walend.somnifugi.sql92;
29
30 /**
31  * Immutable class that represents a token. A token consists of two
32  * parts. And integer that defines the token, and an optional value
33  * that defines an associated value. For example a LONG token has
34  * an associated value that is the value of the long it represents.
35  */

36 class SelectorToken
37 {
38     
39     // Pre-allocate TRUE, FALSE and UNKNOWN tokens since these are used
40
// constantly during evaluation.
41
static final SelectorToken trueToken =
42             new SelectorToken(Selector.TRUE, "true");
43     static final SelectorToken falseToken =
44             new SelectorToken(Selector.FALSE, "false");
45     static final SelectorToken unknownToken =
46             new SelectorToken(Selector.UNKNOWN, "unknown");
47     
48     // Pre-allocate a couple other tokens that commonly appear in expressions.
49
// Note that LTE and GTE are used to evaluate BETWEEN so it's important
50
// to have them in here.
51
static final SelectorToken equalsToken =
52             new SelectorToken(Selector.EQUALS, "=");
53     static final SelectorToken notEqualsToken =
54             new SelectorToken(Selector.NOT_EQUALS, "<>");
55     static final SelectorToken gtToken =
56             new SelectorToken(Selector.GT, ">");
57     static final SelectorToken gteToken =
58             new SelectorToken(Selector.GTE, ">=");
59     static final SelectorToken ltToken =
60             new SelectorToken(Selector.LT, "<");
61     static final SelectorToken lteToken =
62             new SelectorToken(Selector.LTE, "<=");
63     
64     // Pre-allocate marker tokens
65
static final SelectorToken andMarker =
66             new SelectorToken(Selector.AND_MARKER, "&");
67     static final SelectorToken orMarker =
68             new SelectorToken(Selector.OR_MARKER, "|");
69     
70     // What this token is.
71
int token = Selector.UNKNOWN;
72     
73     // Some tokens have an associated value. For example:
74
// ESCAPE has an escape character.
75
// IDENTIFIER has the identifier String
76
// STRING has the String value
77
// DOUBLE has the Float value
78
Object JavaDoc value = null;
79     
80     public static synchronized SelectorToken getInstance(int token, Object JavaDoc value)
81     {
82         
83         switch (token)
84         {
85             
86             case Selector.TRUE:
87                 return trueToken;
88             case Selector.FALSE:
89                 return falseToken;
90             case Selector.UNKNOWN:
91                 return unknownToken;
92             case Selector.EQUALS:
93                 return equalsToken;
94             case Selector.GTE:
95                 return gteToken;
96             case Selector.LTE:
97                 return lteToken;
98             case Selector.GT:
99                 return gtToken;
100             case Selector.LT:
101                 return ltToken;
102             case Selector.NOT_EQUALS:
103                 return notEqualsToken;
104             case Selector.AND_MARKER:
105                 return andMarker;
106             case Selector.OR_MARKER:
107                 return orMarker;
108             default:
109                 return new SelectorToken(token, value);
110         }
111     }
112     
113     public static SelectorToken getInstance(int token)
114     {
115         return getInstance(token, null);
116     }
117     
118     private SelectorToken(int token)
119     {
120         this.token = token;
121     }
122     
123     private SelectorToken(int token, Object JavaDoc value)
124     {
125         this.token = token;
126         this.value = value;
127     }
128     
129     public int getToken()
130     {
131         return token;
132     }
133     
134     public Object JavaDoc getValue()
135     {
136         return value;
137     }
138     
139     public boolean equals(Object JavaDoc o)
140     {
141         
142         if (this == o) return true;
143         
144         if (!(o instanceof SelectorToken))
145         {
146             return false;
147         }
148         
149         SelectorToken obj = (SelectorToken)o;
150         
151         if (obj.token != token)
152         {
153             return false;
154         }
155         
156         return (value == null ? obj.value == null : value.equals(obj.value));
157     }
158     
159     public int hashCode()
160     {
161         
162         if (value == null)
163         {
164             return token;
165         }
166         else
167         {
168             return value.hashCode() * token;
169         }
170     }
171     
172     public String JavaDoc toString()
173     {
174         return ("[" + token + "," +
175                 (value == null ? "null" : value.toString()) + "]");
176     }
177 }
178
Popular Tags