Package org.kiwiproject.base
Class KiwiPrimitives
java.lang.Object
org.kiwiproject.base.KiwiPrimitives
Static utilities that operate on primitive values, and are not already provided by classes in Guava's
com.google.common.primitives package.-
Nested Class Summary
Nested ClassesModifier and TypeClassDescriptionstatic enumEnum representing options for converting a numeric value into a boolean. -
Method Summary
Modifier and TypeMethodDescriptionstatic booleanbooleanFromInt(int value) Converts the given int value to a boolean.static booleanbooleanFromInt(int value, KiwiPrimitives.BooleanConversionOption option) Converts the given int value to a boolean using the specifiedKiwiPrimitives.BooleanConversionOption.static booleanbooleanFromLong(long value) Converts the given long value to a boolean.static booleanbooleanFromLong(long value, KiwiPrimitives.BooleanConversionOption option) Converts the given long value to a boolean using the specifiedKiwiPrimitives.BooleanConversionOption.static intfirstNonZero(int first, int second) Returns the first non-zero argument, otherwise throwsIllegalArgumentExceptionif both arguments are zero.static longfirstNonZero(long first, long second) Returns the first non-zero argument, otherwise throwsIllegalArgumentExceptionif both arguments are zero.static OptionalDoubleAttempt to parse the givenCharSequenceto andouble.static DoubleAttempt to parse the givenCharSequenceto anDouble.static doubleAttempt to parse the givenCharSequenceto andouble.static OptionalIntAttempt to parse the givenCharSequenceto anint.static IntegerAttempt to parse the givenCharSequenceto anInteger.static intAttempt to parse the givenCharSequenceto anint.static OptionalLongAttempt to parse the givenCharSequenceto anlong.static LongAttempt to parse the givenCharSequenceto anLong.static longAttempt to parse the givenCharSequenceto anlong.
-
Method Details
-
firstNonZero
public static int firstNonZero(int first, int second) Returns the first non-zero argument, otherwise throwsIllegalArgumentExceptionif both arguments are zero.- Parameters:
first- the first int to checksecond- 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 throwsIllegalArgumentExceptionif both arguments are zero.- Parameters:
first- the first int to checksecond- the second int to check- Returns:
- the first non-zero value
- Throws:
IllegalArgumentException- if both arguments are zero
-
tryParseIntOrNull
Attempt to parse the givenCharSequenceto anInteger.- Parameters:
cs- the value to parse- Returns:
- the value as an Integer or
nullif the value cannot be parsed
-
tryParseInt
Attempt to parse the givenCharSequenceto anint.- Parameters:
cs- the value to parse- Returns:
- an
OptionalIntthat will contain the parsed value or will be empty if the input cannot be parsed
-
tryParseIntOrThrow
Attempt to parse the givenCharSequenceto anint.- Parameters:
cs- the value to parse- Returns:
- the parsed
intvalue if successful; otherwise if it cannot be parsed, this method always throws an exception - Throws:
IllegalStateException- if the value cannot be parsed
-
tryParseLongOrNull
Attempt to parse the givenCharSequenceto anLong.- Parameters:
cs- the value to parse- Returns:
- the value as a Long or
nullif the value cannot be parsed
-
tryParseLong
Attempt to parse the givenCharSequenceto anlong.- Parameters:
cs- the value to parse- Returns:
- an
OptionalLongthat will contain the parsed value or will be empty if the input cannot be parsed
-
tryParseLongOrThrow
Attempt to parse the givenCharSequenceto anlong.- Parameters:
cs- the value to parse- Returns:
- the parsed
longvalue if successful; otherwise if it cannot be parsed this method always throws an exception - Throws:
IllegalStateException- if the value cannot be parsed
-
tryParseDoubleOrNull
Attempt to parse the givenCharSequenceto anDouble.- Parameters:
cs- the value to parse- Returns:
- the value as a Double or
nullif the value cannot be parsed
-
tryParseDouble
Attempt to parse the givenCharSequenceto andouble.- Parameters:
cs- the value to parse- Returns:
- an
OptionalDoublethat will contain the parsed value or will be empty if the input cannot be parsed
-
tryParseDoubleOrThrow
Attempt to parse the givenCharSequenceto andouble.- Parameters:
cs- the value to parse- Returns:
- the parsed
doublevalue 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
Converts the given long value to a boolean using the specifiedKiwiPrimitives.BooleanConversionOption.- Parameters:
value- the value to convertoption- 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
Converts the given int value to a boolean using the specifiedKiwiPrimitives.BooleanConversionOption.- Parameters:
value- the value to convertoption- how to convert the int value into a boolean- Returns:
- true if the value is non-zero, otherwise false
-