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

com.pojosontheweb.ttt.Util Maven / Gradle / Ivy

The newest version!
package com.pojosontheweb.ttt;

import java.nio.charset.Charset;
import java.util.Collections;
import java.util.List;

/**
 * Utilty class (exception trapping and the like)
 */
public class Util {

    public static  void toRtExNoResult(String msg, RunnableEx code) {
        try {
            code.run();
        } catch (Throwable e) {
            if (msg != null) {
                throw new RuntimeException(msg, e);
            } else {
                throw new RuntimeException(e);
            }
        }
    }

    public static  void toRtExNoResult(RunnableEx code) {
        toRtExNoResult(null, code);
    }

    public static  T toRtEx(SupplierEx code) {
        return toRtEx(null, code);
    }

    public static  T toRtEx(String msg, SupplierEx code) {
        try {
            return code.get();
        } catch (Throwable e) {
            if (msg == null) {
                throw new RuntimeException(e);
            } else {
                throw new RuntimeException(msg, e);
            }
        }
    }

    @FunctionalInterface
    public interface SupplierEx {
        T get() throws Throwable;
    }

    @FunctionalInterface
    public interface RunnableEx {
        void run() throws Throwable;
    }

    public static   List withEmptyListIfNull(List l) {
        if (l==null) {
            return Collections.emptyList();
        }
        return l;
    }

    public static  T elvis(T t, T defaultValue) {
        return t == null ? defaultValue : t;
    }

    public static final Charset CHARSET_UTF8 = Charset.forName("UTF-8");

    public static Charset getCharsetWithDefault(Charset charset) {
        return elvis(charset, CHARSET_UTF8);
    }

}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy