1 18 package org.apache.activemq.filter; 19 20 import java.math.BigDecimal ; 21 22 import javax.jms.JMSException ; 23 24 29 public class ConstantExpression implements Expression { 30 31 static class BooleanConstantExpression extends ConstantExpression implements BooleanExpression { 32 public BooleanConstantExpression(Object value) { 33 super(value); 34 } 35 public boolean matches(MessageEvaluationContext message) throws JMSException { 36 Object object = evaluate(message); 37 return object!=null && object==Boolean.TRUE; 38 } 39 } 40 41 public static final BooleanConstantExpression NULL = new BooleanConstantExpression(null); 42 public static final BooleanConstantExpression TRUE = new BooleanConstantExpression(Boolean.TRUE); 43 public static final BooleanConstantExpression FALSE = new BooleanConstantExpression(Boolean.FALSE); 44 45 private Object value; 46 47 public static ConstantExpression createFromDecimal(String text) { 48 49 if( text.endsWith("l") || text.endsWith("L") ) 51 text = text.substring(0, text.length()-1); 52 53 Number value; 54 try { 55 value = new Long (text); 56 } catch ( NumberFormatException e) { 57 value = new BigDecimal (text); 59 } 60 61 long l = value.longValue(); 62 if (Integer.MIN_VALUE <= l && l <= Integer.MAX_VALUE) { 63 value = new Integer (value.intValue()); 64 } 65 return new ConstantExpression(value); 66 } 67 68 public static ConstantExpression createFromHex(String text) { 69 Number value = new Long (Long.parseLong(text.substring(2), 16)); 70 long l = value.longValue(); 71 if (Integer.MIN_VALUE <= l && l <= Integer.MAX_VALUE) { 72 value = new Integer (value.intValue()); 73 } 74 return new ConstantExpression(value); 75 } 76 77 public static ConstantExpression createFromOctal(String text) { 78 Number value = new Long (Long.parseLong(text, 8)); 79 long l = value.longValue(); 80 if (Integer.MIN_VALUE <= l && l <= Integer.MAX_VALUE) { 81 value = new Integer (value.intValue()); 82 } 83 return new ConstantExpression(value); 84 } 85 86 public static ConstantExpression createFloat(String text) { 87 Number value = new Double (text); 88 return new ConstantExpression(value); 89 } 90 91 public ConstantExpression(Object value) { 92 this.value = value; 93 } 94 95 public Object evaluate(MessageEvaluationContext message) throws JMSException { 96 return value; 97 } 98 99 public Object getValue() { 100 return value; 101 } 102 103 106 public String toString() { 107 if (value == null) { 108 return "NULL"; 109 } 110 if (value instanceof Boolean ) { 111 return ((Boolean ) value).booleanValue() ? "TRUE" : "FALSE"; 112 } 113 if (value instanceof String ) { 114 return encodeString((String ) value); 115 } 116 return value.toString(); 117 } 118 119 124 public int hashCode() { 125 return toString().hashCode(); 126 } 127 128 133 public boolean equals(Object o) { 134 135 if (o == null || !this.getClass().equals(o.getClass())) { 136 return false; 137 } 138 return toString().equals(o.toString()); 139 140 } 141 142 143 150 public static String encodeString(String s) { 151 StringBuffer b = new StringBuffer (); 152 b.append('\''); 153 for (int i = 0; i < s.length(); i++) { 154 char c = s.charAt(i); 155 if (c == '\'') { 156 b.append(c); 157 } 158 b.append(c); 159 } 160 b.append('\''); 161 return b.toString(); 162 } 163 164 } 165 | Popular Tags |