public abstract class Arithmetic extends Object
This class serves as a bridge between the representation of Lua numbers as
Number
and the appropriate dispatch of arithmetic operations
via the methods of LuaMathOperators
.
There are two concrete instances of this class, FLOAT
(the float
arithmetic) and INTEGER
(the integer arithmetic). In order to obtain
the correct instance for the given numbers, use the static methods
of(Number, Number)
(for lookup based on two arguments, i.e. for binary
operations) or of(Number)
(for lookup based on a single argument, i.e.
for unary minus).
The arithmetic methods of this class yield boxed results.
Example: Given two objects a
and b
of type Number
,
use the Arithmetic
class to compute the value of the Lua expression
(a // b)
:
// Number a, b final Number result; Arithmetic m = Arithmetic.of(a, b); if (m != null) { result = m.idiv(a, b); } else { throw new IllegalStateException("a or b is nil"); }
Modifier and Type | Field and Description |
---|---|
static Arithmetic |
FLOAT
Float arithmetic.
|
static Arithmetic |
INTEGER
Integer arithmetic.
|
Modifier and Type | Method and Description |
---|---|
abstract Number |
add(Number a,
Number b)
Returns the boxed result of the Lua addition of the two numbers
a and b . |
abstract Double |
div(Number a,
Number b)
Returns the boxed result of the Lua float division of the two numbers
a and b . |
abstract Number |
idiv(Number a,
Number b)
Returns the boxed result of the Lua floor division of the two numbers
a and b . |
abstract Number |
mod(Number a,
Number b)
Returns the boxed result of the Lua modulo of the two numbers
a and b . |
abstract Number |
mul(Number a,
Number b)
Returns the boxed result of the Lua multiplication of the two numbers
a and b . |
static Arithmetic |
of(Number n)
Return an arithmetic based on the concrete type of the argument
n . |
static Arithmetic |
of(Number a,
Number b)
Return an arithmetic based on the concrete types of the arguments
a
and b . |
abstract Double |
pow(Number a,
Number b)
Returns the boxed result of the Lua exponentiation of the two numbers
a and b . |
abstract Number |
sub(Number a,
Number b)
Returns the boxed result of the Lua subtraction of the two numbers
a and b . |
abstract Number |
unm(Number n)
Returns the boxed result of the Lua arithmetic negation of the number
n . |
public static final Arithmetic INTEGER
Invokes arithmetic operations from LuaMathOperators
with all arguments
converted to Lua integers (i.e. long
s).
public static final Arithmetic FLOAT
Invokes arithmetic operations from LuaMathOperators
with all arguments
converted to Lua floats (i.e. double
s).
public abstract Number add(Number a, Number b)
a
and b
.a
- first addend, must not be null
b
- second addend, must not be null
(a + b)
NullPointerException
- if a
or b
is null
public abstract Number sub(Number a, Number b)
a
and b
.a
- the minuend, must not be null
b
- the subtrahend, must not be null
(a - b)
NullPointerException
- if a
or b
is null
public abstract Number mul(Number a, Number b)
a
and b
.a
- first factor, must not be null
b
- second factor, must not be null
(a * b)
NullPointerException
- if a
or b
is null
public abstract Double div(Number a, Number b)
a
and b
.a
- the dividend, must not be null
b
- the divisor, must not be null
(a / b)
NullPointerException
- if a
or b
is null
public abstract Number mod(Number a, Number b)
a
and b
.a
- the dividend, must not be null
b
- the divisor, must not be null
(a % b)
NullPointerException
- if a
or b
is null
public abstract Number idiv(Number a, Number b)
a
and b
.a
- the dividend, must not be null
b
- the divisor, must not be null
(a // b)
NullPointerException
- if a
or b
is null
public abstract Double pow(Number a, Number b)
a
and b
.a
- the base, must not be null
b
- the exponent, must not be null
(a ^ b)
NullPointerException
- if a
or b
is null
public abstract Number unm(Number n)
n
.n
- the operand, must not be null
(-n)
NullPointerException
- if n
is null
public static Arithmetic of(Number a, Number b)
public static Arithmetic of(Number n)
Copyright © 2016. All rights reserved.