001    /** 
002     * 
003     * Copyright 2004 Protique Ltd
004     * Copyright 2004 Hiram Chirino
005     * 
006     * Licensed under the Apache License, Version 2.0 (the "License"); 
007     * you may not use this file except in compliance with the License. 
008     * You may obtain a copy of the License at 
009     * 
010     * http://www.apache.org/licenses/LICENSE-2.0
011     * 
012     * Unless required by applicable law or agreed to in writing, software
013     * distributed under the License is distributed on an "AS IS" BASIS, 
014     * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 
015     * See the License for the specific language governing permissions and 
016     * limitations under the License. 
017     * 
018     **/
019    package org.activemq.filter;
020    
021    import java.math.BigDecimal;
022    
023    import javax.jms.JMSException;
024    import javax.jms.Message;
025    
026    /**
027     * Represents a constant expression
028     * 
029     * @version $Revision: 1.1.1.1 $
030     */
031    public class ConstantExpression implements Expression {
032    
033        static class BooleanConstantExpression extends ConstantExpression implements BooleanExpression {
034            public BooleanConstantExpression(Object value) {
035                super(value);
036            }
037        }
038    
039        public static final BooleanConstantExpression NULL = new BooleanConstantExpression(null);
040        public static final BooleanConstantExpression TRUE = new BooleanConstantExpression(Boolean.TRUE);
041        public static final BooleanConstantExpression FALSE = new BooleanConstantExpression(Boolean.FALSE);
042    
043        private Object value;
044    
045        public static ConstantExpression createFromDecimal(String text) {
046                    
047            // Strip off the 'l' or 'L' if needed.
048            if( text.endsWith("l") || text.endsWith("L") )
049                    text = text.substring(0, text.length()-1);
050    
051            Number value;
052            try {
053                    value = new Long(text);
054            } catch ( NumberFormatException e) {
055                    // The number may be too big to fit in a long.
056                    value = new BigDecimal(text);                   
057            }
058            
059            long l = value.longValue();
060            if (Integer.MIN_VALUE <= l && l <= Integer.MAX_VALUE) {
061                value = new Integer(value.intValue());
062            }
063            return new ConstantExpression(value);
064        }
065    
066        public static ConstantExpression createFromHex(String text) {
067            Number value = new Long(Long.parseLong(text.substring(2), 16));
068            long l = value.longValue();
069            if (Integer.MIN_VALUE <= l && l <= Integer.MAX_VALUE) {
070                value = new Integer(value.intValue());
071            }
072            return new ConstantExpression(value);
073        }
074    
075        public static ConstantExpression createFromOctal(String text) {
076            Number value = new Long(Long.parseLong(text, 8));
077            long l = value.longValue();
078            if (Integer.MIN_VALUE <= l && l <= Integer.MAX_VALUE) {
079                value = new Integer(value.intValue());
080            }
081            return new ConstantExpression(value);
082        }
083    
084        public static ConstantExpression createFloat(String text) {
085            Number value = new Double(text);
086            return new ConstantExpression(value);
087        }
088    
089        public ConstantExpression(Object value) {
090            this.value = value;
091        }
092    
093        public Object evaluate(Message message) throws JMSException {
094            return value;
095        }
096    
097        public Object getValue() {
098            return value;
099        }    
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    }