2 * XdccDownloader - CommandReader.java - Copyright © 2013 David Roden
4 * This program is free software: you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation, either version 3 of the License, or
7 * (at your option) any later version.
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
14 * You should have received a copy of the GNU General Public License
15 * along with this program. If not, see <http://www.gnu.org/licenses/>.
18 package net.pterodactylus.xdcc.ui.stdin;
20 import java.io.BufferedReader;
21 import java.io.Reader;
22 import java.util.Collections;
23 import java.util.List;
25 import net.pterodactylus.xdcc.core.Core;
26 import net.pterodactylus.xdcc.data.Bot;
27 import net.pterodactylus.xdcc.data.Pack;
29 import com.google.common.collect.Lists;
30 import com.google.common.util.concurrent.AbstractExecutionThreadService;
33 * Command interface for arbitrary {@link Reader}s.
35 * @author <a href="mailto:bombe@pterodactylus.net">David ‘Bombe’ Roden</a>
37 public class CommandReader extends AbstractExecutionThreadService {
39 /** The core being controlled. */
40 private final Core core;
42 /** The reader to read commands from. */
43 private final BufferedReader reader;
46 * Creates a new command reader.
49 * The core being controlled
51 * The reader to read commands from
53 public CommandReader(Core core, Reader reader) {
55 this.reader = new BufferedReader(reader);
59 // ABSTRACTEXECUTIONTHREADSERVICE METHODS
63 protected void run() throws Exception {
66 final List<Result> lastResult = Lists.newArrayList();
67 while ((line = reader.readLine()) != null) {
68 if (line.equals("")) {
71 String[] words = line.split(" +");
72 if (words[0].equalsIgnoreCase("search")) {
74 for (Bot bot : core.bots()) {
75 for (Pack pack : bot) {
77 for (int wordIndex = 1; wordIndex < words.length; ++wordIndex) {
78 if (words[wordIndex].startsWith("-") && pack.name().toLowerCase().contains(words[wordIndex].toLowerCase().substring(1))) {
82 if (!words[wordIndex].startsWith("-") && !pack.name().toLowerCase().contains(words[wordIndex].toLowerCase())) {
88 lastResult.add(new Result(bot, pack));
92 Collections.sort(lastResult);
94 for (Result result : lastResult) {
95 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()));
97 System.out.println("End of Search.");
104 /** Container for result information. */
105 private static class Result implements Comparable<Result> {
107 /** The bot carrying the pack. */
108 private final Bot bot;
111 private final Pack pack;
114 * Creates a new result.
117 * The bot carrying the pack
121 private Result(Bot bot, Pack pack) {
131 * Returns the bot carrying the pack.
133 * @return The bot carrying the pack
149 // COMPARABLE METHODS
153 public int compareTo(Result result) {
154 return pack().name().compareToIgnoreCase(result.pack().name());