forked from plusone/plusone-commons
添加 OptionalUtil#orElseNull 方法。
parent
dce24ef4ef
commit
6176f9ab71
|
@ -23,6 +23,8 @@ import java.util.OptionalLong;
|
||||||
|
|
||||||
import javax.annotation.Nullable;
|
import javax.annotation.Nullable;
|
||||||
|
|
||||||
|
import com.google.common.annotations.Beta;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* OptionalUtil
|
* OptionalUtil
|
||||||
*
|
*
|
||||||
|
@ -119,6 +121,19 @@ public class OptionalUtil {
|
||||||
return optionalOf(objectOptional.orElse(null));
|
return optionalOf(objectOptional.orElse(null));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* return the value of the optional object if present,
|
||||||
|
* otherwise {@code null}.
|
||||||
|
*
|
||||||
|
* @param <T> the class of the value
|
||||||
|
* @param optionalObj {@link Optional} object, which must be non-null.
|
||||||
|
* @return the value of the optional object if present, otherwise {@code null}.
|
||||||
|
*/
|
||||||
|
@Beta
|
||||||
|
public static <T> T orElseNull(Optional<T> optionalObj) {
|
||||||
|
return optionalObj.orElse(null);
|
||||||
|
}
|
||||||
|
|
||||||
private OptionalUtil() {
|
private OptionalUtil() {
|
||||||
throw new IllegalStateException("Utility class");
|
throw new IllegalStateException("Utility class");
|
||||||
}
|
}
|
||||||
|
|
|
@ -97,12 +97,12 @@ class SimpleJdbcTemplateTests {
|
||||||
final String sql = "INSERT INTO test_table(id, created_by, create_time, updated_by, update_time, status) VALUES(?, ?, ?, ?, ?, ?)";
|
final String sql = "INSERT INTO test_table(id, created_by, create_time, updated_by, update_time, status) VALUES(?, ?, ?, ?, ?, ?)";
|
||||||
final List<Object[]> params = recordList.stream()
|
final List<Object[]> params = recordList.stream()
|
||||||
.map(r -> new Object[] {
|
.map(r -> new Object[] {
|
||||||
r.getValueAsString("id").orElse(null),
|
OptionalUtil.orElseNull(r.getValueAsString("id")),
|
||||||
r.getValueAsString("created_by").orElse(null),
|
OptionalUtil.orElseNull(r.getValueAsString("created_by")),
|
||||||
r.getValueAsString("create_time").orElse(null),
|
OptionalUtil.orElseNull(r.getValueAsString("create_time")),
|
||||||
r.getValueAsString("updated_by").orElse(null),
|
OptionalUtil.orElseNull(r.getValueAsString("updated_by")),
|
||||||
r.getValueAsString("update_time").orElse(null),
|
OptionalUtil.orElseNull(r.getValueAsString("update_time")),
|
||||||
r.getValueAsString("status").orElse(null)
|
OptionalUtil.orElseNull(r.getValueAsString("status"))
|
||||||
})
|
})
|
||||||
.collect(Collectors.toList());
|
.collect(Collectors.toList());
|
||||||
try (Connection conn = this.dataSource.getConnection()) {
|
try (Connection conn = this.dataSource.getConnection()) {
|
||||||
|
|
Loading…
Reference in New Issue