From ea327a4fc67bb62babbcb6dbddc0acaab66cad3d Mon Sep 17 00:00:00 2001 From: Scott Leberknight <174812+sleberknight@users.noreply.github.com> Date: Sat, 7 Oct 2023 17:47:47 -0400 Subject: [PATCH] Add int-to-boolean conversion methods to KiwiPrimitives (#1057) * Add overloaded booleanFromInt methods: one accepts a int argument and uses the ZERO_OR_ONE conversion option, the second accepts int and BooleanConversionOption arguments. Closes #1054 --- .../org/kiwiproject/base/KiwiPrimitives.java | 29 ++++++++++++++ .../kiwiproject/base/KiwiPrimitivesTest.java | 40 ++++++++++++++++++- 2 files changed, 68 insertions(+), 1 deletion(-) diff --git a/src/main/java/org/kiwiproject/base/KiwiPrimitives.java b/src/main/java/org/kiwiproject/base/KiwiPrimitives.java index bf408272..8f964006 100644 --- a/src/main/java/org/kiwiproject/base/KiwiPrimitives.java +++ b/src/main/java/org/kiwiproject/base/KiwiPrimitives.java @@ -221,4 +221,33 @@ public static boolean booleanFromLong(long value, BooleanConversionOption option case NON_ZERO_AS_TRUE -> value != 0; }; } + + /** + * Converts the given int 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 #booleanFromInt(int, BooleanConversionOption) + */ + public static boolean booleanFromInt(int value) { + return booleanFromInt(value, BooleanConversionOption.ZERO_OR_ONE); + } + + /** + * Converts the given int value to a boolean using the specified {@link BooleanConversionOption}. + * + * @param value the value to convert + * @param option how to convert the int value into a boolean + * @return true if the value is non-zero, otherwise false + */ + public static boolean booleanFromInt(int 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; + }; + } } diff --git a/src/test/java/org/kiwiproject/base/KiwiPrimitivesTest.java b/src/test/java/org/kiwiproject/base/KiwiPrimitivesTest.java index a13a828b..9a2b934e 100644 --- a/src/test/java/org/kiwiproject/base/KiwiPrimitivesTest.java +++ b/src/test/java/org/kiwiproject/base/KiwiPrimitivesTest.java @@ -14,6 +14,7 @@ import org.junit.jupiter.params.provider.CsvSource; import org.junit.jupiter.params.provider.NullAndEmptySource; import org.junit.jupiter.params.provider.ValueSource; +import org.kiwiproject.base.KiwiPrimitives.BooleanConversionOption; @DisplayName("KiwiPrimitives") @ExtendWith(SoftAssertionsExtension.class) @@ -272,9 +273,46 @@ void shouldThrowIllegalArgument_WithZeroOrOneConversionOption_WhenValueIsNotZero 0, NON_ZERO_AS_TRUE, false """) void shouldConvertLong_UsingBooleanConversionOption(long value, - KiwiPrimitives.BooleanConversionOption option, + BooleanConversionOption option, boolean expectedResult) { assertThat(KiwiPrimitives.booleanFromLong(value, option)).isEqualTo(expectedResult); } } + + @Nested + class BooleanFromInt { + + @ParameterizedTest + @CsvSource(textBlock = """ + 1, true, + 0, false + """) + void shouldConvertInt_WithZeroOrOneConversionOption(int value, boolean expectedResult) { + assertThat(KiwiPrimitives.booleanFromInt(value)).isEqualTo(expectedResult); + } + + @ParameterizedTest + @ValueSource(ints = { -10, -5, -1, 2, 10, 42 }) + void shouldThrowIllegalArgument_WithZeroOrOneConversionOption_WhenValueIsNotZeroOrOne(int value) { + assertThatIllegalArgumentException() + .isThrownBy(() -> KiwiPrimitives.booleanFromInt(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 shouldConvertInt_UsingBooleanConversionOption(int value, + BooleanConversionOption option, + boolean expectedResult) { + assertThat(KiwiPrimitives.booleanFromInt(value, option)).isEqualTo(expectedResult); + } + } }