Class FlowControlUtils
- java.lang.Object
-
- io.servicetalk.concurrent.internal.FlowControlUtils
-
public final class FlowControlUtils extends java.lang.ObjectA set of utility methods for safe math operations to prevent overflow.
-
-
Method Summary
All Methods Static Methods Concrete Methods Modifier and Type Method Description static intaddWithOverflowProtection(int x, int y)Adds two positive ints and returnsInteger.MAX_VALUEif overflow occurs.static longaddWithOverflowProtection(long x, long y)Adds two positive longs and returnsLong.MAX_VALUEif overflow occurs.static longaddWithOverflowProtectionIfGtEqNegativeOne(long x, long y)static intaddWithOverflowProtectionIfNotNegative(int x, int y)Ifxis non-negative this method behaves the same asaddWithOverflowProtection(int, int).static longaddWithOverflowProtectionIfNotNegative(long x, long y)Ifxis non-negative this method behaves the same asaddWithOverflowProtection(long, long).static intaddWithOverflowProtectionIfPositive(int x, int y)Ifxis positive this method behaves the same asaddWithOverflowProtection(int, int).static longaddWithOverflowProtectionIfPositive(long x, long y)Ifxis positive this method behaves the same asaddWithOverflowProtection(long, long).static longaddWithUnderOverflowProtection(long x, long y)Add two longs and prevent [under|over]flow which is defined as if bothxandyhave the same sign but the result ofx + yhas a different sign.
-
-
-
Method Detail
-
addWithOverflowProtectionIfGtEqNegativeOne
public static long addWithOverflowProtectionIfGtEqNegativeOne(long x, long y)Ifxis>=-1this method behaves the same asaddWithOverflowProtection(long, long). Ifxis<-1thenxis returned.- Parameters:
x- first value (may be negative).y- second value (should be positive).- Returns:
- The result of
x+yorLong.MAX_VALUEif overflow occurs, orxifx<-1.
-
addWithOverflowProtectionIfNotNegative
public static long addWithOverflowProtectionIfNotNegative(long x, long y)Ifxis non-negative this method behaves the same asaddWithOverflowProtection(long, long). Ifxis negative thenxis returned.- Parameters:
x- first value (may be negative).y- second value (should be positive).- Returns:
- The result of
x+yorLong.MAX_VALUEif overflow occurs, orxifxis negative.
-
addWithOverflowProtectionIfNotNegative
public static int addWithOverflowProtectionIfNotNegative(int x, int y)Ifxis non-negative this method behaves the same asaddWithOverflowProtection(int, int). Ifxis negative thenxis returned.- Parameters:
x- first value (may be negative).y- second value (should be positive).- Returns:
- The result of
x+yorInteger.MAX_VALUEif overflow occurs, orxifxis negative.
-
addWithOverflowProtectionIfPositive
public static int addWithOverflowProtectionIfPositive(int x, int y)Ifxis positive this method behaves the same asaddWithOverflowProtection(int, int). Ifxis negative or zero thenxis returned.- Parameters:
x- first value (may be negative).y- second value (should be positive).- Returns:
- The result of
x+yorInteger.MAX_VALUEif overflow occurs, orxifxis negative.
-
addWithOverflowProtectionIfPositive
public static long addWithOverflowProtectionIfPositive(long x, long y)Ifxis positive this method behaves the same asaddWithOverflowProtection(long, long). Ifxis negative or zero thenxis returned.- Parameters:
x- first value (may be negative).y- second value (should be positive).- Returns:
- The result of
x+yorLong.MAX_VALUEif overflow occurs, orxifxis negative.
-
addWithOverflowProtection
public static long addWithOverflowProtection(long x, long y)Adds two positive longs and returnsLong.MAX_VALUEif overflow occurs.- Parameters:
x- first value (should be positive).y- second value (should be positive).- Returns:
- The result of
x+yorLong.MAX_VALUEif overflow occurs.
-
addWithOverflowProtection
public static int addWithOverflowProtection(int x, int y)Adds two positive ints and returnsInteger.MAX_VALUEif overflow occurs.- Parameters:
x- first value (should be positive).y- second value (should be positive).- Returns:
- The result of
x+yorInteger.MAX_VALUEif overflow occurs.
-
addWithUnderOverflowProtection
public static long addWithUnderOverflowProtection(long x, long y)Add two longs and prevent [under|over]flow which is defined as if bothxandyhave the same sign but the result ofx + yhas a different sign.- Parameters:
x- first value.y- second value.- Returns:
x + yif no overflowLong.MAX_VALUEif overflow in the positive directionLong.MIN_VALUEif otherwise in the negative direction
-
-