From f2da0a65589748bd29d61e7a94674aff4697b5fe Mon Sep 17 00:00:00 2001 From: tschulz Date: Fri, 20 May 2016 11:12:22 +0200 Subject: [PATCH] Implement Command Registry --- .../api/commands/CommandRegistry.java | 86 +++++++++++++++++++ 1 file changed, 86 insertions(+) create mode 100644 src/main/java/org/telegram/telegrambots/api/commands/CommandRegistry.java diff --git a/src/main/java/org/telegram/telegrambots/api/commands/CommandRegistry.java b/src/main/java/org/telegram/telegrambots/api/commands/CommandRegistry.java new file mode 100644 index 00000000..ab9afd6f --- /dev/null +++ b/src/main/java/org/telegram/telegrambots/api/commands/CommandRegistry.java @@ -0,0 +1,86 @@ +package org.telegram.telegrambots.api.commands; + +import org.telegram.telegrambots.api.objects.Message; + +import java.util.Arrays; +import java.util.Collection; +import java.util.HashMap; +import java.util.Map; + +/** + * @author tschulz + */ +public final class CommandRegistry implements ICommandRegistery { + + private final Map commandRegistryMap = new HashMap<>(); + + public CommandRegistry(String botToken) { + register(new HelpCommand(this, botToken)); + } + + @Override + public final boolean register(Command command) { + if (commandRegistryMap.containsKey(command.getCommandIdentifier())) { + return false; + } + commandRegistryMap.put(command.getCommandIdentifier(), command); + return true; + } + + @Override + public final Map registerAll(Command... commands) { + Map resultMap = new HashMap<>(commands.length); + for (Command command : commands) { + resultMap.put(command, register(command)); + } + return resultMap; + } + + @Override + public final boolean deregister(Command command) { + if (commandRegistryMap.containsKey(command.getCommandIdentifier())) { + commandRegistryMap.remove(command.getCommandIdentifier()); + return true; + } + return false; + } + + @Override + public final Map deregisterAll(Command... commands) { + Map resultMap = new HashMap<>(commands.length); + for (Command command : commands) { + resultMap.put(command, deregister(command)); + } + return resultMap; + } + + @Override + public final Collection getRegisteredCommands() { + return commandRegistryMap.values(); + } + + /** + * executes a command if present and replies the success + * + * @param message input message + * @return true if success or false otherwise + */ + public final boolean executeCommand(Message message) { + if (message.hasText()) { + String text = message.getText(); + if (!text.isEmpty() && text.startsWith(Command.COMMAND_INIT_CHARACTER)) { + String commandMessage = text.substring(1); + String[] commandSplit = commandMessage.split(Command.COMMAND_PARAMETER_SEPERATOR); + + String command = commandSplit[0]; + + if (commandRegistryMap.containsKey(command)) { + String[] parameters = Arrays.copyOfRange(commandSplit, 1, commandSplit.length); + commandRegistryMap.get(command).execute(parameters, message.getChatId()); + return true; + } + } + } + return false; + } +} \ No newline at end of file