Operators in SPEL

We can use many operators in SpEL such as :

  1. Mathematical
  2. relational
  3. logical etc

There are given a lot of examples of using different operators in SpEL.


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
	

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(int)").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.

	
// -- AND --

// evaluates to false
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);

// -- OR --

// evaluates to true
boolean trueValue = parser.parseExpression("true or false").getValue(Boolean.class);

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

// -- NOT --

// evaluates to false
boolean falseValue = parser.parseExpression("!true").getValue(Boolean.class);

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

Examples of using operators in SPEL


import org.springframework.expression.ExpressionParser;
import org.springframework.expression.spel.standard.SpelExpressionParser;


public class Test {
public static void main(String[] args) {
ExpressionParser parser = new SpelExpressionParser();

//arithmetic operator
System.out.println(parser.parseExpression("'Welcome SPEL'+'!'").getValue());
System.out.println(parser.parseExpression("10 * 10/2").getValue());
System.out.println(parser.parseExpression("'Today is: '+ new java.util.Date()").getValue());

//logical operator
System.out.println(parser.parseExpression("true and true").getValue());

//Relational operator
System.out.println(parser.parseExpression("'Mithilesh'.length()==5").getValue());
}
}