Class FlowControlUtils

java.lang.Object
io.servicetalk.concurrent.internal.FlowControlUtils

public final class FlowControlUtils extends Object
A set of utility methods for safe math operations to prevent overflow.
  • Method Details

    • addWithOverflowProtectionIfGtEqNegativeOne

      public static long addWithOverflowProtectionIfGtEqNegativeOne(long x, long y)
      If x is >=-1 this method behaves the same as addWithOverflowProtection(long, long). If x is <-1 then x is returned.
      Parameters:
      x - first value (may be negative).
      y - second value (should be positive).
      Returns:
      The result of x+y or Long.MAX_VALUE if overflow occurs, or x if x<-1.
    • addWithOverflowProtectionIfNotNegative

      public static long addWithOverflowProtectionIfNotNegative(long x, long y)
      If x is non-negative this method behaves the same as addWithOverflowProtection(long, long). If x is negative then x is returned.
      Parameters:
      x - first value (may be negative).
      y - second value (should be positive).
      Returns:
      The result of x+y or Long.MAX_VALUE if overflow occurs, or x if x is negative.
    • addWithOverflowProtectionIfNotNegative

      public static int addWithOverflowProtectionIfNotNegative(int x, int y)
      If x is non-negative this method behaves the same as addWithOverflowProtection(int, int). If x is negative then x is returned.
      Parameters:
      x - first value (may be negative).
      y - second value (should be positive).
      Returns:
      The result of x+y or Integer.MAX_VALUE if overflow occurs, or x if x is negative.
    • addWithOverflowProtectionIfPositive

      public static int addWithOverflowProtectionIfPositive(int x, int y)
      If x is positive this method behaves the same as addWithOverflowProtection(int, int). If x is negative or zero then x is returned.
      Parameters:
      x - first value (may be negative).
      y - second value (should be positive).
      Returns:
      The result of x+y or Integer.MAX_VALUE if overflow occurs, or x if x is negative.
    • addWithOverflowProtectionIfPositive

      public static long addWithOverflowProtectionIfPositive(long x, long y)
      If x is positive this method behaves the same as addWithOverflowProtection(long, long). If x is negative or zero then x is returned.
      Parameters:
      x - first value (may be negative).
      y - second value (should be positive).
      Returns:
      The result of x+y or Long.MAX_VALUE if overflow occurs, or x if x is negative.
    • addWithOverflowProtection

      public static long addWithOverflowProtection(long x, long y)
      Adds two positive longs and returns Long.MAX_VALUE if overflow occurs.
      Parameters:
      x - first value (should be positive).
      y - second value (should be positive).
      Returns:
      The result of x+y or Long.MAX_VALUE if overflow occurs.
    • addWithOverflowProtection

      public static int addWithOverflowProtection(int x, int y)
      Adds two positive ints and returns Integer.MAX_VALUE if overflow occurs.
      Parameters:
      x - first value (should be positive).
      y - second value (should be positive).
      Returns:
      The result of x+y or Integer.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 both x and y have the same sign but the result of x + y has a different sign.
      Parameters:
      x - first value.
      y - second value.
      Returns:
    • addWithUnderOverflowProtection

      public static int addWithUnderOverflowProtection(int x, int y)
      Add two longs and prevent [under|over]flow which is defined as if both x and y have the same sign but the result of x + y has a different sign.
      Parameters:
      x - first value.
      y - second value.
      Returns: