Spring Expression - Operators

Operators 

Relational operators 

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.
// evaluates to true 
boolean trueValue = parser.parseExpression("2 == 2").getValue(Boolean.class); 
// evaluates to false 
boolean falseValue = parser.parseExpression("2 < -5.0").getValue(Boolean.class); 
// evaluates to true 
boolean trueValue = parser.parseExpression("'black' < 'block'").getValue(Boolean.class);

In addition to standard relational operators SpEL supports the instanceof and regular expression based matches operator.

// evaluates to false

boolean falseValue = parser.parseExpression( "'xyz' instanceof T(Integer.class)").getValue(Boolean.class);
// evaluates to true
boolean trueValue = parser.parseExpression( "'5.00' matches '\^-?\\d+(\\.\\d{2})?$'").getValue(Boolean.class);
//evaluates to false
boolean falseValue = parser.parseExpression( "'5.0067' matches '\^-?\\d+(\\.\\d{2})?$'").getValue(Boolean.class);

Each symbolic operator can also be specified as a purely alphabetic equivalent. This avoids problems where the symbols used have special meaning for the document type in which the expression is embedded (eg. an XML document). The textual equivalents are shown here: lt (<), gt (>), le (#), ge (>=), eq (==), ne (!=), div (/), mod (%), not (!). These are case insensitive.


Logical operators 

The logical operators that are supported are and, or, and not. Their use is demonstrated below.

boolean falseValue = parser.parseExpression("true and false").getValue(Boolean.class);

// evaluates to true
String expression = "isMember('Nikola Tesla') and isMember('Mihajlo Pupin')";
boolean trueValue = parser.parseExpression(expression).getValue(societyContext, Boolean.class);
boolean trueValue = parser.parseExpression("true or false").getValue(Boolean.class);

String expression = "isMember('Nikola Tesla') or isMember('Albert Einstein')";
boolean trueValue = parser.parseExpression(expression).getValue(societyContext, Boolean.class);

boolean falseValue = parser.parseExpression("!true").getValue(Boolean.class);
String expression = "isMember('Nikola Tesla') and !isMember('Mihajlo Pupin')";
boolean falseValue = parser.parseExpression(expression).getValue(societyContext, Boolean.class);


Mathematical operators 

The addition operator can be used on both numbers and strings. Subtraction, multiplication and division can be used only on numbers. Other mathematical operators supported are modulus (%) and exponential power (^). Standard operator precedence is enforced. These operators are demonstrated below.

// Addition 
int two = parser.parseExpression("1 + 1").getValue(Integer.class); // 2 
String testString = parser.parseExpression( "'test' + ' ' + 'string'").getValue(String.class); // 'test string' 

// Subtraction 
int four = parser.parseExpression("1 - -3").getValue(Integer.class); // 4 
double d = parser.parseExpression("1000.00 - 1e4").getValue(Double.class); // -9000 

// Multiplication 
int six = parser.parseExpression("-2 * -3").getValue(Integer.class); // 6 

double twentyFour = parser.parseExpression("2.0 * 3e0 * 4").getValue(Double.class); // 24.0 
// Division 
int minusTwo = parser.parseExpression("6 / -3").getValue(Integer.class); // -2 
double one = parser.parseExpression("8.0 / 4e0 / 2").getValue(Double.class); // 1.0 

// Modulus 
int three = parser.parseExpression("7 % 4").getValue(Integer.class); // 3 
int one = parser.parseExpression("8 / 5 % 2").getValue(Integer.class); // 1 

// Operator precedence 
int minusTwentyOne = parser.parseExpression("1+2-3*8").getValue(Integer.class); // -21 


Comments

Popular posts from this blog

Today Walkin 14th-Sept

Hibernate Search - Elasticsearch with JSON manipulation

Spring Elasticsearch Operations