Skip to content

Commit

Permalink
Add KiwiBooleans and KiwiInteger util classes (#1125)
Browse files Browse the repository at this point in the history
* KiwiBooleans contains methods to convert Boolean into boolean
* KiwiIntegers contains methods to convert Integer to int

Closes #1123
Closes #1124
  • Loading branch information
sleberknight authored Apr 27, 2024
1 parent b1bfe19 commit 824b571
Show file tree
Hide file tree
Showing 4 changed files with 166 additions and 0 deletions.
47 changes: 47 additions & 0 deletions src/main/java/org/kiwiproject/base/KiwiBooleans.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
package org.kiwiproject.base;

import static java.util.Objects.isNull;

import lombok.experimental.UtilityClass;
import org.checkerframework.checker.nullness.qual.Nullable;

/**
* Utilities for working with {@link Boolean} wrapper objects.
*/
@UtilityClass
public class KiwiBooleans {

/**
* Return the {@code boolean} value of the {@link Boolean} when non-null,
* otherwise return {@code true}.
*
* @param booleanObject the possibly null Boolean object
* @return the boolean value of the Boolean object when non-null, otherwise {@code true}
*/
public static boolean toBooleanOrTrue(@Nullable Boolean booleanObject) {
return toBooleanOrDefault(booleanObject, true);
}

/**
* Return the {@code boolean} value of the {@link Boolean} when non-null,
* otherwise return {@code false}.
*
* @param booleanObject the possibly null Boolean object
* @return the boolean value of the Boolean object when non-null, otherwise {@code false}
*/
public static boolean toBooleanOrFalse(@Nullable Boolean booleanObject) {
return toBooleanOrDefault(booleanObject, false);
}

/**
* Return the {@code boolean} value of the {@link Boolean} when non-null,
* otherwise return the default value.
*
* @param booleanObject the possibly null Boolean object
* @param defaultValue the value to use when the Boolean argument is null
* @return the boolean value of the Boolean object when non-null, otherwise {@code defaultValue}
*/
public static boolean toBooleanOrDefault(@Nullable Boolean booleanObject, boolean defaultValue) {
return isNull(booleanObject) ? defaultValue : booleanObject;
}
}
36 changes: 36 additions & 0 deletions src/main/java/org/kiwiproject/base/KiwiIntegers.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
package org.kiwiproject.base;

import static java.util.Objects.isNull;

import lombok.experimental.UtilityClass;
import org.checkerframework.checker.nullness.qual.Nullable;

/**
* Utilities for working with {@link Integer} wrapper objects.
*/
@UtilityClass
public class KiwiIntegers {

/**
* Return the {@code int} value of the {@link Integer} when non-null,
* otherwise return zero.
*
* @param integerObject the possibly null Integer object
* @return the {@code int} value of the Integer object when non-null, otherwise {@code 0} (zero)
*/
public static int toIntOrZero(@Nullable Integer integerObject) {
return toIntOrDefault(integerObject, 0);
}

/**
* Return the {@code int} value of the {@link Integer} when non-null,
* otherwise return the default value.
*
* @param integerObject the possibly null Integer object
* @param defaultValue the value to use when the Integer argument is null
* @return the {@code int} value of the Integer object when non-null, otherwise {@code defaultValue}
*/
public static int toIntOrDefault(@Nullable Integer integerObject, int defaultValue) {
return isNull(integerObject) ? defaultValue : integerObject;
}
}
47 changes: 47 additions & 0 deletions src/test/java/org/kiwiproject/base/KiwiBooleansTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
package org.kiwiproject.base;

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

import org.junit.jupiter.api.DisplayName;
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.CsvSource;

@DisplayName("KiwiBooleans")
class KiwiBooleansTest {

@ParameterizedTest(name = "Boolean: {0}, expect: {1}")
@CsvSource(textBlock = """
TRUE, true,
FALSE, false,
null, true
""", nullValues = "null")
void toBooleanOrTrue(Boolean booleanObject, boolean expectedResult) {
assertThat(KiwiBooleans.toBooleanOrTrue(booleanObject))
.isEqualTo(expectedResult);
}

@ParameterizedTest(name = "Boolean: {0}, expect: {1}")
@CsvSource(textBlock = """
TRUE, true,
FALSE, false,
null, false
""", nullValues = "null")
void toBooleanOrFalse(Boolean booleanObject, boolean expectedResult) {
assertThat(KiwiBooleans.toBooleanOrFalse(booleanObject))
.isEqualTo(expectedResult);
}

@ParameterizedTest(name = "Boolean: {0}, default: {1}, expect: {2}")
@CsvSource(textBlock = """
TRUE, true, true
TRUE, false, true
FALSE, true, false
FALSE, true, false
null, true, true
null, false, false
""", nullValues = "null")
void toBooleanOrDefault(Boolean booleanObject, boolean defaultValue, boolean expectedResult) {
assertThat(KiwiBooleans.toBooleanOrDefault(booleanObject, defaultValue))
.isEqualTo(expectedResult);
}
}
36 changes: 36 additions & 0 deletions src/test/java/org/kiwiproject/base/KiwiIntegersTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
package org.kiwiproject.base;

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

import org.junit.jupiter.api.DisplayName;
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.CsvSource;

@DisplayName("KiwiIntegers")
class KiwiIntegersTest {

@ParameterizedTest(name = "Integer: {0}, expect: {1}")
@CsvSource(textBlock = """
1, 1
42, 42
-10, -10
null, 0
""", nullValues = "null")
void toIntOrZero(Integer integerObject, int expectedResult) {
assertThat(KiwiIntegers.toIntOrZero(integerObject))
.isEqualTo(expectedResult);
}

@ParameterizedTest(name = "Integer: {0}, default: {1}, expect: {2}")
@CsvSource(textBlock = """
10, 0, 10
42, 11, 42
-10, 5, -10
null, 0, 0
null, 42, 42
""", nullValues = "null")
void toIntOrDefault(Integer integerObject, int defaultValue, int expectedResult) {
assertThat(KiwiIntegers.toIntOrDefault(integerObject, defaultValue))
.isEqualTo(expectedResult);
}
}

0 comments on commit 824b571

Please sign in to comment.