All Downloads are FREE. Search and download functionalities are using the official Maven repository.

top.cutexingluo.tools.common.opt.OptionalResult Maven / Gradle / Ivy

There is a newer version: 1.1.6
Show newest version
package top.cutexingluo.tools.common.opt;


import lombok.Data;
import top.cutexingluo.tools.common.base.IData;

import java.util.function.Consumer;
import java.util.function.Supplier;

/**
 * Optional  扩展类
 *
 * @author XingTian
 * @version 1.0.0
 * @date 2024/3/22 16:37
 * @since 1.0.4
 */
@Data
public class OptionalResult implements IData {

    /**
     * 值
     */
    protected T value;

    /**
     * 是否存在
     */
    protected boolean exists;


    /**
     * 空对象
     */
    public OptionalResult() {
        this.exists = false;
    }

    /**
     * value 存在 则 exists = true
     */
    public OptionalResult(T value) {
        this.value = value;
        this.exists = this.value != null;
    }

    /**
     * 仅使用 exists 变量
     */
    public OptionalResult(boolean exists) {
        this.exists = exists;
    }

    public OptionalResult(T value, boolean exists) {
        this.value = value;
        this.exists = exists;
    }

    //-----methods------

    /**
     * 如果 exists == true , value 却为空则执行 consumer
     *
     * @return 最终结果/执行之后是否存在值
     */
    public boolean existsIfAbsent(Consumer> consumer) {
        if (exists && value == null) {
            if (consumer == null) {
                return false;
            }
            consumer.accept(this); // 需手动赋值 exists
        }
        return exists;
    }

    /**
     * 如果 exists == true , value 却为空则执行 consumer
     * 

自动判断结果是否有值

* * @return 最终结果/执行之后是否存在值 */ public boolean existsIfAbsentCheck(Consumer> consumer) { if (exists && value == null) { if (consumer == null) { return false; } consumer.accept(this); exists = value != null; } return exists; } /** * 如果 exists == true , value 却为空则执行 supplier *

自动判断结果是否有值

* * @return 最终结果/执行之后是否存在值 */ public boolean existsIfAbsentCheck(Supplier supplier) { return existsIfAbsentCheck((o) -> { if (supplier != null) { o.value = supplier.get(); } }); } @Override public T data() { return value; } }




© 2015 - 2024 Weber Informatics LLC | Privacy Policy