Class FlowControlUtils
java.lang.Object
io.servicetalk.concurrent.internal.FlowControlUtils
A set of utility methods for safe math operations to prevent overflow.
-
Method Summary
Modifier and TypeMethodDescriptionstatic int
addWithOverflowProtection
(int x, int y) Adds two positive ints and returnsInteger.MAX_VALUE
if overflow occurs.static long
addWithOverflowProtection
(long x, long y) Adds two positive longs and returnsLong.MAX_VALUE
if overflow occurs.static long
addWithOverflowProtectionIfGtEqNegativeOne
(long x, long y) static int
addWithOverflowProtectionIfNotNegative
(int x, int y) Ifx
is non-negative this method behaves the same asaddWithOverflowProtection(int, int)
.static long
addWithOverflowProtectionIfNotNegative
(long x, long y) Ifx
is non-negative this method behaves the same asaddWithOverflowProtection(long, long)
.static int
addWithOverflowProtectionIfPositive
(int x, int y) Ifx
is positive this method behaves the same asaddWithOverflowProtection(int, int)
.static long
addWithOverflowProtectionIfPositive
(long x, long y) Ifx
is positive this method behaves the same asaddWithOverflowProtection(long, long)
.static int
addWithUnderOverflowProtection
(int x, int y) Add two longs and prevent [under|over]flow which is defined as if bothx
andy
have the same sign but the result ofx + y
has a different sign.static long
addWithUnderOverflowProtection
(long x, long y) Add two longs and prevent [under|over]flow which is defined as if bothx
andy
have the same sign but the result ofx + y
has a different sign.
-
Method Details
-
addWithOverflowProtectionIfGtEqNegativeOne
public static long addWithOverflowProtectionIfGtEqNegativeOne(long x, long y) Ifx
is>=-1
this method behaves the same asaddWithOverflowProtection(long, long)
. Ifx
is<-1
thenx
is returned.- Parameters:
x
- first value (may be negative).y
- second value (should be positive).- Returns:
- The result of
x+y
orLong.MAX_VALUE
if overflow occurs, orx
ifx<-1
.
-
addWithOverflowProtectionIfNotNegative
public static long addWithOverflowProtectionIfNotNegative(long x, long y) Ifx
is non-negative this method behaves the same asaddWithOverflowProtection(long, long)
. Ifx
is negative thenx
is returned.- Parameters:
x
- first value (may be negative).y
- second value (should be positive).- Returns:
- The result of
x+y
orLong.MAX_VALUE
if overflow occurs, orx
ifx
is negative.
-
addWithOverflowProtectionIfNotNegative
public static int addWithOverflowProtectionIfNotNegative(int x, int y) Ifx
is non-negative this method behaves the same asaddWithOverflowProtection(int, int)
. Ifx
is negative thenx
is returned.- Parameters:
x
- first value (may be negative).y
- second value (should be positive).- Returns:
- The result of
x+y
orInteger.MAX_VALUE
if overflow occurs, orx
ifx
is negative.
-
addWithOverflowProtectionIfPositive
public static int addWithOverflowProtectionIfPositive(int x, int y) Ifx
is positive this method behaves the same asaddWithOverflowProtection(int, int)
. Ifx
is negative or zero thenx
is returned.- Parameters:
x
- first value (may be negative).y
- second value (should be positive).- Returns:
- The result of
x+y
orInteger.MAX_VALUE
if overflow occurs, orx
ifx
is negative.
-
addWithOverflowProtectionIfPositive
public static long addWithOverflowProtectionIfPositive(long x, long y) Ifx
is positive this method behaves the same asaddWithOverflowProtection(long, long)
. Ifx
is negative or zero thenx
is returned.- Parameters:
x
- first value (may be negative).y
- second value (should be positive).- Returns:
- The result of
x+y
orLong.MAX_VALUE
if overflow occurs, orx
ifx
is negative.
-
addWithOverflowProtection
public static long addWithOverflowProtection(long x, long y) Adds two positive longs and returnsLong.MAX_VALUE
if overflow occurs.- Parameters:
x
- first value (should be positive).y
- second value (should be positive).- Returns:
- The result of
x+y
orLong.MAX_VALUE
if overflow occurs.
-
addWithOverflowProtection
public static int addWithOverflowProtection(int x, int y) Adds two positive ints and returnsInteger.MAX_VALUE
if overflow occurs.- Parameters:
x
- first value (should be positive).y
- second value (should be positive).- Returns:
- The result of
x+y
orInteger.MAX_VALUE
if overflow occurs.
-
addWithUnderOverflowProtection
public static long addWithUnderOverflowProtection(long x, long y) Add two longs and prevent [under|over]flow which is defined as if bothx
andy
have the same sign but the result ofx + y
has a different sign.- Parameters:
x
- first value.y
- second value.- Returns:
x + y
if no overflowLong.MAX_VALUE
if overflow in the positive directionLong.MIN_VALUE
if otherwise in the negative direction
-
addWithUnderOverflowProtection
public static int addWithUnderOverflowProtection(int x, int y) Add two longs and prevent [under|over]flow which is defined as if bothx
andy
have the same sign but the result ofx + y
has a different sign.- Parameters:
x
- first value.y
- second value.- Returns:
x + y
if no overflowInteger.MAX_VALUE
if overflow in the positive directionInteger.MIN_VALUE
if otherwise in the negative direction
-