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

net.e6tech.elements.common.resources.Transactional Maven / Gradle / Ivy

There is a newer version: 2.7.9
Show newest version
/*
 * Copyright 2017 Futeh Kao
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package net.e6tech.elements.common.resources;

import net.e6tech.elements.common.util.function.*;

import java.util.concurrent.Callable;

/**
 * Created by futeh.
 */
@FunctionalInterface
@SuppressWarnings({"squid:S00112", "squid:S1602"})
public interface Transactional {

     T open();

    default  R apply(Callable callable) {
        return commit(callable);
    }

    default  R call(Callable callable) {
        return commit(callable);
    }

    default  R commit(Callable callable) {
        Resources resources = open();
        resources.submit(r -> {
            return callable.call();
        });
        return resources.commit();
    }

    default void run(RunnableWithException runnable) {
        commit(runnable);
    }

    default void accept(RunnableWithException runnable) {
        commit(runnable);
    }

    default void commit(RunnableWithException runnable) {
        Resources resources = open();
        resources.submit(r -> {
            runnable.run();
        });
        resources.commit();
    }

    default  R apply(Class cls, FunctionWithException function) {
        return commit(cls, function);
    }

    default  R commit(Class cls, FunctionWithException function) {
        Resources resources = open();
        resources.submit(r -> {
            return function.apply(r.getInstance(cls));
        });
        return resources.commit();
    }

    default  void accept(Class cls, ConsumerWithException consumer) {
            commit(cls, consumer);
    }

    default  void commit(Class cls, ConsumerWithException consumer) {
        Resources resources = open();
        resources.submit(r -> {
            consumer.accept(r.getInstance(cls));
        });
        resources.commit();
    }

    default  R apply(Class cls, Class cls2, BiFunctionWithException function) {
        return commit(cls, cls2, function);
    }

    default  R commit(Class cls, Class cls2, BiFunctionWithException function) {
        Resources resources = open();
        resources.submit(r -> {
            return function.apply(r.getInstance(cls), r.getInstance(cls2));
        });
        return resources.commit();
    }

    default  void accept(Class cls, Class cls2, BiConsumerWithException consumer) {
        commit(cls, cls2, consumer);
    }

    default  void commit(Class cls, Class cls2, BiConsumerWithException consumer) {
        Resources resources = open();
        resources.submit(r -> {
            consumer.accept(r.getInstance(cls), r.getInstance(cls2));
        });
        resources.commit();
    }

    default  R apply(Class cls, Class cls2, Class cls3, TriFunctionWithException function) {
        return commit(cls, cls2, cls3, function);
    }

    default  R commit(Class cls, Class cls2, Class cls3, TriFunctionWithException function) {
        Resources resources = open();
        resources.submit(r -> {
            return function.apply(r.getInstance(cls), r.getInstance(cls2), r.getInstance(cls3));
        });
        return resources.commit();
    }

    default  void accept(Class cls, Class cls2, Class cls3, TriConsumerWithException consumer) {
        commit(cls, cls2, cls3, consumer);
    }

    default  void commit(Class cls, Class cls2, Class cls3, TriConsumerWithException consumer) {
        Resources resources = open();
        resources.submit(r -> {
            consumer.accept(r.getInstance(cls), r.getInstance(cls2), r.getInstance(cls3));
        });
        resources.commit();
    }

    default  void accept(Class[] classes, ConsumerWithException consumer) {
        commit(classes, consumer);
    }

    @SuppressWarnings("unchecked")
    default  void commit(Class[] classes, ConsumerWithException consumer) {
        Resources resources = open();
        resources.submit(r -> {
            Object[] arguments = new Object[classes.length];
            for (int i = 0; i < arguments.length; i++) {
                arguments[i] = r.getInstance(classes[i]);
            }
            consumer.accept(arguments);
        });
        resources.commit();
    }

    default  void apply(Class[] classes, FunctionWithException function) {
        commit(classes, function);
    }

    @SuppressWarnings("unchecked")
    default  R commit(Class[] classes, FunctionWithException function) {
        Resources resources = open();
        resources.submit(r -> {
            Object[] arguments = new Object[classes.length];
            for (int i = 0; i < arguments.length; i++) {
                arguments[i] = r.getInstance(classes[i]);
            }
            return function.apply(arguments);
        });
        return resources.commit();
    }

}