-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add KiwiBooleans and KiwiInteger util classes (#1125)
* KiwiBooleans contains methods to convert Boolean into boolean * KiwiIntegers contains methods to convert Integer to int Closes #1123 Closes #1124
- Loading branch information
1 parent
b1bfe19
commit 824b571
Showing
4 changed files
with
166 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} |