Write output to a given writer, not only stdout.
[xudocci.git] / src / main / java / net / pterodactylus / xdcc / ui / stdin / CommandReader.java
1 /*
2  * XdccDownloader - CommandReader.java - Copyright © 2013 David Roden
3  *
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.
8  *
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.
13  *
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/>.
16  */
17
18 package net.pterodactylus.xdcc.ui.stdin;
19
20 import java.io.BufferedReader;
21 import java.io.Reader;
22 import java.io.Writer;
23 import java.util.Collections;
24 import java.util.List;
25
26 import net.pterodactylus.xdcc.core.Core;
27 import net.pterodactylus.xdcc.data.Bot;
28 import net.pterodactylus.xdcc.data.Pack;
29
30 import com.google.common.collect.Lists;
31 import com.google.common.util.concurrent.AbstractExecutionThreadService;
32
33 /**
34  * Command interface for arbitrary {@link Reader}s.
35  *
36  * @author <a href="mailto:bombe@pterodactylus.net">David ‘Bombe’ Roden</a>
37  */
38 public class CommandReader extends AbstractExecutionThreadService {
39
40         /** The core being controlled. */
41         private final Core core;
42
43         /** The reader to read commands from. */
44         private final BufferedReader reader;
45
46         /** The writer to write the results to. */
47         private final Writer writer;
48
49         /**
50          * Creates a new command reader.
51          *
52          * @param core
53          *              The core being controlled
54          * @param reader
55          *              The reader to read commands from
56          * @param writer
57          *              The write to write results to
58          */
59         public CommandReader(Core core, Reader reader, Writer writer) {
60                 this.core = core;
61                 this.reader = new BufferedReader(reader);
62                 this.writer = writer;
63         }
64
65         //
66         // ABSTRACTEXECUTIONTHREADSERVICE METHODS
67         //
68
69         @Override
70         protected void run() throws Exception {
71                 String lastLine = "";
72                 String line;
73                 final List<Result> lastResult = Lists.newArrayList();
74                 while ((line = reader.readLine()) != null) {
75                         if (line.equals("")) {
76                                 line = lastLine;
77                         }
78                         String[] words = line.split(" +");
79                         if (words[0].equalsIgnoreCase("search")) {
80                                 lastResult.clear();
81                                 for (Bot bot : core.bots()) {
82                                         for (Pack pack : bot) {
83                                                 boolean found = true;
84                                                 for (int wordIndex = 1; wordIndex < words.length; ++wordIndex) {
85                                                         if (words[wordIndex].startsWith("-") && pack.name().toLowerCase().contains(words[wordIndex].toLowerCase().substring(1))) {
86                                                                 found = false;
87                                                                 break;
88                                                         }
89                                                         if (!words[wordIndex].startsWith("-") && !pack.name().toLowerCase().contains(words[wordIndex].toLowerCase())) {
90                                                                 found = false;
91                                                                 break;
92                                                         }
93                                                 }
94                                                 if (found) {
95                                                         lastResult.add(new Result(bot, pack));
96                                                 }
97                                         }
98                                 }
99                                 Collections.sort(lastResult);
100                                 int counter = 0;
101                                 for (Result result : lastResult) {
102                                         writer.write(String.format("[%d] %s (%s) from %s (#%s) on %s\n", counter++, result.pack().name(), result.pack().size(), result.bot().name(), result.pack().id(), result.bot().network().name()));
103                                 }
104                                 writer.write("End of Search.\n");
105                         }
106
107                         lastLine = line;
108                         writer.flush();
109                 }
110         }
111
112         /** Container for result information. */
113         private static class Result implements Comparable<Result> {
114
115                 /** The bot carrying the pack. */
116                 private final Bot bot;
117
118                 /** The pack. */
119                 private final Pack pack;
120
121                 /**
122                  * Creates a new result.
123                  *
124                  * @param bot
125                  *              The bot carrying the pack
126                  * @param pack
127                  *              The pack
128                  */
129                 private Result(Bot bot, Pack pack) {
130                         this.bot = bot;
131                         this.pack = pack;
132                 }
133
134                 //
135                 // ACCESSORS
136                 //
137
138                 /**
139                  * Returns the bot carrying the pack.
140                  *
141                  * @return The bot carrying the pack
142                  */
143                 public Bot bot() {
144                         return bot;
145                 }
146
147                 /**
148                  * Returns the pack.
149                  *
150                  * @return The pack
151                  */
152                 public Pack pack() {
153                         return pack;
154                 }
155
156                 //
157                 // COMPARABLE METHODS
158                 //
159
160                 @Override
161                 public int compareTo(Result result) {
162                         return pack().name().compareToIgnoreCase(result.pack().name());
163                 }
164
165         }
166
167 }