Более удобный API для AnswerText

This commit is contained in:
Struchkov Mark 2023-05-14 01:49:36 +03:00
parent 423e17f896
commit 812c82467c
Signed by: upagge
GPG Key ID: D3018BE7BA428CA6
4 changed files with 65 additions and 14 deletions

View File

@ -7,7 +7,6 @@ import dev.struchkov.godfather.main.domain.jackson.TelegramPayloadSerializer;
import dev.struchkov.godfather.main.domain.keyboard.KeyBoard; import dev.struchkov.godfather.main.domain.keyboard.KeyBoard;
import dev.struchkov.godfather.quarkus.domain.content.send.SendAttachment; import dev.struchkov.godfather.quarkus.domain.content.send.SendAttachment;
import dev.struchkov.haiti.utils.container.ContextKey; import dev.struchkov.haiti.utils.container.ContextKey;
import io.smallrye.mutiny.Uni;
import lombok.Getter; import lombok.Getter;
import lombok.NoArgsConstructor; import lombok.NoArgsConstructor;
import lombok.Setter; import lombok.Setter;
@ -149,10 +148,6 @@ public class BoxAnswer {
return Optional.empty(); return Optional.empty();
} }
public Uni<BoxAnswer> toUni() {
return Uni.createFrom().item(this);
}
public static final class Builder { public static final class Builder {
private String message; private String message;

View File

@ -6,6 +6,7 @@ import dev.struchkov.godfather.main.domain.unit.UnitActiveType;
import dev.struchkov.godfather.quarkus.domain.BoxAnswer; import dev.struchkov.godfather.quarkus.domain.BoxAnswer;
import dev.struchkov.godfather.quarkus.domain.unit.func.CallBackConsumer; import dev.struchkov.godfather.quarkus.domain.unit.func.CallBackConsumer;
import dev.struchkov.godfather.quarkus.domain.unit.func.ProcessingData; import dev.struchkov.godfather.quarkus.domain.unit.func.ProcessingData;
import dev.struchkov.godfather.quarkus.domain.unit.func.UniConsumer;
import dev.struchkov.godfather.quarkus.domain.unit.func.UniPredicate; import dev.struchkov.godfather.quarkus.domain.unit.func.UniPredicate;
import io.smallrye.mutiny.Uni; import io.smallrye.mutiny.Uni;
@ -15,6 +16,7 @@ import java.util.Set;
import java.util.UUID; import java.util.UUID;
import java.util.function.Consumer; import java.util.function.Consumer;
import java.util.function.Function; import java.util.function.Function;
import java.util.function.Predicate;
import java.util.function.Supplier; import java.util.function.Supplier;
import java.util.regex.Pattern; import java.util.regex.Pattern;
import java.util.stream.Collectors; import java.util.stream.Collectors;
@ -108,21 +110,46 @@ public class AnswerText<M extends Message> extends MainUnit<M> {
return this; return this;
} }
public Builder<M> answer(Consumer<M> answer) { public Builder<M> answerConsumer(Consumer<M> consumer) {
this.boxAnswer = message -> { this.boxAnswer = message -> Uni.createFrom().item(() -> {
answer.accept(message); consumer.accept(message);
return Uni.createFrom().nullItem(); return null;
}; });
return this; return this;
} }
public Builder<M> answer(Function<M, Uni<BoxAnswer>> answer) { public Builder<M> answerConsumerUni(UniConsumer<M> uniConsumer) {
this.boxAnswer = message -> uniConsumer.accept(message).replaceWith(() -> null);
return this;
}
public Builder<M> answerUni(Uni<?> answer) {
this.boxAnswer = message -> answer.replaceWith(() -> null);
return this;
}
public Builder<M> answer(BoxAnswer boxAnswer) {
this.boxAnswer = message -> Uni.createFrom().item(boxAnswer);
return this;
}
public Builder<M> answerUni(Function<M, Uni<BoxAnswer>> answer) {
this.boxAnswer = answer::apply; this.boxAnswer = answer::apply;
return this; return this;
} }
public Builder<M> answer(Function<M, BoxAnswer> answer) {
this.boxAnswer = message -> Uni.createFrom().item(() -> answer.apply(message));
return this;
}
public Builder<M> answer(Supplier<BoxAnswer> answer) { public Builder<M> answer(Supplier<BoxAnswer> answer) {
this.boxAnswer = message -> Uni.createFrom().item(answer.get()); this.boxAnswer = message -> Uni.createFrom().item(answer);
return this;
}
public Builder<M> answerUni(Supplier<Uni<BoxAnswer>> answer) {
this.boxAnswer = message -> answer.get();
return this; return this;
} }
@ -189,11 +216,16 @@ public class AnswerText<M extends Message> extends MainUnit<M> {
return this; return this;
} }
public Builder<M> triggerCheck(UniPredicate<M> trigger) { public Builder<M> triggerCheckUni(UniPredicate<M> trigger) {
triggerCheck = trigger; triggerCheck = trigger;
return this; return this;
} }
public Builder<M> triggerCheck(Predicate<M> trigger) {
triggerCheck = UniPredicate.predicate(trigger);
return this;
}
public Builder<M> matchThreshold(Integer val) { public Builder<M> matchThreshold(Integer val) {
matchThreshold = val; matchThreshold = val;
return this; return this;

View File

@ -10,6 +10,7 @@ import dev.struchkov.haiti.utils.Checker;
import java.util.HashSet; import java.util.HashSet;
import java.util.Set; import java.util.Set;
import java.util.UUID; import java.util.UUID;
import java.util.function.Predicate;
import java.util.regex.Pattern; import java.util.regex.Pattern;
import java.util.stream.Collectors; import java.util.stream.Collectors;
@ -131,11 +132,16 @@ public class ReplaceCmd<M extends Message> extends MainUnit<M> {
return this; return this;
} }
public Builder<M> triggerCheck(UniPredicate<M> trigger) { public Builder<M> triggerCheckUni(UniPredicate<M> trigger) {
triggerCheck = trigger; triggerCheck = trigger;
return this; return this;
} }
public Builder<M> triggerCheck(Predicate<M> trigger) {
triggerCheck = UniPredicate.predicate(trigger);
return this;
}
public Builder<M> matchThreshold(Integer val) { public Builder<M> matchThreshold(Integer val) {
matchThreshold = val; matchThreshold = val;
return this; return this;

View File

@ -0,0 +1,18 @@
package dev.struchkov.godfather.quarkus.domain.unit.func;
import io.smallrye.mutiny.Uni;
import java.util.function.Consumer;
public interface UniConsumer<T> {
Uni<Void> accept(T t);
static <D> UniConsumer<D> consumer(Consumer<D> consumer) {
return t -> Uni.createFrom().deferred(() -> {
consumer.accept(t);
return Uni.createFrom().voidItem();
});
}
}