2 * XdccDownloader - Download.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.data;
20 import static com.google.common.base.Preconditions.checkNotNull;
22 import java.io.OutputStream;
23 import java.net.InetAddress;
24 import java.util.concurrent.atomic.AtomicLong;
25 import java.util.concurrent.atomic.AtomicReference;
27 import net.pterodactylus.irc.DccReceiver;
29 import com.google.common.base.Predicate;
32 * Information about an ongoing download.
34 * @author <a href="mailto:bombe@pterodactylus.net">David ‘Bombe’ Roden</a>
36 public class Download {
38 /** Predicate that identifies downloads that have started. */
39 public static final Predicate<Download> FILTER_RUNNING = new Predicate<Download>() {
42 public boolean apply(Download download) {
43 return download.dccReceiver() != null;
47 /** The bot that is being downloaded from. */
48 private final Bot bot;
50 /** The pack that is being downloaded. */
51 private final Pack pack;
53 /** The name of the file being downloaded. */
54 private final AtomicReference<String> filename = new AtomicReference<String>();
56 /** The size of the file being downloaded. */
57 private final AtomicLong filesize = new AtomicLong();
59 /** The remote address. */
60 private final AtomicReference<InetAddress> remoteAddress = new AtomicReference<InetAddress>();
62 /** The output stream. */
63 private final AtomicReference<OutputStream> outputStream = new AtomicReference<OutputStream>();
65 /** The DCC receiver. */
66 private final AtomicReference<DccReceiver> dccReceiver = new AtomicReference<DccReceiver>();
69 * Creates a new download.
72 * The bot offering the download
74 * The pack being downloaded
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");
86 * Returns the bot offering the download.
88 * @return The bot offering the download
95 * The pack that is being downloaded.
97 * @return The pack being downloaded
104 * The full name of the file being written.
106 * @return The full name of the file
108 public String filename() {
109 return filename.get();
113 * Returns the size of the file.
115 * @return The size of the file
117 public long filesize() {
118 return filesize.get();
122 * Returns the remote address to download from.
124 * @return The remote address to download from
126 public InetAddress remoteAddress() {
127 return remoteAddress.get();
131 * The output stream that writes to the file.
133 * @return The output stream
135 public OutputStream outputStream() {
136 return outputStream.get();
140 * The DCC receiver that is downloading the file
142 * @return The DCC receiver
144 public DccReceiver dccReceiver() {
145 return dccReceiver.get();
153 * Sets the full name of the file being downloaded.
156 * The full name of the file
157 * @return This download
159 public Download filename(String filename) {
160 this.filename.set(filename);
165 * Sets the size of the download.
168 * The size of the download
169 * @return This download
171 public Download filesize(long filesize) {
172 this.filesize.set(filesize);
177 * Sets the remote address of the download.
179 * @param remoteAddress
180 * The remote address of the download
181 * @return This download
183 public Download remoteAddress(InetAddress remoteAddress) {
184 this.remoteAddress.set(remoteAddress);
189 * Sets the output stream the download is being written to
191 * @param outputStream
193 * @return This download
195 public Download outputStream(OutputStream outputStream) {
196 this.outputStream.set(outputStream);
201 * Sets the DCC receiver that downloads the file.
205 * @return This download
207 public Download dccReceiver(DccReceiver dccReceiver) {
208 this.dccReceiver.set(dccReceiver);
217 public boolean equals(Object object) {
218 if (!(object instanceof Download)) {
221 Download download = (Download) object;
222 return bot().equals(download.bot()) && pack().equals(download.pack());
226 public int hashCode() {
227 return bot().hashCode() ^ pack().hashCode();