The relational operators; equal, not equal, less than, less than or equal, greater than, and greater than or equal are supported using standard operator notation.
ExpressionEvaluator.GetValue(null, "2 == 2") // true ExpressionEvaluator.GetValue(null, "date('1974-08-24') != DateTime.Today") // true ExpressionEvaluator.GetValue(null, "2 < -5.0") // false ExpressionEvaluator.GetValue(null, "DateTime.Today <= date('1974-08-24')") // false ExpressionEvaluator.GetValue(null, "'Test' >= 'test'") // true ExpressionEvaluator.GetValue(null, "3 in {1, 2, 3, 4, 5}") // true ExpressionEvaluator.GetValue(null, "'Abc' like '[A-Z]b*'") // true ExpressionEvaluator.GetValue(null, "'Abc' like '?'") // false ExpressionEvaluator.GetValue(null, "1 between {1, 5}") // true ExpressionEvaluator.GetValue(null, "'efg' between {'abc', 'xyz'}") // true ExpressionEvaluator.GetValue(null, "'xyz' is int") // false ExpressionEvaluator.GetValue(null, "{1, 2, 3, 4, 5} is IList") // true ExpressionEvaluator.GetValue(null, "'5.0067' matches '^-?\d+(\.\d{2})?$'")) // false ExpressionEvaluator.GetValue(null, @"'5.00' matches '^-?\d+(\.\d{2})?$'") // true
The logical operators that are supported are and, or, and not. Their use is demonstrated below
// AND bool falseValue = (bool) ExpressionEvaluator.GetValue(null, "true and false"); //false string expression = @"IsMember('Nikola Tesla') and IsMember('Mihajlo Pupin')"; bool trueValue = (bool) ExpressionEvaluator.GetValue(ieee, expression); //true // OR bool trueValue = (bool) ExpressionEvaluator.GetValue(null, "true or false"); //true string expression = @"IsMember('Nikola Tesla') or IsMember('Albert Einstien')"; bool trueValue = (bool) ExpressionEvaluator.GetValue(ieee, expression); // true // NOT bool falseValue = (bool) ExpressionEvaluator.GetValue(null, "!true"); // AND and NOT string expression = @"IsMember('Nikola Tesla') and !IsMember('Mihajlo Pupin')"; bool falseValue = (bool) ExpressionEvaluator.GetValue(ieee, expression);
The bitwise operators that are supported are and, or, xor and not. Their use is demonstrated below. Note, that the logical and bitwise operators are the same and their interpretation depends if you pass in integral values or boolean values.
// AND int result = (int) ExpressionEvaluator.GetValue(null, "1 and 3"); // 1 & 3 // OR int result = (int) ExpressionEvaluator.GetValue(null, "1 or 3"); // 1 | 3 // XOR int result = (int) ExpressionEvaluator.GetValue(null, "1 xor 3"); // 1 ^ 3 // NOT int result = (int) ExpressionEvaluator.GetValue(null, "!1"); // ~1
The addition operator can be used on numbers, strings and dates. Subtraction can be used on numbers and dates. Multiplication and division can be used only on numbers. Other mathematical operators supported are modulus (%) and exponential power (^). Standard operator precedence is enforced.
// Addition int two = (int)ExpressionEvaluator.GetValue(null, "1 + 1"); // 2 String testString = (String) ExpressionEvaluator.GetValue(null, "'test' + ' ' + 'string'"); //'test string' DateTime dt = (DateTime) ExpressionEvaluator.GetValue(null, "date('1974-08-24') + 5"); // 8/29/1974 // Subtraction int four = (int) ExpressionEvaluator.GetValue(null, "1 - -3"); //4 Decimal dec = (Decimal) ExpressionEvaluator.GetValue(null, "1000.00m - 1e4"); // 9000.00 TimeSpan ts = (TimeSpan) ExpressionEvaluator.GetValue(null, "date('2004-08-14') - date('1974-08-24')"); //10948.00:00:00 // Multiplication int six = (int) ExpressionEvaluator.GetValue(null, "-2 * -3"); // 6 int twentyFour = (int) ExpressionEvaluator.GetValue(null, "2.0 * 3e0 * 4"); // 24 // Division int minusTwo = (int) ExpressionEvaluator.GetValue(null, "6 / -3"); // -2 int one = (int) ExpressionEvaluator.GetValue(null, "8.0 / 4e0 / 2"); // 1 // Modulus int three = (int) ExpressionEvaluator.GetValue(null, "7 % 4"); // 3 int one = (int) ExpressionEvaluator.GetValue(null, "8.0 % 5e0 % 2"); // 1 // Exponent int sixteen = (int) ExpressionEvaluator.GetValue(null, "-2 ^ 4"); // 16 // Operator precedence int minusFortyFive = (int) ExpressionEvaluator.GetValue(null, "1+2-3*8^2/2/2"); // -45
You can use the ternary operator for performing if-then-else conditional logic inside the expression. A minimal example is
String aTrueString = (String) ExpressionEvaluator.GetValue(null, false ? 'trueExp' : 'falseExp'")
Example
package com.javasafari.core; import org.springframework.beans.factory.annotation.Value; import org.springframework.stereotype.Component; @Component("clientBean") public class Client { @Value("#{itemBean.qty > 1000 ? true : false}") private boolean discount; public boolean isDiscount() { return discount; } public void setDiscount(boolean discount) { this.discount = discount; } @Override public String toString() { return "Client [discount=" + discount + "]"; } }
package com.javasafari.core; import org.springframework.beans.factory.annotation.Value; import org.springframework.stereotype.Component; @Component("itemBean") public class Item { @Value("1900") private int qty; public int getQty() { return qty; } public void setQty(int qty) { this.qty = qty; } }
Output Client [discount=true]
<beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd"> <bean id="client" class="com.javasafari.core.Customer"> <property name="discount" value="#{itemBean.qty < 1000 ? true : false}" /> </bean> <bean id="itemBean" class="com.javasafari.core.Item"> <property name="qty" value="1900" /> </bean> </beans>
Output Client [discount=true]