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