View Javadoc

1   /*** 
2    * 
3    * Copyright 2004 Protique Ltd
4    * Copyright 2004 Hiram Chirino
5    * 
6    * Licensed under the Apache License, Version 2.0 (the "License"); 
7    * you may not use this file except in compliance with the License. 
8    * 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   **/
19  package org.codehaus.activemq.filter;
20  
21  import javax.jms.JMSException;
22  import javax.jms.Message;
23  
24  /***
25   * Represents a constant expression
26   * 
27   * @version $Revision: 1.1 $
28   */
29  public class ConstantExpression implements Expression {
30  
31      static class BooleanConstantExpression extends ConstantExpression implements BooleanExpression {
32          public BooleanConstantExpression(Object value) {
33              super(value);
34          }
35      }
36  
37      public static final BooleanConstantExpression NULL = new BooleanConstantExpression(null);
38      public static final BooleanConstantExpression TRUE = new BooleanConstantExpression(Boolean.TRUE);
39      public static final BooleanConstantExpression FALSE = new BooleanConstantExpression(Boolean.FALSE);
40  
41      private Object value;
42  
43      public static ConstantExpression createInteger(String text) {
44          Number value = new Long(text);
45          long l = value.longValue();
46          if (Integer.MIN_VALUE <= l && l <= Integer.MAX_VALUE) {
47              value = new Integer(value.intValue());
48          }
49          return new ConstantExpression(value);
50      }
51  
52      public static ConstantExpression createFloat(String text) {
53          Number value = new Double(text);
54          return new ConstantExpression(value);
55      }
56  
57      public ConstantExpression(Object value) {
58          this.value = value;
59      }
60  
61      public Object evaluate(Message message) throws JMSException {
62          return value;
63      }
64  
65  
66      /***
67       * @see java.lang.Object#toString()
68       */
69      public String toString() {
70          if (value == null) {
71              return "NULL";
72          }
73          if (value instanceof Boolean) {
74              return ((Boolean) value).booleanValue() ? "TRUE" : "FALSE";
75          }
76          if (value instanceof String) {
77              return encodeString((String) value);
78          }
79          return value.toString();
80      }
81  
82      /***
83       * TODO: more efficient hashCode()
84       *
85       * @see java.lang.Object#hashCode()
86       */
87      public int hashCode() {
88          return toString().hashCode();
89      }
90  
91      /***
92       * TODO: more efficient hashCode()
93       *
94       * @see java.lang.Object#equals(java.lang.Object)
95       */
96      public boolean equals(Object o) {
97  
98          if (o == null || !this.getClass().equals(o.getClass())) {
99              return false;
100         }
101         return toString().equals(o.toString());
102 
103     }
104 
105 
106     /***
107      * Encodes the value of string so that it looks like it would look like
108      * when it was provided in a selector.
109      *
110      * @param string
111      * @return
112      */
113     private String encodeString(String s) {
114         StringBuffer b = new StringBuffer();
115         b.append('\'');
116         for (int i = 0; i < s.length(); i++) {
117             char c = s.charAt(i);
118             if (c == '\'') {
119                 b.append(c);
120             }
121             b.append(c);
122         }
123         b.append('\'');
124         return b.toString();
125     }
126 }