package cn.axzo.msg.center.utils.desision;

import lombok.AccessLevel;
import lombok.Getter;
import lombok.RequiredArgsConstructor;

import java.util.function.Supplier;

/**
 * @author yanglin
 */
@RequiredArgsConstructor(access = AccessLevel.PRIVATE)
public class DecisionValue<T> {

    private static final DecisionValue<?> NOT_SURE = new DecisionValue<>(Decision.NOT_SURE, null);

    private final Decision decision;
    @Getter private final T value;

    @SuppressWarnings("unchecked")
    public static <T> DecisionValue<T> notSure() {
        return (DecisionValue<T>) NOT_SURE;
    }

    public static <T> DecisionValue<T> decide(T value) {
        return new DecisionValue<>(Decision.DECIDED, value);
    }

    public T orElse(Supplier<T> supplier) {
        return isDecided() ? value : supplier.get();
    }

    public T orElse(T value) {
        return isDecided() ? this.value : value;
    }

    public boolean isDecided() {
        return decision == Decision.DECIDED;
    }

}