Class Jdk8Methods
The contents of this class replace functionality available in JDK 8.
Specification for implementors
This is a thread-safe utility class. All returned classes are immutable and thread-safe.-
Method Summary
Modifier and TypeMethodDescriptionstatic int
floorDiv(int a, int b)
Returns the floor division.static long
floorDiv(long a, long b)
Returns the floor division.static int
floorMod(int a, int b)
Returns the floor modulus.static int
floorMod(long a, int b)
Returns the floor modulus.static long
floorMod(long a, long b)
Returns the floor modulus.static int
safeAdd(int a, int b)
Safely adds two int values.static long
safeAdd(long a, long b)
Safely adds two long values.static int
safeMultiply(int a, int b)
Safely multiply one int by another.static long
safeMultiply(long a, int b)
Safely multiply a long by an int.static long
safeMultiply(long a, long b)
Multiply two values throwing an exception if overflow occurs.static int
safeSubtract(int a, int b)
Safely subtracts one int from another.static long
safeSubtract(long a, long b)
Safely subtracts one long from another.static int
safeToInt(long value)
Safely convert a long to an int.
-
Method Details
-
safeAdd
public static int safeAdd(int a, int b)Safely adds two int values.- Parameters:
a
- the first valueb
- the second value- Returns:
- the result
- Throws:
ArithmeticException
- if the result overflows an int
-
safeAdd
public static long safeAdd(long a, long b)Safely adds two long values.- Parameters:
a
- the first valueb
- the second value- Returns:
- the result
- Throws:
ArithmeticException
- if the result overflows a long
-
safeSubtract
public static int safeSubtract(int a, int b)Safely subtracts one int from another.- Parameters:
a
- the first valueb
- the second value to subtract from the first- Returns:
- the result
- Throws:
ArithmeticException
- if the result overflows an int
-
safeSubtract
public static long safeSubtract(long a, long b)Safely subtracts one long from another.- Parameters:
a
- the first valueb
- the second value to subtract from the first- Returns:
- the result
- Throws:
ArithmeticException
- if the result overflows a long
-
safeMultiply
public static int safeMultiply(int a, int b)Safely multiply one int by another.- Parameters:
a
- the first valueb
- the second value- Returns:
- the result
- Throws:
ArithmeticException
- if the result overflows an int
-
safeMultiply
public static long safeMultiply(long a, int b)Safely multiply a long by an int.- Parameters:
a
- the first valueb
- the second value- Returns:
- the new total
- Throws:
ArithmeticException
- if the result overflows a long
-
safeMultiply
public static long safeMultiply(long a, long b)Multiply two values throwing an exception if overflow occurs.- Parameters:
a
- the first valueb
- the second value- Returns:
- the new total
- Throws:
ArithmeticException
- if the result overflows a long
-
safeToInt
public static int safeToInt(long value)Safely convert a long to an int.- Parameters:
value
- the value to convert- Returns:
- the int value
- Throws:
ArithmeticException
- if the result overflows an int
-
floorDiv
public static long floorDiv(long a, long b)Returns the floor division.This returns
0
forfloorDiv(0, 4)
.
This returns-1
forfloorDiv(-1, 4)
.
This returns-1
forfloorDiv(-2, 4)
.
This returns-1
forfloorDiv(-3, 4)
.
This returns-1
forfloorDiv(-4, 4)
.
This returns-2
forfloorDiv(-5, 4)
.- Parameters:
a
- the dividendb
- the divisor- Returns:
- the floor division
-
floorMod
public static long floorMod(long a, long b)Returns the floor modulus.This returns
0
forfloorMod(0, 4)
.
This returns1
forfloorMod(-1, 4)
.
This returns2
forfloorMod(-2, 4)
.
This returns3
forfloorMod(-3, 4)
.
This returns0
forfloorMod(-4, 4)
.- Parameters:
a
- the dividendb
- the divisor- Returns:
- the floor modulus (positive)
-
floorMod
public static int floorMod(long a, int b)Returns the floor modulus.This returns
0
forfloorMod(0, 4)
.
This returns3
forfloorMod(-1, 4)
.
This returns2
forfloorMod(-2, 4)
.
This returns1
forfloorMod(-3, 4)
.
This returns0
forfloorMod(-4, 4)
.
This returns3
forfloorMod(-5, 4)
.- Parameters:
a
- the dividendb
- the divisor- Returns:
- the floor modulus (positive)
-
floorDiv
public static int floorDiv(int a, int b)Returns the floor division.This returns
1
forfloorDiv(3, 3)
.
This returns0
forfloorDiv(2, 3)
.
This returns0
forfloorDiv(1, 3)
.
This returns0
forfloorDiv(0, 3)
.
This returns-1
forfloorDiv(-1, 3)
.
This returns-1
forfloorDiv(-2, 3)
.
This returns-1
forfloorDiv(-3, 3)
.
This returns-2
forfloorDiv(-4, 3)
.- Parameters:
a
- the dividendb
- the divisor- Returns:
- the floor division
-
floorMod
public static int floorMod(int a, int b)Returns the floor modulus.This returns
0
forfloorMod(3, 3)
.
This returns2
forfloorMod(2, 3)
.
This returns1
forfloorMod(1, 3)
.
This returns0
forfloorMod(0, 3)
.
This returns2
forfloorMod(-1, 3)
.
This returns1
forfloorMod(-2, 3)
.
This returns0
forfloorMod(-3, 3)
.
This returns2
forfloorMod(-4, 3)
.- Parameters:
a
- the dividendb
- the divisor- Returns:
- the floor modulus (positive)
-