ConstantExpression.java
001 /* Licensed to the Apache Software Foundation (ASF) under one
002  * or more contributor license agreements.  See the NOTICE file
003  * distributed with this work for additional information
004  * regarding copyright ownership.  The ASF licenses this file
005  * to you under the Apache License, Version 2.0 (the
006  * "License"); you may not use this file except in compliance
007  * with the License.  You may obtain a copy of the License at
008  
009  *   http://www.apache.org/licenses/LICENSE-2.0
010  
011  * Unless required by applicable law or agreed to in writing,
012  * software distributed under the License is distributed on an
013  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
014  * KIND, either express or implied.  See the License for the
015  * specific language governing permissions and limitations
016  * under the License.
017  */
018 package org.apache.qpid.filter;
019 
020 import org.apache.qpid.QpidException;
021 import org.apache.qpid.client.message.AbstractJMSMessage;
022 
023 import java.math.BigDecimal;
024 
025 /**
026  * Represents a constant expression
027  */
028 public class ConstantExpression implements Expression
029 {
030 
031     static class BooleanConstantExpression extends ConstantExpression implements BooleanExpression
032     {
033         public BooleanConstantExpression(Object value)
034         {
035             super(value);
036         }
037 
038         public boolean matches(AbstractJMSMessage messagethrows QpidException
039         {
040             Object object = evaluate(message);
041 
042             return (object != null&& (object == Boolean.TRUE);
043         }
044     }
045 
046     public static final BooleanConstantExpression NULL = new BooleanConstantExpression(null);
047     public static final BooleanConstantExpression TRUE = new BooleanConstantExpression(Boolean.TRUE);
048     public static final BooleanConstantExpression FALSE = new BooleanConstantExpression(Boolean.FALSE);
049 
050     private Object value;
051 
052     public static ConstantExpression createFromDecimal(String text)
053     {
054 
055         // Strip off the 'l' or 'L' if needed.
056         if (text.endsWith("l"|| text.endsWith("L"))
057         {
058             text = text.substring(0, text.length() 1);
059         }
060 
061         Number value;
062         try
063         {
064             value = new Long(text);
065         }
066         catch (NumberFormatException e)
067         {
068             // The number may be too big to fit in a long.
069             value = new BigDecimal(text);
070         }
071 
072         long l = value.longValue();
073         if ((Integer.MIN_VALUE <= l&& (l <= Integer.MAX_VALUE))
074         {
075             value = new Integer(value.intValue());
076         }
077 
078         return new ConstantExpression(value);
079     }
080 
081     public static ConstantExpression createFromHex(String text)
082     {
083         Number value = new Long(Long.parseLong(text.substring(2)16));
084         long l = value.longValue();
085         if ((Integer.MIN_VALUE <= l&& (l <= Integer.MAX_VALUE))
086         {
087             value = new Integer(value.intValue());
088         }
089 
090         return new ConstantExpression(value);
091     }
092 
093     public static ConstantExpression createFromOctal(String text)
094     {
095         Number value = new Long(Long.parseLong(text, 8));
096         long l = value.longValue();
097         if ((Integer.MIN_VALUE <= l&& (l <= Integer.MAX_VALUE))
098         {
099             value = new Integer(value.intValue());
100         }
101 
102         return new ConstantExpression(value);
103     }
104 
105     public static ConstantExpression createFloat(String text)
106     {
107         Number value = new Double(text);
108 
109         return new ConstantExpression(value);
110     }
111 
112     public ConstantExpression(Object value)
113     {
114         this.value = value;
115     }
116 
117     public Object evaluate(AbstractJMSMessage messagethrows QpidException
118     {
119         return value;
120     }
121 
122     public Object getValue()
123     {
124         return value;
125     }
126 
127     /**
128      @see Object#toString()
129      */
130     public String toString()
131     {
132         if (value == null)
133         {
134             return "NULL";
135         }
136 
137         if (value instanceof Boolean)
138         {
139             return ((Booleanvalue).booleanValue() "TRUE" "FALSE";
140         }
141 
142         if (value instanceof String)
143         {
144             return ConstantExpression.encodeString((Stringvalue);
145         }
146 
147         return value.toString();
148     }
149 
150     /**
151      * TODO: more efficient hashCode()
152      *
153      @see Object#hashCode()
154      */
155     public int hashCode()
156     {
157         return toString().hashCode();
158     }
159 
160     /**
161      * TODO: more efficient hashCode()
162      *
163      @see Object#equals(Object)
164      */
165     public boolean equals(Object o)
166     {
167 
168         if ((o == null|| !this.getClass().equals(o.getClass()))
169         {
170             return false;
171         }
172 
173         return toString().equals(o.toString());
174 
175     }
176 
177     /**
178      * Encodes the value of string so that it looks like it would look like
179      * when it was provided in a selector.
180      *
181      @param s
182      @return
183      */
184     public static String encodeString(String s)
185     {
186         StringBuffer b = new StringBuffer();
187         b.append('\'');
188         for (int i = 0; i < s.length(); i++)
189         {
190             char c = s.charAt(i);
191             if (c == '\'')
192             {
193                 b.append(c);
194             }
195 
196             b.append(c);
197         }
198 
199         b.append('\'');
200 
201         return b.toString();
202     }
203 
204 }