85b92d119f8e0d767af88e183bfb05da2c511b7f
[xudocci.git] / src / main / java / net / pterodactylus / xdcc / ui / stdin / StatsCommand.java
1 /*
2  * XdccDownloader - StatsCommand.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 static java.util.Collections.emptyList;
21
22 import java.io.IOException;
23 import java.io.Writer;
24 import java.util.Collection;
25 import java.util.List;
26 import java.util.Set;
27
28 import net.pterodactylus.xdcc.core.Core;
29 import net.pterodactylus.xdcc.data.Bot;
30 import net.pterodactylus.xdcc.data.Pack;
31
32 import com.google.common.collect.Sets;
33
34 /**
35  * Command that outputs a short statistic of what is going on.
36  *
37  * @author <a href="mailto:bombe@pterodactylus.net">David ‘Bombe’ Roden</a>
38  */
39 public class StatsCommand implements Command {
40
41         /** The core to operate on. */
42         private final Core core;
43
44         /**
45          * Creates a new stats command.
46          *
47          * @param core
48          *              The core to operate on
49          */
50         public StatsCommand(Core core) {
51                 this.core = core;
52         }
53
54         //
55         // COMMAND METHODS
56         //
57
58         @Override
59         public String getName() {
60                 return "stats";
61         }
62
63         @Override
64         public Collection<String> getAliases() {
65                 return emptyList();
66         }
67
68         @Override
69         public State execute(State state, List<String> parameters, Writer outputWriter) throws IOException {
70                 int configuredChannelsCount = core.channels().size();
71                 int joinedChannelsCount = core.joinedChannels().size();
72                 int extraChannelsCount = core.extraChannels().size();
73                 Collection<Bot> bots = core.bots();
74                 Set<String> packNames = Sets.newHashSet();
75                 int packsCount = 0;
76                 for (Bot bot : bots) {
77                         packsCount += bot.packs().size();
78                         for (Pack pack : bot) {
79                                 packNames.add(pack.name());
80                         }
81                 }
82
83                 outputWriter.write(String.format("%d channels (%d joined, %d extra), %d bots offering %d packs (%d unique).\n", configuredChannelsCount, joinedChannelsCount, extraChannelsCount, bots.size(), packsCount, packNames.size()));
84                 outputWriter.flush();
85                 return state;
86         }
87
88 }