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