add comments to BaseAbilityBot.java and static construction method to ReplyCollection.java

This commit is contained in:
Valentin Afanasiev 2021-01-31 22:05:52 +03:00
parent ec4f81b94a
commit 39a5678543
3 changed files with 27 additions and 20 deletions

View File

@ -348,14 +348,14 @@ public abstract class BaseAbilityBot extends DefaultAbsSender implements Ability
.flatMap(ext -> stream(ext.getClass().getMethods()) .flatMap(ext -> stream(ext.getClass().getMethods())
.filter(checkReturnType(Reply.class)) .filter(checkReturnType(Reply.class))
.map(returnReply(ext))) .map(returnReply(ext)))
.flatMap(Reply::stream); .flatMap(Reply::stream);
// Extract all replies from extension instances methods, returning ReplyCollection
Stream<Reply> extensionCollectionReplies = extensions.stream() Stream<Reply> extensionCollectionReplies = extensions.stream()
.flatMap(extension -> stream(extension.getClass().getMethods()) .flatMap(extension -> stream(extension.getClass().getMethods())
.filter(checkReturnType(ReplyCollection.class)) .filter(checkReturnType(ReplyCollection.class))
.map(returnReplyCollection(extension)) .map(returnReplyCollection(extension))
.map(ReplyCollection::getReplies)) .flatMap(ReplyCollection::stream));
.flatMap(Collection::stream);
// Replies can be standalone or attached to abilities, fetch those too // Replies can be standalone or attached to abilities, fetch those too
Stream<Reply> abilityReplies = abilities.values().stream() Stream<Reply> abilityReplies = abilities.values().stream()

View File

@ -1,7 +1,15 @@
package org.telegram.abilitybots.api.objects; package org.telegram.abilitybots.api.objects;
import java.util.Collection; import java.util.Collection;
import java.util.stream.Stream;
import static com.google.common.collect.Lists.newArrayList;
/**
* A wrapping object containing Replies. Return this in your bot class to get replies registered.
*
* @see Reply
*/
public class ReplyCollection { public class ReplyCollection {
public final Collection<Reply> replies; public final Collection<Reply> replies;
@ -14,4 +22,11 @@ public class ReplyCollection {
return replies; return replies;
} }
public Stream<Reply> stream(){
return replies.stream();
}
public static ReplyCollection of(Reply... replies){
return new ReplyCollection(newArrayList(replies));
}
} }

View File

@ -8,9 +8,6 @@ import org.telegram.abilitybots.api.objects.Reply;
import org.telegram.abilitybots.api.objects.ReplyCollection; import org.telegram.abilitybots.api.objects.ReplyCollection;
import org.telegram.abilitybots.api.toggle.AbilityToggle; import org.telegram.abilitybots.api.toggle.AbilityToggle;
import java.util.ArrayList;
import java.util.List;
import static org.telegram.abilitybots.api.objects.Ability.builder; import static org.telegram.abilitybots.api.objects.Ability.builder;
import static org.telegram.abilitybots.api.objects.Flag.CALLBACK_QUERY; import static org.telegram.abilitybots.api.objects.Flag.CALLBACK_QUERY;
import static org.telegram.abilitybots.api.objects.Flag.MESSAGE; import static org.telegram.abilitybots.api.objects.Flag.MESSAGE;
@ -86,21 +83,16 @@ public class DefaultBot extends AbilityBot {
} }
public ReplyCollection createReplyCollection() { public ReplyCollection createReplyCollection() {
List<Reply> replyList = new ArrayList<>(); return ReplyCollection.of(
replyList.add( Reply.of(
Reply.of( upd -> silent.send("first reply answer", upd.getMessage().getChatId()),
upd -> silent.send("first reply answer", upd.getMessage().getChatId()), update -> update.getMessage().getText().equalsIgnoreCase(FIRST_REPLY_KEY_MESSAGE)
update -> update.getMessage().getText().equalsIgnoreCase(FIRST_REPLY_KEY_MESSAGE) ),
) Reply.of(
upd -> silent.send("second reply answer", upd.getMessage().getChatId()),
update -> update.getMessage().getText().equalsIgnoreCase(SECOND_REPLY_KEY_MESSAGE)
)
); );
replyList.add(
Reply.of(
upd -> silent.send("second reply answer", upd.getMessage().getChatId()),
update -> update.getMessage().getText().equalsIgnoreCase(SECOND_REPLY_KEY_MESSAGE)
)
);
return new ReplyCollection(replyList);
} }
public Ability testAbility() { public Ability testAbility() {