Class KiwiPrimitives

java.lang.Object
org.kiwiproject.base.KiwiPrimitives

public final class KiwiPrimitives extends Object
Static utilities that operate on primitive values, and are not already provided by classes in Guava's com.google.common.primitives package.
  • Method Details

    • firstNonZero

      public static int firstNonZero(int first, int second)
      Returns the first non-zero argument, otherwise throws IllegalArgumentException if both arguments are zero.
      Parameters:
      first - the first int to check
      second - the second int to check
      Returns:
      the first non-zero value
      Throws:
      IllegalArgumentException - if both arguments are zero
    • firstNonZero

      public static long firstNonZero(long first, long second)
      Returns the first non-zero argument, otherwise throws IllegalArgumentException if both arguments are zero.
      Parameters:
      first - the first int to check
      second - the second int to check
      Returns:
      the first non-zero value
      Throws:
      IllegalArgumentException - if both arguments are zero
    • tryParseIntOrNull

      public static Integer tryParseIntOrNull(CharSequence cs)
      Attempt to parse the given CharSequence to an Integer.
      Parameters:
      cs - the value to parse
      Returns:
      the value as an Integer or null if the value cannot be parsed
    • tryParseInt

      public static OptionalInt tryParseInt(CharSequence cs)
      Attempt to parse the given CharSequence to an int.
      Parameters:
      cs - the value to parse
      Returns:
      an OptionalInt that will contain the parsed value or will be empty if the input cannot be parsed
    • tryParseIntOrThrow

      public static int tryParseIntOrThrow(CharSequence cs)
      Attempt to parse the given CharSequence to an int.
      Parameters:
      cs - the value to parse
      Returns:
      the parsed int value if successful; otherwise if it cannot be parsed, this method always throws an exception
      Throws:
      IllegalStateException - if the value cannot be parsed
    • tryParseLongOrNull

      public static Long tryParseLongOrNull(CharSequence cs)
      Attempt to parse the given CharSequence to an Long.
      Parameters:
      cs - the value to parse
      Returns:
      the value as a Long or null if the value cannot be parsed
    • tryParseLong

      public static OptionalLong tryParseLong(CharSequence cs)
      Attempt to parse the given CharSequence to an long.
      Parameters:
      cs - the value to parse
      Returns:
      an OptionalLong that will contain the parsed value or will be empty if the input cannot be parsed
    • tryParseLongOrThrow

      public static long tryParseLongOrThrow(CharSequence cs)
      Attempt to parse the given CharSequence to an long.
      Parameters:
      cs - the value to parse
      Returns:
      the parsed long value if successful; otherwise if it cannot be parsed this method always throws an exception
      Throws:
      IllegalStateException - if the value cannot be parsed
    • tryParseDoubleOrNull

      public static Double tryParseDoubleOrNull(CharSequence cs)
      Attempt to parse the given CharSequence to an Double.
      Parameters:
      cs - the value to parse
      Returns:
      the value as a Double or null if the value cannot be parsed
    • tryParseDouble

      public static OptionalDouble tryParseDouble(CharSequence cs)
      Attempt to parse the given CharSequence to an double.
      Parameters:
      cs - the value to parse
      Returns:
      an OptionalDouble that will contain the parsed value or will be empty if the input cannot be parsed
    • tryParseDoubleOrThrow

      public static double tryParseDoubleOrThrow(CharSequence cs)
      Attempt to parse the given CharSequence to an double.
      Parameters:
      cs - the value to parse
      Returns:
      the parsed double value if successful; otherwise if it cannot be parsed this method always throws an exception
      Throws:
      IllegalStateException - if the value cannot be parsed
    • booleanFromLong

      public static boolean booleanFromLong(long value)
      Converts the given long value to a boolean. The value must be zero or one.
      Parameters:
      value - the value to convert
      Returns:
      true if the value is one, or false if zero
      Throws:
      IllegalArgumentException - if the value in the column is not zero, one, or NULL
      See Also:
    • booleanFromLong

      public static boolean booleanFromLong(long value, KiwiPrimitives.BooleanConversionOption option)
      Converts the given long value to a boolean using the specified KiwiPrimitives.BooleanConversionOption.
      Parameters:
      value - the value to convert
      option - how to convert the long value into a boolean
      Returns:
      true if the value is non-zero, otherwise false
    • booleanFromInt

      public static boolean booleanFromInt(int value)
      Converts the given int value to a boolean. The value must be zero or one.
      Parameters:
      value - the value to convert
      Returns:
      true if the value is one, or false if zero
      Throws:
      IllegalArgumentException - if the value in the column is not zero, one, or NULL
      See Also:
    • booleanFromInt

      public static boolean booleanFromInt(int value, KiwiPrimitives.BooleanConversionOption option)
      Converts the given int value to a boolean using the specified KiwiPrimitives.BooleanConversionOption.
      Parameters:
      value - the value to convert
      option - how to convert the int value into a boolean
      Returns:
      true if the value is non-zero, otherwise false