Comparison operators compare identifiers to literals in a boolean
expression that evaluates to either TRUE
or
FALSE
. Comparison operations can be combined into
more complex expressions using the logical operators
AND
and OR
. Expressions are
evaluated from left to right:
Age < 30AND
Weight >= 100.00OR
LName = 'Smith'
In this example, the expression would be evaluated as if it had parentheses placed as follows (parentheses can be used to group expressions and can change the precedence of evaluation):
(
Age < 30 AND Weight >= 100.00)
OR(
LName = 'Smith')
Either the LName
must be equal to
'Smith'
or the LName
can be any
value
as long as the Age
is
less than 30
and the Weight
is
greater than or equal to 100
. Evaluating these
kinds of expressions should be second nature for most programmers.
The following message selector uses three of the six algebraic
comparison operators, which are
=
,
>
,
>=
,
<
,
<=
, and
<>
(not equal):
Age<
30 AND Weight>=
100.00 OR LName=
'Smith'
These algebraic comparison operators can be used on any of the
primitive property types except for boolean
. The
boolean
and String
property
types are restricted to the =
or the
<>
algebraic operators.
String types can be compared using the
LIKE
comparison operator. For example:
Age < 30 AND Weight >= 100.00 OR LName LIKE
'Sm%th'
The LIKE
comparison operator attempts to match
each character in the literal with characters of the property value.
Two special wildcard characters, underscore (_
)
and percent (%
), can be used with the
LIKE
comparison. The underscore stands for any
single character. The percent symbol stands for any sequence of
characters. All other characters stand for themselves and are case
sensitive. Table 4.1 provides some examples of
successful and unsuccessful comparisons using the
LIKE
operator.
The BETWEEN
operator can be used to specify a
range (inclusive). For example:
Age BETWEEN 20 and 30
This expression is the same as:
(Age >= 20) AND (Age <=30)
The IN
operator can be used to specify
membership in a set:
LName IN ('Smith', 'Jones', 'Brown')
This expression is the same as:
(LName = 'Smith') OR (LName = 'Jones') OR (LName = 'Brown')
The NOT
logical operator can be used in
combination with the LIKE
,
BETWEEN
, IN
, and IS
NULL
(discussed later) operators to reverse their
evaluation. If the expression would have evaluated to
TRUE
, it becomes FALSE,
and
vice versa.
When no property or header exists to match an identifier in a message
selector, the value of the identifier is assigned a
null
value. Nonexistent properties
evaluating to null
present some problems with
message selectors. In some cases, the null
value
of the property cannot be evaluated in a conditional expression. The
result is an unknown evaluation—a nice way
of saying the result is not predictable across JMS providers. If, for
example, a particular message contains the Age
= 20
and Weight
=
90.00
properties but does not have an
LName
property, then the message selector
following would evaluate as shown:
Age < 30 AND Weight >= 100.00 OR LName = 'Smith' ____ _____ ____ _ TRUE AND FALSE OR UNKNOWN
The results of evaluating unknown
expressions with
logical
operators (AND
,
OR
,
NOT
) are shown in Table 4.2, Table 4.3, and Table 4.4.
To avoid problems, the IS
NULL
or IS NOT NULL
comparison can be used to check for the existence of a property:
Age IS NULL AND Weight IS NOT NULL
The previous expression selects messages that do not have an
Age
property but do have a
Weight
property.