Docjar: A Java Source and Docuemnt Enginecom.*    java.*    javax.*    org.*    all    new    plug-in

Quick Search    Search Deep

Source code: org/activemq/filter/ConstantExpression.java


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.activemq.filter;
20  
21  import java.math.BigDecimal;
22  
23  import javax.jms.JMSException;
24  import javax.jms.Message;
25  
26  /**
27   * Represents a constant expression
28   * 
29   * @version $Revision: 1.1.1.1 $
30   */
31  public class ConstantExpression implements Expression {
32  
33      static class BooleanConstantExpression extends ConstantExpression implements BooleanExpression {
34          public BooleanConstantExpression(Object value) {
35              super(value);
36          }
37      }
38  
39      public static final BooleanConstantExpression NULL = new BooleanConstantExpression(null);
40      public static final BooleanConstantExpression TRUE = new BooleanConstantExpression(Boolean.TRUE);
41      public static final BooleanConstantExpression FALSE = new BooleanConstantExpression(Boolean.FALSE);
42  
43      private Object value;
44  
45      public static ConstantExpression createFromDecimal(String text) {
46              
47        // Strip off the 'l' or 'L' if needed.
48        if( text.endsWith("l") || text.endsWith("L") )
49          text = text.substring(0, text.length()-1);
50  
51        Number value;
52        try {
53          value = new Long(text);
54        } catch ( NumberFormatException e) {
55          // The number may be too big to fit in a long.
56            value = new BigDecimal(text);        
57        }
58        
59          long l = value.longValue();
60          if (Integer.MIN_VALUE <= l && l <= Integer.MAX_VALUE) {
61              value = new Integer(value.intValue());
62          }
63          return new ConstantExpression(value);
64      }
65  
66      public static ConstantExpression createFromHex(String text) {
67          Number value = new Long(Long.parseLong(text.substring(2), 16));
68          long l = value.longValue();
69          if (Integer.MIN_VALUE <= l && l <= Integer.MAX_VALUE) {
70              value = new Integer(value.intValue());
71          }
72          return new ConstantExpression(value);
73      }
74  
75      public static ConstantExpression createFromOctal(String text) {
76          Number value = new Long(Long.parseLong(text, 8));
77          long l = value.longValue();
78          if (Integer.MIN_VALUE <= l && l <= Integer.MAX_VALUE) {
79              value = new Integer(value.intValue());
80          }
81          return new ConstantExpression(value);
82      }
83  
84      public static ConstantExpression createFloat(String text) {
85          Number value = new Double(text);
86          return new ConstantExpression(value);
87      }
88  
89      public ConstantExpression(Object value) {
90          this.value = value;
91      }
92  
93      public Object evaluate(Message message) throws JMSException {
94          return value;
95      }
96  
97      public Object getValue() {
98          return value;
99      }    
100 
101     /**
102      * @see java.lang.Object#toString()
103      */
104     public String toString() {
105         if (value == null) {
106             return "NULL";
107         }
108         if (value instanceof Boolean) {
109             return ((Boolean) value).booleanValue() ? "TRUE" : "FALSE";
110         }
111         if (value instanceof String) {
112             return encodeString((String) value);
113         }
114         return value.toString();
115     }
116 
117     /**
118      * TODO: more efficient hashCode()
119      *
120      * @see java.lang.Object#hashCode()
121      */
122     public int hashCode() {
123         return toString().hashCode();
124     }
125 
126     /**
127      * TODO: more efficient hashCode()
128      *
129      * @see java.lang.Object#equals(java.lang.Object)
130      */
131     public boolean equals(Object o) {
132 
133         if (o == null || !this.getClass().equals(o.getClass())) {
134             return false;
135         }
136         return toString().equals(o.toString());
137 
138     }
139 
140 
141     /**
142      * Encodes the value of string so that it looks like it would look like
143      * when it was provided in a selector.
144      *
145      * @param string
146      * @return
147      */
148     public static String encodeString(String s) {
149         StringBuffer b = new StringBuffer();
150         b.append('\'');
151         for (int i = 0; i < s.length(); i++) {
152             char c = s.charAt(i);
153             if (c == '\'') {
154                 b.append(c);
155             }
156             b.append(c);
157         }
158         b.append('\'');
159         return b.toString();
160     }
161 }