--- /dev/null
+/*
+ * 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 <http://www.gnu.org/licenses/>.
+ */
+
+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 <a href="mailto:bombe@pterodactylus.net">David ‘Bombe’ Roden</a>
+ */
+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<Result> 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<Result> {
+
+ /** 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());
+ }
+
+ }
+
+}