Merge pull request #15113 from danielmcnally285/danielmcnally285_return_first_non_null

BAEL-7010 - return first non null
This commit is contained in:
Liam Williams 2023-11-23 22:29:58 +00:00 committed by GitHub
commit b6dc257c80
4 changed files with 204 additions and 0 deletions

View File

@ -0,0 +1,16 @@
package com.baeldung.returnfirstnonnull;
class LazyEvaluate {
String methodA() {
return null;
}
String methodB() {
return "first non null";
}
String methodC() {
return "second non null";
}
}

View File

@ -0,0 +1,30 @@
package com.baeldung.returnfirstnonempty;
import static org.assertj.core.api.Assertions.assertThat;
import java.util.Arrays;
import java.util.List;
import java.util.Optional;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
public class ReturnFirstNonEmptyOptionalUnitTest {
private List<Optional<String>> optionals;
@BeforeEach
public void init() {
optionals = Arrays.asList(Optional.<String> empty(), Optional.of("first non empty"), Optional.of("second non empty"));
}
@Test
void givenListOfOptionals_whenStreaming_thenReturnFirstNonEmpty() {
Optional<String> object = optionals.stream()
.filter(Optional::isPresent)
.map(Optional::get)
.findFirst();
assertThat(object).contains("first non empty");
}
}

View File

@ -0,0 +1,77 @@
package com.baeldung.returnfirstnonnull;
import static org.assertj.core.api.Assertions.assertThat;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.mockito.Mockito.times;
import static org.mockito.Mockito.verify;
import java.util.Objects;
import java.util.Optional;
import java.util.function.Supplier;
import java.util.stream.Stream;
import org.apache.commons.lang3.ObjectUtils;
import org.junit.jupiter.api.Test;
import org.mockito.Mockito;
public class ReturnFirstNonNullLazyEvaluateUnitTest {
private final LazyEvaluate spy = Mockito.spy(new LazyEvaluate());
@Test
void givenChainOfMethods_whenUsingIfStatements_thenLazilyEvaluateMethodsUntilFirstNonNull() {
String object = spy.methodA();
if (object == null) {
object = spy.methodB();
}
if (object == null) {
object = spy.methodC();
}
assertEquals("first non null", object);
verify(spy, times(1)).methodA();
verify(spy, times(1)).methodB();
verify(spy, times(0)).methodC();
}
@Test
void givenChainOfMethods_whenUsingApacheCommonsLang3_thenReturnFirstNonNull() {
String object = ObjectUtils.getFirstNonNull(spy::methodA, spy::methodB, spy::methodC);
assertEquals("first non null", object);
verify(spy, times(1)).methodA();
verify(spy, times(1)).methodB();
verify(spy, times(0)).methodC();
}
@Test
void givenChainOfMethods_whenUsingSupplierInterface_thenLazilyEvaluateMethodsUntilFirstNonNull() {
Optional<String> object = Stream.<Supplier<String>> of(spy::methodA, spy::methodB, spy::methodC)
.map(Supplier::get)
.filter(Objects::nonNull)
.findFirst();
assertThat(object).contains("first non null");
verify(spy, times(1)).methodA();
verify(spy, times(1)).methodB();
verify(spy, times(0)).methodC();
}
@Test
void givenNonNullObjectAndFallbackMethod_whenUsingApacheCommonsLang3_thenReturnFirstNonNull() {
String nonNullObject = spy.methodB();
String object = ObjectUtils.getIfNull(nonNullObject, spy::methodC);
assertEquals("first non null", object);
verify(spy, times(0)).methodC();
}
@Test
void givenNullObjectAndFallbackMethod_whenUsingApacheCommonsLang3_thenReturnFirstNonNull() {
String nullObject = null;
String object = ObjectUtils.getIfNull(nullObject, spy::methodB);
assertEquals("first non null", object);
}
}

View File

@ -0,0 +1,81 @@
package com.baeldung.returnfirstnonnull;
import static org.assertj.core.api.Assertions.assertThat;
import static org.junit.jupiter.api.Assertions.assertEquals;
import java.util.Arrays;
import java.util.List;
import java.util.Objects;
import java.util.Optional;
import org.apache.commons.lang3.ObjectUtils;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import com.google.common.base.MoreObjects;
import com.google.common.base.Predicates;
import com.google.common.collect.Iterables;
public class ReturnFirstNonNullUnitTest {
private List<String> objects;
@BeforeEach
public void init() {
objects = Arrays.asList(null, "first non null", "second nun null");
}
@Test
void givenListOfObjects_whenFilterIsLambdaNullCheckInStream_thenReturnFirstNonNull() {
Optional<String> object = objects.stream()
.filter(o -> o != null)
.findFirst();
assertThat(object).contains("first non null");
}
@Test
void givenListOfObjects_whenFilterIsMethodRefNullCheckInStream_thenReturnFirstNonNull() {
Optional<String> object = objects.stream()
.filter(Objects::nonNull)
.findFirst();
assertThat(object).contains("first non null");
}
@Test
void givenListOfObjects_whenIteratingWithForLoop_thenReturnFirstNonNull() {
String object = null;
for (int i = 0; i < objects.size(); i++) {
if (objects.get(i) != null) {
object = objects.get(i);
break;
}
}
assertEquals("first non null", object);
}
@Test
void givenListOfObjects_whenUsingApacheCommonsLang3_thenReturnFirstNonNull() {
String object = ObjectUtils.firstNonNull(objects.toArray(new String[0]));
assertEquals("first non null", object);
}
@Test
void givenListOfObjects_whenUsingGoogleGuavaIterables_thenReturnFirstNonNull() {
String object = Iterables.find(objects, Predicates.notNull());
assertEquals("first non null", object);
}
@Test
void givenTwoObjects_whenUsingGoogleGuavaMoreObjects_thenReturnFirstNonNull() {
String nullObject = null;
String nonNullObject = "first non null";
String object = MoreObjects.firstNonNull(nullObject, nonNullObject);
assertEquals("first non null", object);
}
}