KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > activemq > filter > ConstantExpression


1 /**
2  *
3  * Licensed to the Apache Software Foundation (ASF) under one or more
4  * contributor license agreements. See the NOTICE file distributed with
5  * this work for additional information regarding copyright ownership.
6  * The ASF licenses this file to You under the Apache License, Version 2.0
7  * (the "License"); you may not use this file except in compliance with
8  * the License. You may obtain a copy of the License at
9  *
10  * http://www.apache.org/licenses/LICENSE-2.0
11  *
12  * Unless required by applicable law or agreed to in writing, software
13  * distributed under the License is distributed on an "AS IS" BASIS,
14  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15  * See the License for the specific language governing permissions and
16  * limitations under the License.
17  */

18 package org.apache.activemq.filter;
19
20 import java.math.BigDecimal JavaDoc;
21
22 import javax.jms.JMSException JavaDoc;
23
24 /**
25  * Represents a constant expression
26  *
27  * @version $Revision: 1.2 $
28  */

29 public class ConstantExpression implements Expression {
30
31     static class BooleanConstantExpression extends ConstantExpression implements BooleanExpression {
32         public BooleanConstantExpression(Object JavaDoc value) {
33             super(value);
34         }
35         public boolean matches(MessageEvaluationContext message) throws JMSException JavaDoc {
36             Object JavaDoc object = evaluate(message);
37             return object!=null && object==Boolean.TRUE;
38         }
39     }
40
41     public static final BooleanConstantExpression NULL = new BooleanConstantExpression(null);
42     public static final BooleanConstantExpression TRUE = new BooleanConstantExpression(Boolean.TRUE);
43     public static final BooleanConstantExpression FALSE = new BooleanConstantExpression(Boolean.FALSE);
44
45     private Object JavaDoc value;
46
47     public static ConstantExpression createFromDecimal(String JavaDoc text) {
48                 
49         // Strip off the 'l' or 'L' if needed.
50
if( text.endsWith("l") || text.endsWith("L") )
51             text = text.substring(0, text.length()-1);
52
53         Number JavaDoc value;
54         try {
55             value = new Long JavaDoc(text);
56         } catch ( NumberFormatException JavaDoc e) {
57             // The number may be too big to fit in a long.
58
value = new BigDecimal JavaDoc(text);
59         }
60         
61         long l = value.longValue();
62         if (Integer.MIN_VALUE <= l && l <= Integer.MAX_VALUE) {
63             value = new Integer JavaDoc(value.intValue());
64         }
65         return new ConstantExpression(value);
66     }
67
68     public static ConstantExpression createFromHex(String JavaDoc text) {
69         Number JavaDoc value = new Long JavaDoc(Long.parseLong(text.substring(2), 16));
70         long l = value.longValue();
71         if (Integer.MIN_VALUE <= l && l <= Integer.MAX_VALUE) {
72             value = new Integer JavaDoc(value.intValue());
73         }
74         return new ConstantExpression(value);
75     }
76
77     public static ConstantExpression createFromOctal(String JavaDoc text) {
78         Number JavaDoc value = new Long JavaDoc(Long.parseLong(text, 8));
79         long l = value.longValue();
80         if (Integer.MIN_VALUE <= l && l <= Integer.MAX_VALUE) {
81             value = new Integer JavaDoc(value.intValue());
82         }
83         return new ConstantExpression(value);
84     }
85
86     public static ConstantExpression createFloat(String JavaDoc text) {
87         Number JavaDoc value = new Double JavaDoc(text);
88         return new ConstantExpression(value);
89     }
90
91     public ConstantExpression(Object JavaDoc value) {
92         this.value = value;
93     }
94
95     public Object JavaDoc evaluate(MessageEvaluationContext message) throws JMSException JavaDoc {
96         return value;
97     }
98
99     public Object JavaDoc getValue() {
100         return value;
101     }
102
103     /**
104      * @see java.lang.Object#toString()
105      */

106     public String JavaDoc toString() {
107         if (value == null) {
108             return "NULL";
109         }
110         if (value instanceof Boolean JavaDoc) {
111             return ((Boolean JavaDoc) value).booleanValue() ? "TRUE" : "FALSE";
112         }
113         if (value instanceof String JavaDoc) {
114             return encodeString((String JavaDoc) value);
115         }
116         return value.toString();
117     }
118
119     /**
120      * TODO: more efficient hashCode()
121      *
122      * @see java.lang.Object#hashCode()
123      */

124     public int hashCode() {
125         return toString().hashCode();
126     }
127
128     /**
129      * TODO: more efficient hashCode()
130      *
131      * @see java.lang.Object#equals(java.lang.Object)
132      */

133     public boolean equals(Object JavaDoc o) {
134
135         if (o == null || !this.getClass().equals(o.getClass())) {
136             return false;
137         }
138         return toString().equals(o.toString());
139
140     }
141
142
143     /**
144      * Encodes the value of string so that it looks like it would look like
145      * when it was provided in a selector.
146      *
147      * @param string
148      * @return
149      */

150     public static String JavaDoc encodeString(String JavaDoc s) {
151         StringBuffer JavaDoc b = new StringBuffer JavaDoc();
152         b.append('\'');
153         for (int i = 0; i < s.length(); i++) {
154             char c = s.charAt(i);
155             if (c == '\'') {
156                 b.append(c);
157             }
158             b.append(c);
159         }
160         b.append('\'');
161         return b.toString();
162     }
163     
164 }
165
Popular Tags