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: **/package org.activemq.filter.mockrunner;
019:
020: import java.math.BigDecimal;
021:
022: import javax.jms.JMSException;
023: import javax.jms.Message;
024:
025: /**
026: * Alwin Ibba: Changed package
027: *
028: * Represents a constant expression
029: *
030: * @version $Revision: 1.3 $
031: */
032: public class ConstantExpression implements Expression {
033:
034: static class BooleanConstantExpression extends ConstantExpression
035: implements BooleanExpression {
036: public BooleanConstantExpression(Object value) {
037: super (value);
038: }
039: }
040:
041: public static final BooleanConstantExpression NULL = new BooleanConstantExpression(
042: null);
043: public static final BooleanConstantExpression TRUE = new BooleanConstantExpression(
044: Boolean.TRUE);
045: public static final BooleanConstantExpression FALSE = new BooleanConstantExpression(
046: Boolean.FALSE);
047:
048: private Object value;
049:
050: public static ConstantExpression createFromDecimal(String text) {
051:
052: // Strip off the 'l' or 'L' if needed.
053: if (text.endsWith("l") || text.endsWith("L"))
054: text = text.substring(0, text.length() - 1);
055:
056: Number value;
057: try {
058: value = new Long(text);
059: } catch (NumberFormatException e) {
060: // The number may be too big to fit in a long.
061: value = new BigDecimal(text);
062: }
063:
064: long l = value.longValue();
065: if (Integer.MIN_VALUE <= l && l <= Integer.MAX_VALUE) {
066: value = new Integer(value.intValue());
067: }
068: return new ConstantExpression(value);
069: }
070:
071: public static ConstantExpression createFromHex(String text) {
072: Number value = new Long(Long.parseLong(text.substring(2), 16));
073: long l = value.longValue();
074: if (Integer.MIN_VALUE <= l && l <= Integer.MAX_VALUE) {
075: value = new Integer(value.intValue());
076: }
077: return new ConstantExpression(value);
078: }
079:
080: public static ConstantExpression createFromOctal(String text) {
081: Number value = new Long(Long.parseLong(text, 8));
082: long l = value.longValue();
083: if (Integer.MIN_VALUE <= l && l <= Integer.MAX_VALUE) {
084: value = new Integer(value.intValue());
085: }
086: return new ConstantExpression(value);
087: }
088:
089: public static ConstantExpression createFloat(String text) {
090: Number value = new Double(text);
091: return new ConstantExpression(value);
092: }
093:
094: public ConstantExpression(Object value) {
095: this .value = value;
096: }
097:
098: public Object evaluate(Message message) throws JMSException {
099: return value;
100: }
101:
102: public Object getValue() {
103: return value;
104: }
105:
106: /**
107: * @see java.lang.Object#toString()
108: */
109: public String toString() {
110: if (value == null) {
111: return "NULL";
112: }
113: if (value instanceof Boolean) {
114: return ((Boolean) value).booleanValue() ? "TRUE" : "FALSE";
115: }
116: if (value instanceof String) {
117: return encodeString((String) value);
118: }
119: return value.toString();
120: }
121:
122: /**
123: * TODO: more efficient hashCode()
124: *
125: * @see java.lang.Object#hashCode()
126: */
127: public int hashCode() {
128: return toString().hashCode();
129: }
130:
131: /**
132: * TODO: more efficient hashCode()
133: *
134: * @see java.lang.Object#equals(java.lang.Object)
135: */
136: public boolean equals(Object o) {
137:
138: if (o == null || !this .getClass().equals(o.getClass())) {
139: return false;
140: }
141: return toString().equals(o.toString());
142:
143: }
144:
145: /**
146: * Encodes the value of string so that it looks like it would look like
147: * when it was provided in a selector.
148: *
149: * @param string
150: * @return
151: */
152: public static String encodeString(String s) {
153: StringBuffer b = new StringBuffer();
154: b.append('\'');
155: for (int i = 0; i < s.length(); i++) {
156: char c = s.charAt(i);
157: if (c == '\'') {
158: b.append(c);
159: }
160: b.append(c);
161: }
162: b.append('\'');
163: return b.toString();
164: }
165: }
|