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

com.moon.core.util.OptionalUtil Maven / Gradle / Ivy

package com.moon.core.util;

import com.moon.core.lang.ThrowUtil;
import com.moon.core.util.function.NullableFunction;

import java.util.function.*;

import static com.moon.core.lang.ThrowUtil.noInstanceError;

/**
 * @author moonsky
 */
public final class OptionalUtil {

    private OptionalUtil() { noInstanceError(); }

    /*
     * -----------------------------------------------------------
     * return int value
     * -----------------------------------------------------------
     */

    public static  int computeOrZeroIfNull(T obj, ToIntFunction function) {
        return computeOrDefaultIfNull(obj, function, 0);
    }

    public static  int computeOrOneIfNull(T obj, ToIntFunction function) {
        return computeOrDefaultIfNull(obj, function, 1);
    }

    public static  int computeOrDefaultIfNull(T obj, ToIntFunction fn, int defaultValue) {
        return obj == null ? defaultValue : fn.applyAsInt(obj);
    }

    public static  int computeOrElseIfNull(T obj, ToIntFunction function, IntSupplier supplier) {
        return obj == null ? supplier.getAsInt() : function.applyAsInt(obj);
    }

    /*
     * -----------------------------------------------------------
     * return value
     * -----------------------------------------------------------
     */

    public static  R computeOrNull(T obj, Function function) {
        return computeOrDefault(obj, function, null);
    }

    public static  R computeOrDefault(T obj, Function function, R elseVal) {
        return obj == null ? elseVal : function.apply(obj);
    }

    public static  R computeOrElse(T obj, Function function, Supplier supplier) {
        return obj == null ? supplier.get() : function.apply(obj);
    }

    public static  R computeOrThrow(T obj, Function function) {
        return obj == null ? ThrowUtil.runtime() : function.apply(obj);
    }

    public static  R computeOrThrow(T obj, Function function, String message) {
        return obj == null ? ThrowUtil.runtime(message) : function.apply(obj);
    }

    /*
     * -----------------------------------------------------------
     * doesn't has return value
     * -----------------------------------------------------------
     */

    public static  void ifPresent(T obj, Consumer consumer) {
        if (obj != null) {
            consumer.accept(obj);
        }
    }

    /*
     * get or else
     */

    public static  T orElse(T value, T defaultVal) {
        return value == null ? defaultVal : value;
    }

    public static  T orElseGet(T value, Supplier defaultSupplier) {
        return value == null ? defaultSupplier.get() : value;
    }

    public static Object getOrNull(Object optional) {
        return resolveOrNull(optional);
    }

    @SuppressWarnings("all")
    public static Object resolveOrNull(Object optionalReference) {
        return TypeofOptional.resolveOrNull(optionalReference);
    }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy