Add command reader.
[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.util.List;
23
24 import net.pterodactylus.xdcc.core.Core;
25 import net.pterodactylus.xdcc.data.Bot;
26 import net.pterodactylus.xdcc.data.Pack;
27
28 import com.google.common.collect.Lists;
29 import com.google.common.collect.Ordering;
30 import com.google.common.util.concurrent.AbstractExecutionThreadService;
31
32 /**
33  * Command interface for arbitrary {@link Reader}s.
34  *
35  * @author <a href="mailto:bombe@pterodactylus.net">David ‘Bombe’ Roden</a>
36  */
37 public class CommandReader extends AbstractExecutionThreadService {
38
39         /** The core being controlled. */
40         private final Core core;
41
42         /** The reader to read commands from. */
43         private final BufferedReader reader;
44
45         /**
46          * Creates a new command reader.
47          *
48          * @param core
49          *              The core being controlled
50          * @param reader
51          *              The reader to read commands from
52          */
53         public CommandReader(Core core, Reader reader) {
54                 this.core = core;
55                 this.reader = new BufferedReader(reader);
56         }
57
58         //
59         // ABSTRACTEXECUTIONTHREADSERVICE METHODS
60         //
61
62         @Override
63         protected void run() throws Exception {
64                 String line;
65                 final List<Result> lastResult = Lists.newArrayList();
66                 while ((line = reader.readLine()) != null) {
67                         String[] words = line.split(" +");
68                         if (words[0].equalsIgnoreCase("search")) {
69                                 lastResult.clear();
70                                 for (Bot bot : core.bots()) {
71                                         for (Pack pack : bot) {
72                                                 boolean found = true;
73                                                 for (int wordIndex = 1; wordIndex < words.length; ++wordIndex) {
74                                                         if (words[wordIndex].startsWith("-") && pack.name().toLowerCase().contains(words[wordIndex].toLowerCase().substring(1))) {
75                                                                 found = false;
76                                                                 break;
77                                                         }
78                                                         if (!words[wordIndex].startsWith("-") && !pack.name().toLowerCase().contains(words[wordIndex].toLowerCase())) {
79                                                                 found = false;
80                                                                 break;
81                                                         }
82                                                 }
83                                                 if (found) {
84                                                         lastResult.add(new Result(bot, pack));
85                                                 }
86                                         }
87                                 }
88                                 int counter = 0;
89                                 for (Result result : Ordering.natural().sortedCopy(lastResult)) {
90                                         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()));
91                                 }
92                                 System.out.println("End of Search.");
93                         }
94                 }
95         }
96
97         /** Container for result information. */
98         private static class Result implements Comparable<Result> {
99
100                 /** The bot carrying the pack. */
101                 private final Bot bot;
102
103                 /** The pack. */
104                 private final Pack pack;
105
106                 /**
107                  * Creates a new result.
108                  *
109                  * @param bot
110                  *              The bot carrying the pack
111                  * @param pack
112                  *              The pack
113                  */
114                 private Result(Bot bot, Pack pack) {
115                         this.bot = bot;
116                         this.pack = pack;
117                 }
118
119                 //
120                 // ACCESSORS
121                 //
122
123                 /**
124                  * Returns the bot carrying the pack.
125                  *
126                  * @return The bot carrying the pack
127                  */
128                 public Bot bot() {
129                         return bot;
130                 }
131
132                 /**
133                  * Returns the pack.
134                  *
135                  * @return The pack
136                  */
137                 public Pack pack() {
138                         return pack;
139                 }
140
141                 //
142                 // COMPARABLE METHODS
143                 //
144
145                 @Override
146                 public int compareTo(Result result) {
147                         return pack().name().compareToIgnoreCase(result.pack().name());
148                 }
149
150         }
151
152 }