/*
 * @author Ranides Atterwim <ranides@gmail.com>
 * @copyright Ranides Atterwim
 * @license WTFPL
 * @url http://ranides.net/projects/assira
 */
package net.ranides.assira.generic;

import java.util.function.Supplier;


@SuppressWarnings({
    "PMD.ShortVar", 
    "PMD.ShortMethodName"
})
public final class ValueUtils {
    
    private ValueUtils() { }

    public static boolean isTrue(Object value) {
        return (value instanceof Boolean) && (Boolean)value;
    }

    public static boolean isFalse(Object value) {
        return (value instanceof Boolean) && !(Boolean)value;
    }
    
    /**
     * Returns first non-null value or {@code null} if any value doesn't fit
     * @param <T>
     * @param values
     * @return
     */
    @SafeVarargs
    public static <T> T or(T... values) {
        for(T value : values) { if(null != value) { return value; } }
        return null;
    }

    /**
     * Returns first non-null value or {@code null} if any value doesn't fit
     * @param <T>
     * @param a
     * @param b
     * @return
     */
    public static <T> T or(T a, T b) {
        if(null!=a) { return a; }
        if(null!=b) { return b; }
        return null;
    }
	
	public static <T> T eval(T ddefault, Supplier<? extends T> supplier) {
		try {
			T s = supplier.get();
			if(s != null) {
				return s;
			}
		} catch(NullPointerException $0) { // NOPMD
			// do nothing
		}
		return ddefault;
	}

}
