/** * GRANITE DATA SERVICES * Copyright (C) 2006-2015 GRANITE DATA SERVICES S.A.S. * * This file is part of the Granite Data Services Platform. * * Granite Data Services is free software; you can redistribute it and/or * modify it under the terms of the GNU Lesser General Public * License as published by the Free Software Foundation; either * version 2.1 of the License, or (at your option) any later version. * * Granite Data Services is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser * General Public License for more details. * * You should have received a copy of the GNU Lesser General Public * License along with this library; if not, write to the Free Software * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, * USA, or see <http://www.gnu.org/licenses/>. */ package org.granite.gravity.selector; import java.math.BigDecimal; import javax.jms.JMSException; /** * Represents a constant expression * * @version $Revision: 1.2 $ */ public class ConstantExpression implements Expression { static class BooleanConstantExpression extends ConstantExpression implements BooleanExpression { public BooleanConstantExpression(Object value) { super(value); } public boolean matches(MessageEvaluationContext message) throws JMSException { Object object = evaluate(message); return object!=null && object==Boolean.TRUE; } } public static final BooleanConstantExpression NULL = new BooleanConstantExpression(null); public static final BooleanConstantExpression TRUE = new BooleanConstantExpression(Boolean.TRUE); public static final BooleanConstantExpression FALSE = new BooleanConstantExpression(Boolean.FALSE); private Object value; public static ConstantExpression createFromDecimal(String text) { // Strip off the 'l' or 'L' if needed. if( text.endsWith("l") || text.endsWith("L") ) text = text.substring(0, text.length()-1); Number value; try { value = new Long(text); } catch ( NumberFormatException e) { // The number may be too big to fit in a long. value = new BigDecimal(text); } long l = value.longValue(); if (Integer.MIN_VALUE <= l && l <= Integer.MAX_VALUE) { value = new Integer(value.intValue()); } return new ConstantExpression(value); } public static ConstantExpression createFromHex(String text) { Number value = new Long(Long.parseLong(text.substring(2), 16)); long l = value.longValue(); if (Integer.MIN_VALUE <= l && l <= Integer.MAX_VALUE) { value = new Integer(value.intValue()); } return new ConstantExpression(value); } public static ConstantExpression createFromOctal(String text) { Number value = new Long(Long.parseLong(text, 8)); long l = value.longValue(); if (Integer.MIN_VALUE <= l && l <= Integer.MAX_VALUE) { value = new Integer(value.intValue()); } return new ConstantExpression(value); } public static ConstantExpression createFloat(String text) { Number value = new Double(text); return new ConstantExpression(value); } public ConstantExpression(Object value) { this.value = value; } public Object evaluate(MessageEvaluationContext message) throws JMSException { return value; } public Object getValue() { return value; } /** * @see java.lang.Object#toString() */ @Override public String toString() { if (value == null) { return "NULL"; } if (value instanceof Boolean) { return ((Boolean) value).booleanValue() ? "TRUE" : "FALSE"; } if (value instanceof String) { return encodeString((String) value); } return value.toString(); } /** * TODO: more efficient hashCode() * * @see java.lang.Object#hashCode() */ @Override public int hashCode() { return toString().hashCode(); } /** * TODO: more efficient hashCode() * * @see java.lang.Object#equals(java.lang.Object) */ @Override public boolean equals(Object o) { if (o == null || !this.getClass().equals(o.getClass())) { return false; } return toString().equals(o.toString()); } /** * Encodes the value of string so that it looks like it would look like * when it was provided in a selector. * * @param s the string to encode * @return the encoded string */ public static String encodeString(String s) { StringBuffer b = new StringBuffer(); b.append('\''); for (int i = 0; i < s.length(); i++) { char c = s.charAt(i); if (c == '\'') { b.append(c); } b.append(c); } b.append('\''); return b.toString(); } }