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

org.telegram.telegrambots.abilitybots.api.objects.Reply Maven / Gradle / Ivy

There is a newer version: 8.0.0
Show newest version
package org.telegram.telegrambots.abilitybots.api.objects;

import com.google.common.base.MoreObjects;
import com.google.common.collect.ImmutableList;
import org.telegram.telegrambots.abilitybots.api.bot.BaseAbilityBot;
import org.telegram.telegrambots.meta.api.objects.Update;

import java.util.List;
import java.util.Objects;
import java.util.function.BiConsumer;
import java.util.function.BiFunction;
import java.util.function.Predicate;
import java.util.stream.Stream;

import static com.google.common.collect.Lists.newArrayList;

/**
 * A reply consists of update conditionals and an action to be applied on the update.
 * 

* If an update satisfies the {@link Reply#conditions} set by the reply, then it's safe to {@link Reply#actOn(BaseAbilityBot, Update)}. * * @author Abbas Abou Daya */ public class Reply { public final List> conditions; public final BiConsumer action; private boolean statsEnabled; private String name; Reply(List> conditions, BiConsumer action) { this.conditions = ImmutableList.>builder() .addAll(conditions) .build(); this.action = action; statsEnabled = false; } Reply(List> conditions, BiConsumer action, String name) { this(conditions, action); if (Objects.nonNull(name)) { enableStats(name); } } public static Reply of(BiConsumer action, List> conditions) { return new Reply(conditions, action); } @SafeVarargs public static Reply of(BiConsumer action, Predicate... conditions) { return Reply.of(action, newArrayList(conditions)); } public boolean isOkFor(Update update) { // The following variable is required to avoid bug #JDK-8044546 BiFunction, Boolean> stateAnd = (state, cond) -> state && cond.test(update); return conditions.stream().reduce(true, stateAnd, Boolean::logicalAnd); } public void actOn(BaseAbilityBot bot, Update update) { action.accept(bot, update); } public List> conditions() { return conditions; } public BiConsumer action() { return action; } public Stream stream(){ return Stream.of(this); } public Reply enableStats(String name) { this.name = name; statsEnabled = true; return this; } public boolean statsEnabled() { return statsEnabled; } public String name() { return name; } @Override public boolean equals(Object o) { if (this == o) return true; if (o == null || getClass() != o.getClass()) return false; Reply reply = (Reply) o; return Objects.equals(conditions, reply.conditions) && Objects.equals(action, reply.action); } @Override public int hashCode() { return Objects.hash(conditions, action); } @Override public String toString() { return MoreObjects.toStringHelper(this) .add("conditions", conditions) .add("action", action) .toString(); } }





© 2015 - 2024 Weber Informatics LLC | Privacy Policy