929c2f32ff233f477683fed2c655ea4c517be81e
[xudocci.git] / src / main / java / net / pterodactylus / xdcc / ui / stdin / FailedDownloadsCommand.java
1 /*
2  * XdccDownloader - FailedDownloadsCommand.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.lang.String.format;
21 import static java.util.Collections.emptySet;
22
23 import java.io.IOException;
24 import java.io.Writer;
25 import java.util.ArrayList;
26 import java.util.Collection;
27 import java.util.List;
28
29 import net.pterodactylus.xdcc.data.Download;
30
31 /**
32  * TODO
33  *
34  * @author <a href="mailto:bombe@pterodactylus.net">David ‘Bombe’ Roden</a>
35  */
36 public class FailedDownloadsCommand implements Command {
37
38         private final DownloadFailures downloadFailures;
39
40         public FailedDownloadsCommand(DownloadFailures downloadFailures) {
41                 this.downloadFailures = downloadFailures;
42         }
43
44         @Override
45         public String getName() {
46                 return "failed";
47         }
48
49         @Override
50         public Collection<String> getAliases() {
51                 return emptySet();
52         }
53
54         @Override
55         public State execute(State state, List<String> parameters, Writer outputWriter) throws IOException {
56                 int downloadIndex = 0;
57                 List<Download> failedDownloads = new ArrayList<>();
58                 for (DownloadFailure downloadFailure : downloadFailures) {
59                         Download download = downloadFailure.getDownload();
60                         failedDownloads.add(download);
61                         outputWriter.write(format("[%d] %s from %s\n", downloadIndex, download.filename(), download.bot().name()));
62                         downloadIndex++;
63                 }
64                 outputWriter.write("End of failed downloads.\n");
65                 return state.setLastFailedDownloads(failedDownloads);
66         }
67
68 }