Add predicate that identifies running downloads.
[xudocci.git] / src / main / java / net / pterodactylus / xdcc / data / Download.java
1 /*
2  * XdccDownloader - Download.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.data;
19
20 import static com.google.common.base.Preconditions.checkNotNull;
21
22 import java.io.OutputStream;
23 import java.net.InetAddress;
24
25 import net.pterodactylus.irc.DccReceiver;
26
27 import com.google.common.base.Predicate;
28
29 /**
30  * Information about an ongoing download.
31  *
32  * @author <a href="mailto:bombe@pterodactylus.net">David ‘Bombe’ Roden</a>
33  */
34 public class Download {
35
36         /** Predicate that identifies downloads that have started. */
37         public static final Predicate<Download> FILTER_RUNNING = new Predicate<Download>() {
38
39                 @Override
40                 public boolean apply(Download download) {
41                         return download.dccReceiver() != null;
42                 }
43         };
44
45         /** The bot that is being downloaded from. */
46         private final Bot bot;
47
48         /** The pack that is being downloaded. */
49         private final Pack pack;
50
51         /** The name of the file being downloaded. */
52         private String filename;
53
54         /** The size of the file being downloaded. */
55         private long filesize;
56
57         /** The remote address. */
58         private InetAddress remoteAddress;
59
60         /** The output stream. */
61         private OutputStream outputStream;
62
63         /** The DCC receiver. */
64         private DccReceiver dccReceiver;
65
66         /**
67          * Creates a new download.
68          *
69          * @param bot
70          *              The bot offering the download
71          * @param pack
72          *              The pack being downloaded
73          */
74         public Download(Bot bot, Pack pack) {
75                 this.bot = checkNotNull(bot, "bot must not be null");
76                 this.pack = checkNotNull(pack, "pack must not be null");
77         }
78
79         //
80         // ACCESSORS
81         //
82
83         /**
84          * Returns the bot offering the download.
85          *
86          * @return The bot offering the download
87          */
88         public Bot bot() {
89                 return bot;
90         }
91
92         /**
93          * The pack that is being downloaded.
94          *
95          * @return The pack being downloaded
96          */
97         public Pack pack() {
98                 return pack;
99         }
100
101         /**
102          * The full name of the file being written.
103          *
104          * @return The full name of the file
105          */
106         public String filename() {
107                 return filename;
108         }
109
110         /**
111          * Returns the size of the file.
112          *
113          * @return The size of the file
114          */
115         public long filesize() {
116                 return filesize;
117         }
118
119         /**
120          * Returns the remote address to download from.
121          *
122          * @return The remote address to download from
123          */
124         public InetAddress remoteAddress() {
125                 return remoteAddress;
126         }
127
128         /**
129          * The output stream that writes to the file.
130          *
131          * @return The output stream
132          */
133         public OutputStream outputStream() {
134                 return outputStream;
135         }
136
137         /**
138          * The DCC receiver that is downloading the file
139          *
140          * @return The DCC receiver
141          */
142         public DccReceiver dccReceiver() {
143                 return dccReceiver;
144         }
145
146         //
147         // MUTATORS
148         //
149
150         /**
151          * Sets the full name of the file being downloaded.
152          *
153          * @param filename
154          *              The full name of the file
155          * @return This download
156          */
157         public Download filename(String filename) {
158                 this.filename = filename;
159                 return this;
160         }
161
162         /**
163          * Sets the size of the download.
164          *
165          * @param filesize
166          *              The size of the download
167          * @return This download
168          */
169         public Download filesize(long filesize) {
170                 this.filesize = filesize;
171                 return this;
172         }
173
174         /**
175          * Sets the remote address of the download.
176          *
177          * @param remoteAddress
178          *              The remote address of the download
179          * @return This download
180          */
181         public Download remoteAddress(InetAddress remoteAddress) {
182                 this.remoteAddress = remoteAddress;
183                 return this;
184         }
185
186         /**
187          * Sets the output stream the download is being written to
188          *
189          * @param outputStream
190          *              The output stream
191          * @return This download
192          */
193         public Download outputStream(OutputStream outputStream) {
194                 this.outputStream = outputStream;
195                 return this;
196         }
197
198         /**
199          * Sets the DCC receiver that downloads the file.
200          *
201          * @param dccReceiver
202          *              The DCC receiver
203          * @return This download
204          */
205         public Download dccReceiver(DccReceiver dccReceiver) {
206                 this.dccReceiver = dccReceiver;
207                 return this;
208         }
209
210         //
211         // OBJECT METHODS
212         //
213
214         @Override
215         public boolean equals(Object object) {
216                 if (!(object instanceof Download)) {
217                         return false;
218                 }
219                 Download download = (Download) object;
220                 return bot().equals(download.bot()) && pack().equals(download.pack());
221         }
222
223         @Override
224         public int hashCode() {
225                 return bot().hashCode() ^ pack().hashCode();
226         }
227
228 }