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

org.nlab.util.Tries Maven / Gradle / Ivy

package org.nlab.util;

import java.util.concurrent.Callable;

import org.jooq.lambda.Unchecked;
import org.jooq.lambda.fi.lang.CheckedRunnable;
import org.jooq.lambda.fi.util.function.CheckedConsumer;
import org.jooq.lambda.fi.util.function.CheckedFunction;
import org.jooq.lambda.fi.util.function.CheckedSupplier;


/**
 * Created by nlabrot on 11/03/16.
 */
public final class Tries {

    public static  R tryWithResult(CheckedSupplier supplier, CheckedFunction f) {
        try (T closeable = supplier.get()) {
            return Unchecked.function(f).apply(closeable);
        } catch (Throwable e) {
            throw new IllegalStateException(e);
        }
    }

    public static  R tryWithResult(Callable f) {
        try {
            return f.call();
        } catch (Throwable e) {
            throw new IllegalStateException(e);
        }
    }


    public static  void tryWithoutResult(CheckedSupplier supplier, CheckedConsumer f) {
        try (T closeable = supplier.get()) {
            Unchecked.consumer(f).accept(closeable);
        } catch (Throwable e) {
            throw new IllegalStateException(e);
        }
    }

    public static  void tryWithoutResult(CheckedRunnable f) {
        try {
            f.run();
        } catch (Throwable e) {
            throw new IllegalStateException(e);
        }
    }

}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy