001/**
002 *   GRANITE DATA SERVICES
003 *   Copyright (C) 2006-2013 GRANITE DATA SERVICES S.A.S.
004 *
005 *   This file is part of the Granite Data Services Platform.
006 *
007 *   Granite Data Services is free software; you can redistribute it and/or
008 *   modify it under the terms of the GNU Lesser General Public
009 *   License as published by the Free Software Foundation; either
010 *   version 2.1 of the License, or (at your option) any later version.
011 *
012 *   Granite Data Services is distributed in the hope that it will be useful,
013 *   but WITHOUT ANY WARRANTY; without even the implied warranty of
014 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser
015 *   General Public License for more details.
016 *
017 *   You should have received a copy of the GNU Lesser General Public
018 *   License along with this library; if not, write to the Free Software
019 *   Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301,
020 *   USA, or see <http://www.gnu.org/licenses/>.
021 */
022package org.granite.gravity.selector;
023
024import java.math.BigDecimal;
025
026import javax.jms.JMSException;
027
028/**
029 * Represents a constant expression
030 *
031 * @version $Revision: 1.2 $
032 */
033public class ConstantExpression implements Expression {
034
035    static class BooleanConstantExpression extends ConstantExpression implements BooleanExpression {
036        public BooleanConstantExpression(Object value) {
037            super(value);
038        }
039        public boolean matches(MessageEvaluationContext message) throws JMSException {
040            Object object = evaluate(message);
041            return object!=null && object==Boolean.TRUE;
042        }
043    }
044
045    public static final BooleanConstantExpression NULL = new BooleanConstantExpression(null);
046    public static final BooleanConstantExpression TRUE = new BooleanConstantExpression(Boolean.TRUE);
047    public static final BooleanConstantExpression FALSE = new BooleanConstantExpression(Boolean.FALSE);
048
049    private Object value;
050
051    public static ConstantExpression createFromDecimal(String text) {
052
053        // Strip off the 'l' or 'L' if needed.
054        if( text.endsWith("l") || text.endsWith("L") )
055            text = text.substring(0, text.length()-1);
056
057        Number value;
058        try {
059            value = new Long(text);
060        } catch ( NumberFormatException e) {
061            // The number may be too big to fit in a long.
062            value = new BigDecimal(text);
063        }
064
065        long l = value.longValue();
066        if (Integer.MIN_VALUE <= l && l <= Integer.MAX_VALUE) {
067            value = new Integer(value.intValue());
068        }
069        return new ConstantExpression(value);
070    }
071
072    public static ConstantExpression createFromHex(String text) {
073        Number value = new Long(Long.parseLong(text.substring(2), 16));
074        long l = value.longValue();
075        if (Integer.MIN_VALUE <= l && l <= Integer.MAX_VALUE) {
076            value = new Integer(value.intValue());
077        }
078        return new ConstantExpression(value);
079    }
080
081    public static ConstantExpression createFromOctal(String text) {
082        Number value = new Long(Long.parseLong(text, 8));
083        long l = value.longValue();
084        if (Integer.MIN_VALUE <= l && l <= Integer.MAX_VALUE) {
085            value = new Integer(value.intValue());
086        }
087        return new ConstantExpression(value);
088    }
089
090    public static ConstantExpression createFloat(String text) {
091        Number value = new Double(text);
092        return new ConstantExpression(value);
093    }
094
095    public ConstantExpression(Object value) {
096        this.value = value;
097    }
098
099    public Object evaluate(MessageEvaluationContext message) throws JMSException {
100        return value;
101    }
102
103    public Object getValue() {
104        return value;
105    }
106
107    /**
108     * @see java.lang.Object#toString()
109     */
110    @Override
111    public String toString() {
112        if (value == null) {
113            return "NULL";
114        }
115        if (value instanceof Boolean) {
116            return ((Boolean) value).booleanValue() ? "TRUE" : "FALSE";
117        }
118        if (value instanceof String) {
119            return encodeString((String) value);
120        }
121        return value.toString();
122    }
123
124    /**
125     * TODO: more efficient hashCode()
126     *
127     * @see java.lang.Object#hashCode()
128     */
129    @Override
130    public int hashCode() {
131        return toString().hashCode();
132    }
133
134    /**
135     * TODO: more efficient hashCode()
136     *
137     * @see java.lang.Object#equals(java.lang.Object)
138     */
139    @Override
140    public boolean equals(Object o) {
141
142        if (o == null || !this.getClass().equals(o.getClass())) {
143            return false;
144        }
145        return toString().equals(o.toString());
146
147    }
148
149
150    /**
151     * Encodes the value of string so that it looks like it would look like
152     * when it was provided in a selector.
153     *
154     * @param s the string to encode
155     * @return the encoded string
156     */
157    public static String encodeString(String s) {
158        StringBuffer b = new StringBuffer();
159        b.append('\'');
160        for (int i = 0; i < s.length(); i++) {
161            char c = s.charAt(i);
162            if (c == '\'') {
163                b.append(c);
164            }
165            b.append(c);
166        }
167        b.append('\'');
168        return b.toString();
169    }
170
171}