From 4d7e89afb74faf5bb51ecf9bed6365679414408a Mon Sep 17 00:00:00 2001 From: =?utf8?q?David=20=E2=80=98Bombe=E2=80=99=20Roden?= Date: Tue, 9 Apr 2013 06:58:45 +0200 Subject: [PATCH] Add command reader. --- .../pterodactylus/xdcc/ui/stdin/CommandReader.java | 152 +++++++++++++++++++++ 1 file changed, 152 insertions(+) create mode 100644 src/main/java/net/pterodactylus/xdcc/ui/stdin/CommandReader.java diff --git a/src/main/java/net/pterodactylus/xdcc/ui/stdin/CommandReader.java b/src/main/java/net/pterodactylus/xdcc/ui/stdin/CommandReader.java new file mode 100644 index 0000000..8b35858 --- /dev/null +++ b/src/main/java/net/pterodactylus/xdcc/ui/stdin/CommandReader.java @@ -0,0 +1,152 @@ +/* + * XdccDownloader - CommandReader.java - Copyright © 2013 David Roden + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ + +package net.pterodactylus.xdcc.ui.stdin; + +import java.io.BufferedReader; +import java.io.Reader; +import java.util.List; + +import net.pterodactylus.xdcc.core.Core; +import net.pterodactylus.xdcc.data.Bot; +import net.pterodactylus.xdcc.data.Pack; + +import com.google.common.collect.Lists; +import com.google.common.collect.Ordering; +import com.google.common.util.concurrent.AbstractExecutionThreadService; + +/** + * Command interface for arbitrary {@link Reader}s. + * + * @author David ‘Bombe’ Roden + */ +public class CommandReader extends AbstractExecutionThreadService { + + /** The core being controlled. */ + private final Core core; + + /** The reader to read commands from. */ + private final BufferedReader reader; + + /** + * Creates a new command reader. + * + * @param core + * The core being controlled + * @param reader + * The reader to read commands from + */ + public CommandReader(Core core, Reader reader) { + this.core = core; + this.reader = new BufferedReader(reader); + } + + // + // ABSTRACTEXECUTIONTHREADSERVICE METHODS + // + + @Override + protected void run() throws Exception { + String line; + final List lastResult = Lists.newArrayList(); + while ((line = reader.readLine()) != null) { + String[] words = line.split(" +"); + if (words[0].equalsIgnoreCase("search")) { + lastResult.clear(); + for (Bot bot : core.bots()) { + for (Pack pack : bot) { + boolean found = true; + for (int wordIndex = 1; wordIndex < words.length; ++wordIndex) { + if (words[wordIndex].startsWith("-") && pack.name().toLowerCase().contains(words[wordIndex].toLowerCase().substring(1))) { + found = false; + break; + } + if (!words[wordIndex].startsWith("-") && !pack.name().toLowerCase().contains(words[wordIndex].toLowerCase())) { + found = false; + break; + } + } + if (found) { + lastResult.add(new Result(bot, pack)); + } + } + } + int counter = 0; + for (Result result : Ordering.natural().sortedCopy(lastResult)) { + System.out.println(String.format("[%d] %s (%s) from %s (#%s) on %s", counter++, result.pack().name(), result.pack().size(), result.bot().name(), result.pack().id(), result.bot().network().name())); + } + System.out.println("End of Search."); + } + } + } + + /** Container for result information. */ + private static class Result implements Comparable { + + /** The bot carrying the pack. */ + private final Bot bot; + + /** The pack. */ + private final Pack pack; + + /** + * Creates a new result. + * + * @param bot + * The bot carrying the pack + * @param pack + * The pack + */ + private Result(Bot bot, Pack pack) { + this.bot = bot; + this.pack = pack; + } + + // + // ACCESSORS + // + + /** + * Returns the bot carrying the pack. + * + * @return The bot carrying the pack + */ + public Bot bot() { + return bot; + } + + /** + * Returns the pack. + * + * @return The pack + */ + public Pack pack() { + return pack; + } + + // + // COMPARABLE METHODS + // + + @Override + public int compareTo(Result result) { + return pack().name().compareToIgnoreCase(result.pack().name()); + } + + } + +} -- 2.7.4