forked from plusone/plusone-commons
添加 OptionalUtil#orElseNull 方法。
This commit is contained in:
@@ -23,6 +23,8 @@ import java.util.OptionalLong;
|
||||
|
||||
import javax.annotation.Nullable;
|
||||
|
||||
import com.google.common.annotations.Beta;
|
||||
|
||||
/**
|
||||
* OptionalUtil
|
||||
*
|
||||
@@ -119,6 +121,19 @@ public class OptionalUtil {
|
||||
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() {
|
||||
throw new IllegalStateException("Utility class");
|
||||
}
|
||||
|
Reference in New Issue
Block a user