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

io.github.ma1uta.matrix.bot.Context Maven / Gradle / Ivy

The newest version!
/*
 * Copyright Anatoliy Sablin [email protected]
 *
 * 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 io.github.ma1uta.matrix.bot;

import io.github.ma1uta.matrix.client.MatrixClient;

import java.util.ArrayList;
import java.util.List;
import java.util.function.BiConsumer;
import java.util.function.BiFunction;
import java.util.function.Supplier;

/**
 * Context of the bot.
 *
 * @param  bot configuration.
 * @param  bot dao.
 * @param  bot service.
 * @param  extra data.
 */
public class Context, S extends PersistentService, E> {

    private final Object monitor = new Object();

    private final MatrixClient matrixClient;

    private final S service;

    private C config;

    private E data;

    private List> shutdownListeners = new ArrayList<>();

    private final Bot bot;

    public Context(MatrixClient matrixClient, S service, Bot bot) {
        this.matrixClient = matrixClient;
        this.service = service;
        this.bot = bot;
    }

    public MatrixClient getMatrixClient() {
        return matrixClient;
    }

    public C getConfig() {
        return config;
    }

    public void setConfig(C config) {
        this.config = config;
    }

    public E getData() {
        return data;
    }

    public void setData(E data) {
        this.data = data;
    }

    protected S getService() {
        return service;
    }

    public List> getShutdownListeners() {
        return shutdownListeners;
    }

    public Bot getBot() {
        return bot;
    }

    /**
     * Invoke separate transaction.
     *
     * @param action action.
     */
    public void runInTransaction(BiConsumer, D> action) {
        synchronized (monitor) {
            getService().invoke(dao -> {
                action.accept(this, dao);
                setConfig(dao.save(getConfig()));
            });
        }
    }

    /**
     * INvoke separate transaction.
     *
     * @param action action.
     * @param     result's class.
     * @return result.
     */
    public  R runInTransaction(BiFunction, D, R> action) {
        synchronized (monitor) {
            return getService().invoke(dao -> {
                R result = action.apply(this, dao);
                setConfig(dao.save(getConfig()));
                return result;
            });
        }
    }

    /**
     * Add a new shutdown listener.
     *
     * @param listener shutdown listener.
     */
    public void addShutdownListener(Supplier listener) {
        getShutdownListeners().add(listener);
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy