top.cutexingluo.tools.common.opt.OptRes Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of xingtools-core Show documentation
Show all versions of xingtools-core Show documentation
xingtools 核心,包括各种接口,实体类和工具类
package top.cutexingluo.tools.common.opt;
import lombok.Data;
import top.cutexingluo.tools.common.base.IData;
/**
* Optional 扩展类
*
* @author XingTian
* @version 1.0.0
* @date 2024/3/22 17:04
* @since 1.0.4
*/
@Data
public class OptRes implements IData {
protected T value;
protected Class clazz;
public OptRes(T value) {
this.value = value;
if (value != null) { // if null equals no params
this.clazz = (Class) value.getClass();
}
}
public OptRes() {
}
public OptRes(T value, Class clazz) {
this.value = value;
this.clazz = clazz;
}
public static OptRes of(V value) {
return new OptRes<>(value);
}
public static OptRes ofNoClazz(V value) {
return new OptRes<>(value, null);
}
@Override
public T data() {
return value;
}
/**
* 更新赋值
*
* @param updateClazzIfPresent true=> clazz 存在才赋值
*/
public void update(Object obj, boolean updateClazzIfPresent) {
if (obj == null) {
this.value = null;
} else {
updateCheckPresent(obj, updateClazzIfPresent);
}
}
/**
* obj 存在则更新赋值
*
* @param updateClazzIfPresent true=> 对象存在 clazz 才赋值
*/
public void updateCheckPresent(Object obj, boolean updateClazzIfPresent) {
if (obj == null) return;
if (obj instanceof OptRes) {
OptRes optRes = (OptRes) obj;
this.value = optRes.value;
if (!updateClazzIfPresent || optRes.clazz != null) this.clazz = optRes.clazz;
} else {
this.value = (T) obj;
if (!updateClazzIfPresent) this.clazz = (Class) obj.getClass();
}
}
}
© 2015 - 2024 Weber Informatics LLC | Privacy Policy