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

de.malkusch.telgrambot.api.AbstractTelegramApiProxy Maven / Gradle / Ivy

There is a newer version: 0.0.23
Show newest version
package de.malkusch.telgrambot.api;

import de.malkusch.telgrambot.*;
import de.malkusch.telgrambot.Update.CallbackUpdate.CallbackId;

import java.util.Collection;
import java.util.function.Consumer;
import java.util.function.Function;

import static java.util.Objects.requireNonNull;

public abstract class AbstractTelegramApiProxy implements TelegramApi {

    protected final TelegramApi api;

    protected AbstractTelegramApiProxy(TelegramApi api) {
        this.api = requireNonNull(api);
    }

    protected  R delegate(Function call) {
        return call.apply(api);
    }

    private void delegateVoid(Consumer call) {
        delegate(api -> {
            call.accept(api);
            return null;
        });
    }

    @Override
    public void receiveUpdates(UpdateReceiver... receivers) {
        delegateVoid(api -> api.receiveUpdates(receivers));
    }

    @Override
    public void dropPendingUpdates() {
        delegateVoid(TelegramApi::dropPendingUpdates);
    }

    @Override
    public MessageId send(String message, Button... buttons) {
        return delegate(api -> api.send(message, buttons));
    }

    @Override
    public MessageId send(String message) {
        return delegate(api -> api.send(message));
    }

    @Override
    public void pin(MessageId message) {
        delegateVoid(api -> api.pin(message));
    }

    @Override
    public PinnedMessage pinned() {
        return delegate(TelegramApi::pinned);
    }

    @Override
    public void unpin(MessageId message) {
        delegateVoid(api -> api.unpin(message));
    }

    @Override
    public void unpin() {
        delegateVoid(TelegramApi::unpin);
    }

    @Override
    public void delete(MessageId message) {
        delegateVoid(api -> api.delete(message));
    }

    @Override
    public void delete(Collection messages) {
        delegateVoid(api -> api.delete(messages));
    }

    @Override
    public void disableButtons(MessageId message) {
        delegateVoid(api -> api.disableButtons(message));
    }

    @Override
    public void react(MessageId message, Reaction reaction) {
        delegateVoid(api -> api.react(message, reaction));
    }

    @Override
    public void answer(CallbackId id) {
        delegateVoid(api -> api.answer(id));
    }

    @Override
    public void answer(CallbackId id, String alert) {
        delegateVoid(api -> api.answer(id, alert));
    }

    @Override
    public void close() throws Exception {
        api.close();
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy