Skip to content

Commit

Permalink
Add long-to-boolean conversion methods to KiwiPrimitives
Browse files Browse the repository at this point in the history
* Add BooleanConversionOption enum, which specifies how numeric
  values (not just long) should be converted to boolean values
* Add overloaded booleanFromLong methods: one accepts a long argument
  and uses the ZERO_OR_ONE conversion option, the second accepts
  long and BooleanConversionOption arguments.

Closes #1053
  • Loading branch information
sleberknight committed Oct 7, 2023
1 parent a3e28e2 commit dbc0694
Show file tree
Hide file tree
Showing 2 changed files with 89 additions and 3 deletions.
47 changes: 47 additions & 0 deletions src/main/java/org/kiwiproject/base/KiwiPrimitives.java
Original file line number Diff line number Diff line change
Expand Up @@ -174,4 +174,51 @@ public static double tryParseDoubleOrThrow(CharSequence cs) {
throw new IllegalStateException(e);
}
}

/**
* Enum representing options for converting a numeric value into a boolean.
*/
public enum BooleanConversionOption {

/**
* Convert numeric values into boolean where one represents true and zero
* represents false. No other values are allowed.
*/
ZERO_OR_ONE,

/**
* Convert numeric values into boolean where any non-zero value
* represents true, and zero represents false.
*/
NON_ZERO_AS_TRUE
}

/**
* Converts the given long value to a boolean. The value must be zero or one.
*
* @param value the value to convert
* @return 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 #booleanFromLong(long, BooleanConversionOption)
*/
public static boolean booleanFromLong(long value) {
return booleanFromLong(value, BooleanConversionOption.ZERO_OR_ONE);
}

/**
* Converts the given long value to a boolean using the specified {@link BooleanConversionOption}.
*
* @param value the value to convert
* @param option how to convert the long value into a boolean
* @return true if the value is non-zero, otherwise false
*/
public static boolean booleanFromLong(long value, BooleanConversionOption option) {
return switch (option) {
case ZERO_OR_ONE -> {
checkArgument(value == 0 || value == 1, "value must be 0 or 1, but found %s", value);
yield value == 1;
}
case NON_ZERO_AS_TRUE -> value != 0;
};
}
}
45 changes: 42 additions & 3 deletions src/test/java/org/kiwiproject/base/KiwiPrimitivesTest.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package org.kiwiproject.base;

import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.core.api.Assertions.assertThatIllegalArgumentException;
import static org.assertj.core.api.Assertions.assertThatIllegalStateException;

import org.assertj.core.api.SoftAssertions;
Expand All @@ -10,6 +11,7 @@
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.CsvSource;
import org.junit.jupiter.params.provider.NullAndEmptySource;
import org.junit.jupiter.params.provider.ValueSource;

Expand Down Expand Up @@ -81,7 +83,7 @@ void shouldReturnEmptyOptional_WhenArgumentIsNotParseableToInt(String string) {
class TryParseIntOrThrow {

@Test
void shouldReturInt_WhenArgumentIsParseableToInt() {
void shouldReturnInt_WhenArgumentIsParseableToInt() {
assertThat(KiwiPrimitives.tryParseIntOrThrow("126")).isEqualTo(126);
}

Expand Down Expand Up @@ -149,7 +151,7 @@ void shouldReturnEmptyOptional_WhenArgumentIsNotParseableToLong(String string) {
class TryParseLongOrThrow {

@Test
void shouldReturLong_WhenArgumentIsParseableToLong() {
void shouldReturnLong_WhenArgumentIsParseableToLong() {
assertThat(KiwiPrimitives.tryParseLongOrThrow("1260000")).isEqualTo(1_260_000L);
}

Expand Down Expand Up @@ -217,7 +219,7 @@ void shouldReturnEmptyOptional_WhenArgumentIsNotParseableToDouble(String string)
class TryParseDoubleOrThrow {

@Test
void shouldReturDouble_WhenArgumentIsParseableToDouble() {
void shouldReturnDouble_WhenArgumentIsParseableToDouble() {
assertThat(KiwiPrimitives.tryParseDoubleOrThrow("1260000.042")).isEqualTo(1_260_000.042);
}

Expand All @@ -238,4 +240,41 @@ void shouldThrow_WhenArgumentIsNotParseableToDouble(String string) {
.withCauseExactlyInstanceOf(NumberFormatException.class);
}
}

@Nested
class BooleanFromLong {

@ParameterizedTest
@CsvSource(textBlock = """
1, true,
0, false
""")
void shouldConvertLong_WithZeroOrOneConversionOption(long value, boolean expectedResult) {
assertThat(KiwiPrimitives.booleanFromLong(value)).isEqualTo(expectedResult);
}

@ParameterizedTest
@ValueSource(longs = { -10, -5, -1, 2, 10, 42 })
void shouldThrowIllegalArgument_WithZeroOrOneConversionOption_WhenValueIsNotZeroOrOne(long value) {
assertThatIllegalArgumentException()
.isThrownBy(() -> KiwiPrimitives.booleanFromLong(value))
.withMessage("value must be 0 or 1, but found %d", value);
}

@ParameterizedTest
@CsvSource(textBlock = """
1, ZERO_OR_ONE, true,
1, NON_ZERO_AS_TRUE, true,
-1, NON_ZERO_AS_TRUE, true,
2, NON_ZERO_AS_TRUE, true,
1000, NON_ZERO_AS_TRUE, true,
0, ZERO_OR_ONE, false
0, NON_ZERO_AS_TRUE, false
""")
void shouldConvertLong_UsingBooleanConversionOption(long value,
KiwiPrimitives.BooleanConversionOption option,
boolean expectedResult) {
assertThat(KiwiPrimitives.booleanFromLong(value, option)).isEqualTo(expectedResult);
}
}
}

0 comments on commit dbc0694

Please sign in to comment.