2 * XdccDownloader - StatsCommand.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 static java.lang.String.format;
21 import static java.util.Collections.emptyList;
23 import java.io.IOException;
24 import java.io.Writer;
25 import java.util.Collection;
26 import java.util.List;
27 import java.util.StringJoiner;
29 import net.pterodactylus.xdcc.core.Core;
30 import net.pterodactylus.xdcc.data.ConnectedNetwork;
33 * Command that outputs a short statistic of what is going on.
35 * @author <a href="mailto:bombe@pterodactylus.net">David ‘Bombe’ Roden</a>
37 public class StatsCommand implements Command {
39 private final Core core;
41 public StatsCommand(Core core) {
46 public String getName() {
51 public Collection<String> getAliases() {
56 public State execute(State state, List<String> parameters, Writer outputWriter) throws IOException {
57 int totalBotCount = 0;
58 int totalPackCount = 0;
59 for (ConnectedNetwork network : core.connectedNetworks()) {
60 dumpNetworkStats(outputWriter, network);
61 totalBotCount += network.getBotCount();
62 totalPackCount += network.getPackCount();
64 outputWriter.write(format("Total: %d bots, %d packs.\n", totalBotCount, totalPackCount));
68 private void dumpNetworkStats(Writer outputWriter,
69 ConnectedNetwork network) throws IOException {
70 outputWriter.write(format("Connected to %s via %s:%d.\n",
71 network.getNetwork().name(), network.getHostname(),
72 network.getPort(), network.getNickname()));
73 String joinedChannels = joinChannelNames(network.getChannels());
74 String forcedChannels = joinChannelNames(network.getForcedChannels());
75 if (forcedChannels.isEmpty()) {
76 if (!joinedChannels.isEmpty()) {
77 outputWriter.write(format(" Joined %s.\n", joinedChannels));
80 if (joinedChannels.isEmpty()) {
82 format(" Force-joined %s.\n", forcedChannels));
84 outputWriter.write(format(" Joined %s, force-joined %s.\n",
85 joinedChannels, forcedChannels));
89 format(" %d bots serving %d packs.\n", network.getBotCount(),
90 network.getPackCount()));
93 private String joinChannelNames(Collection<String> channels) {
94 StringJoiner joinedChannels = new StringJoiner(", ");
95 channels.stream().forEach((channel) -> joinedChannels.add(channel));
96 return joinedChannels.toString();