표준 연산자 표기법을 사용하여 관계 연산자 (같음, 같지 않음, 같지 않음, 작음 이하, 더 크고 같음)가 지원됩니다. 다음 목록은 연산자의 몇 가지 예를 보여줍니다.
// 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);
보다 크거나 작은 null에 대한 비교는 단순한 규칙 을 따른다 : null아무것도 아닌 것으로 취급된다 (그것은 0이 아니다). 결과적으로 다른 값은 항상 null 보다 크고 ( X > null항상 true) 아무것도없는 것보다 적은 값은 없습니다. ( X < null항상 false).
대신 숫자 비교를 선호하는 경우 0 에 대한 비교를 위해 숫자 기반 null비교를 사용하지 마십시오. (예 : X > 0또는 X < 0).
표준 관계 연산자 외에도 SpEL은 instanceof와 정규 표현식 기반 matches연산자를 지원합니다 . 다음 목록은 두 가지 예를 보여줍니다.
Java에서 super타입에 대한 연산을 위해 instanceof를 표현식으로 사용할 수 있다.
reason - 위의 예제에서는 double 타입의 프리미티브값을 넘겨 Double 타입 파라미터를 받는 메소드를 호출하였습니다. 이러한 경우 double타입의 프리미트브 값을 갖는 Double 객체가 자동으로 생성되어 메소드에 넘겨집니다
각 기호 연산자는 순전히 알파벳순으로 지정할 수도 있습니다. 이렇게하면식이 포함 된 문서 형식 (예 : XML 문서)에 사용 된 기호가 특별한 의미를 갖는 문제를 피할 수 있습니다. 텍스트와 동등한 내용은 다음과 같습니다.
lt( <)
gt( >)
le( <=)
ge( >=)
eq( ==)
ne( !=)
div( /)
mod( %)
not( !).
모든 텍스트 연산자는 대소 문자를 구분하지 않습니다.
논리 연산자
SpEL은 다음과 같은 논리 연산자를 지원합니다 :
and
or
not
다음 예에서는 논리 연산자를 사용하는 방법을 보여줍니다.
// -- 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);
수학 연산자
더하기 (+) 연산자를 숫자, 날짜, 문자열에 사용할 수 있다.
빼기(-) 연산자를 숫자, 날짜에 사용할 수 있다.
곱하기(*), 나누기(/), 나머지(%)에 대한 연산은 숫자에만 사용 할 수 있다.
연산자 우선 순위 법칙이 적용된다.
숫자와 문자열 모두에서 더하기 연산자를 사용할 수 있습니다. 빼기, 곱하기 및 나누기 연산자는 숫자에만 사용할 수 있습니다. 모듈러스 (%) 및 지수 출력 (^) 연산자를 사용할 수도 있습니다. 표준 운영자 우선 순위가 적용됩니다. 다음 예제는 사용중인 수학 연산자를 보여줍니다.
// 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
배정 연산자
특성을 설정하려면 대입 연산자 ( =)를 사용하십시오 . 이것은 일반적으로 setValue호출 내에서 이루어 지지만 getValue호출 내에서 수행 될 수도 있습니다. 다음 목록은 할당 연산자를 사용하는 두 가지 방법을 보여줍니다.