T
- the type of values comparable in the orderingpublic abstract class Ordering<T> extends Object implements Comparator<T>
T
).
In Lua, only strings and numbers have such an ordering. This class serves the
purpose of a bridge between the concrete representation of Lua numbers
(as Number
) and the raw comparison operations provided by
LuaMathOperators
, and the concrete representation of Lua strings
(as String
) and the comparison operations defined on them.
Consequently, there are two concrete implementations of this class:
NUMERIC
for the numeric ordering and STRING
for the string
ordering. These instances may be used directly for comparing objects of known,
conforming types; for unknown objects, the method of(Object, Object)
returns an ordering that accepts Object
, and uses one of
the two ordering instances, or null
if the arguments
are not directly comparable in Lua.
The comparison methods of this class return unboxed booleans.
This class implements the Comparator
interface by imposing a total
order on the accepted values. For numbers, this total ordering is different
from the one imposed by this class. See the documentation of Ordering.NumericOrdering
for more details.
Example: Given two objects a
, and b
, attempt to
evaluate the Lua expression (a <= b)
:
// Object a, b final boolean result; Ordering<Object> cmp = Ordering.of(a, b); if (cmp != null) { // a and b are comparable in cmp result = cmp.le(a, b); } else { throw new RuntimeException("a and b not comparable"); }
Modifier and Type | Class and Description |
---|---|
static class |
Ordering.NumericOrdering
Numeric ordering.
|
static class |
Ordering.StringOrdering
String ordering.
|
Modifier and Type | Field and Description |
---|---|
static Ordering.NumericOrdering |
NUMERIC
A static instance of the numeric ordering.
|
static Ordering.StringOrdering |
STRING
A static instance of the string ordering.
|
Modifier and Type | Method and Description |
---|---|
abstract boolean |
eq(T a,
T b)
Returns
true if a is equal to b in this ordering. |
static boolean |
isRawEqual(Object a,
Object b)
Returns
true iff the object a is raw-equal to b following
the Lua equality rules. |
abstract boolean |
le(T a,
T b)
Returns
true if a is lesser than or equal to b in this ordering. |
abstract boolean |
lt(T a,
T b)
Returns
true if a is lesser than b in this ordering. |
static Ordering<Object> |
of(Object a,
Object b)
Based on the actual types of the arguments
a and b , returns
the ordering in which a and b can be compared, or null
if they are not comparable. |
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
compare, comparing, comparing, comparingDouble, comparingInt, comparingLong, equals, naturalOrder, nullsFirst, nullsLast, reversed, reverseOrder, thenComparing, thenComparing, thenComparing, thenComparingDouble, thenComparingInt, thenComparingLong
public static final Ordering.NumericOrdering NUMERIC
public static final Ordering.StringOrdering STRING
public abstract boolean eq(T a, T b)
true
if a
is equal to b
in this ordering.a
- first argument, must not be null
b
- second argument, must not be null
true
iff a
is equal to b
in this orderingNullPointerException
- if a
or b
is null
public abstract boolean lt(T a, T b)
true
if a
is lesser than b
in this ordering.a
- first argument, must not be null
b
- second argument, must not be null
true
iff a
is lesser than b
in this orderingNullPointerException
- if a
or b
is null
public abstract boolean le(T a, T b)
true
if a
is lesser than or equal to b
in this ordering.a
- first argument, must not be null
b
- second argument, must not be null
true
iff a
is lesser than or equal to equal to b
in this orderingNullPointerException
- if a
or b
is null
public static boolean isRawEqual(Object a, Object b)
true
iff the object a
is raw-equal to b
following
the Lua equality rules.
Excerpt from the Lua Reference Manual (ยง3.4.4):
Equality (==) first compares the type of its operands. If the types are different, then the result is false. Otherwise, the values of the operands are compared. Strings are compared in the obvious way. Numbers are equal if they denote the same mathematical value.
Tables, userdata, and threads are compared by reference: two objects are considered equal only if they are the same object. Every time you create a new object (a table, userdata, or thread), this new object is different from any previously existing object. Closures with the same reference are always equal. Closures with any detectable difference (different behavior, different definition) are always different.
Note: Rembulan uses Object.equals(Object)
to compare all non-nil,
non-string, and non-numeric values for equality, effectively shifting the
responsibility of adhering to the rules of Lua raw-equality for tables, userdata
and threads to their implementations.
a
- an object, may be null
b
- another object, may be null
true
iff a
is raw-equal to b
public static Ordering<Object> of(Object a, Object b)
a
and b
, returns
the ordering in which a
and b
can be compared, or null
if they are not comparable.
More specifically, if a
and b
are both numbers, returns
an ordering that uses (but is distinct from) NUMERIC
; if a
and
b
are both strings, returns an ordering that uses (but is distinct from)
STRING
; otherwise, returns null
.
Note that when the result is non-null
, it is guaranteed that
1) neither a
nor b
is null
; and 2)
both a
and b
are of types accepted by the underlying ordering.
Caution must be observed when using the ordering with another object c
(i.e., other than a
or b
): the returned ordering will throw
a ClassCastException
if c
is of an incompatible type, or
a NullPointerException
if c
is null
.
Copyright © 2016. All rights reserved.