求求你,不要再使用!=null判空了!
/**
* @author Axin
* @since 2020-09-20
* @summary 一个User类定义
* (Ps:Data 是lombok组件提供的注解,简化了get set等等的约定代码)
*/
@Data
public class User {
private String name;
private String gender;
private School school;
@Data
public static class School {
private String scName;
private String adress;
}
}
public static void main(String[] args) {
User axin = new User();
User.School school = new User.School();
axin.setName("hello");
if (Objects.nonNull(axin) && Objects.nonNull(axin.getSchool())) {
User.School userSc = axin.getSchool();
System.out.println(userSc.getAdress());
}
}
public static void main(String[] args) {
User axin = new User();
User.School school = new User.School();
axin.setName("hello");
// 1. 基本调用
String value1 = OptionalBean.ofNullable(axin)
.getBean(User::getSchool)
.getBean(User.School::getAdress).get();
System.out.println(value1);
}
/**
* @author Axin
* @since 2020-09-10
* @summary 链式调用 bean 中 value 的方法
*/
public final class OptionalBean<T> {
private static final OptionalBean> EMPTY = new OptionalBean<>();
private final T value;
private OptionalBean() {
this.value = null;
}
/**
* 空值会抛出空指针
* @param value
*/
private OptionalBean(T value) {
this.value = Objects.requireNonNull(value);
}
/**
* 包装一个不能为空的 bean
* @param value
* @param
* @return
*/
public staticOptionalBean of(T value) {
return new OptionalBean<>(value);
}
/**
* 包装一个可能为空的 bean
* @param value
* @param
* @return
*/
public staticOptionalBean ofNullable(T value) {
return value == null ? empty() : of(value);
}
/**
* 取出具体的值
* @param fn
* @param
* @return
*/
public T get() {
return Objects.isNull(value) ? null : value;
}
/**
* 取出一个可能为空的对象
* @param fn
* @param
* @return
*/
publicOptionalBean getBean(Function super T, ? extends R> fn) {
return Objects.isNull(value) ? OptionalBean.empty() : OptionalBean.ofNullable(fn.apply(value));
}
/**
* 如果目标值为空 获取一个默认值
* @param other
* @return
*/
public T orElse(T other) {
return value != null ? value : other;
}
/**
* 如果目标值为空 通过lambda表达式获取一个值
* @param other
* @return
*/
public T orElseGet(Supplier extends T> other) {
return value != null ? value : other.get();
}
/**
* 如果目标值为空 抛出一个异常
* @param exceptionSupplier
* @param
* @return
* @throws X
*/
publicT orElseThrow(Supplier extends X> exceptionSupplier) throws X {
if (value != null) {
return value;
} else {
throw exceptionSupplier.get();
}
}
public boolean isPresent() {
return value != null;
}
public void ifPresent(Consumer super T> consumer) {
if (value != null)
consumer.accept(value);
}
@Override
public int hashCode() {
return Objects.hashCode(value);
}
/**
* 空值常量
* @param
* @return
*/
public staticOptionalBean empty() {
@SuppressWarnings("unchecked")
OptionalBeannone = (OptionalBean ) EMPTY;
return none;
}
}
public static void main(String[] args) {
User axin = new User();
User.School school = new User.School();
axin.setName("hello");
// 1. 基本调用
String value1 = OptionalBean.ofNullable(axin)
.getBean(User::getSchool)
.getBean(User.School::getAdress).get();
System.out.println(value1);
// 2. 扩展的 isPresent方法 用法与 Optional 一样
boolean present = OptionalBean.ofNullable(axin)
.getBean(User::getSchool)
.getBean(User.School::getAdress).isPresent();
System.out.println(present);
// 3. 扩展的 ifPresent 方法
OptionalBean.ofNullable(axin)
.getBean(User::getSchool)
.getBean(User.School::getAdress)
.ifPresent(adress -> System.out.println(String.format("地址存在:%s", adress)));
// 4. 扩展的 orElse
String value2 = OptionalBean.ofNullable(axin)
.getBean(User::getSchool)
.getBean(User.School::getAdress).orElse("家里蹲");
System.out.println(value2);
// 5. 扩展的 orElseThrow
try {
String value3 = OptionalBean.ofNullable(axin)
.getBean(User::getSchool)
.getBean(User.School::getAdress).orElseThrow(() -> new RuntimeException("空指针了"));
} catch (Exception e) {
System.out.println(e.getMessage());
}
}