Last active
September 21, 2021 18:24
-
-
Save MuffinTheMan/4cd10f8e5630059ecdc76411fe3a5f8b to your computer and use it in GitHub Desktop.
Simple helper method(s) for Java.
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
package com.coolstuffs.util; | |
import javax.annotation.Nullable; | |
public class ObjectUtil { | |
/** | |
* Similar to MySQL IFNULL(). If first parameter is null, second parameter will be | |
* returned--otherwise, first parameter will be returned. Think of this as the first | |
* parameter being the preferred value and the second parameter as the fall-back value. | |
* | |
* @param preferredValue | |
* @param fallbackValue | |
* @param <T> | |
* @return preferredValue if not null--fallbackValue, otherwise | |
*/ | |
@Nullable | |
public static <T> T ifNull(@Nullable final T preferredValue, @Nullable final T fallbackValue) { | |
return firstNonnull(preferredValue, fallbackValue); | |
} | |
/** | |
* Returns the first nonnull parameter Think of the first parameter as being the preferred | |
* value and the remaining parameters as the fall-back values (in order of preference). | |
* | |
* @param values | |
* @param <T> | |
* @return The first nonnull parameter (or null if all are null) | |
*/ | |
@SafeVarargs | |
@Nullable | |
public static <T> T firstNonnull(@Nullable final T... values) { | |
if (values == null) { | |
return null; | |
} | |
for (final T value : values) { | |
if (value != null) { | |
return value; | |
} | |
} | |
return null; | |
} | |
} | |
// ----- ----- ----- TESTS ----- ----- ----- // | |
// ----- ----- TESTS ----- TESTS ----- ----- // | |
// ----- ----- ----- TESTS ----- ----- ----- // | |
package com.coolstuffs.util; | |
import org.junit.jupiter.params.ParameterizedTest; | |
import org.junit.jupiter.params.provider.CsvSource; | |
import static org.assertj.core.api.Assertions.assertThat; | |
class ObjectUtilUnitTest { | |
@ParameterizedTest | |
@CsvSource(value = { | |
",,", // Both params null--null expected | |
"hello,hello,goodbye", // Neither param null--first param expected | |
"hello,hello,", // First param nonnull--first param expected | |
"goodbye,,goodbye", // First param null--second param expected | |
}) | |
void ifNullStrings(final String expectedValue, final String param1, final String param2) { | |
assertThat(ObjectUtil.ifNull(param1, param2)).isEqualTo(expectedValue); | |
} | |
@ParameterizedTest | |
@CsvSource(value = { | |
",,", // Both params null--null expected | |
"1,1,2", // Neither param null--first param expected | |
"1,1,", // First param nonnull--first param expected | |
"2,,2", // First param null--second param expected | |
}) | |
void ifNullIntegers(final Integer expectedValue, final Integer param1, final Integer param2) { | |
assertThat(ObjectUtil.ifNull(param1, param2)).isEqualTo(expectedValue); | |
} | |
@ParameterizedTest | |
@CsvSource(value = { | |
",,,,,", // All params null--null expected | |
"1,1,2,3,4,5", // No params null--first param expected | |
"1,1,,,,", // First param nonnull--first param expected | |
"2,,2,3,4,5", // First param null--second param expected | |
"3,,,3,4,5", // First and second params null--third param expected | |
"4,,,,4,5", // First, second and third params null--fourth param expected | |
"5,,,,,5", // First, second, third and fourth params null--fifth param expected | |
}) | |
void firstNonnullIntegers(final Integer expectedValue, final Integer param1, final Integer param2, final Integer param3, final Integer param4, final Integer param5) { | |
assertThat(ObjectUtil.firstNonnull(param1, param2, param3, param4, param5)).isEqualTo(expectedValue); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment