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.Comparator;
25 import java.util.concurrent.atomic.AtomicLong;
26 import java.util.concurrent.atomic.AtomicReference;
28 import net.pterodactylus.irc.DccReceiver;
30 import com.google.common.base.Predicate;
33 * Information about an ongoing download.
35 * @author <a href="mailto:bombe@pterodactylus.net">David ‘Bombe’ Roden</a>
37 public class Download {
39 /** Predicate that identifies downloads that have started. */
40 public static final Predicate<Download> FILTER_RUNNING = new Predicate<Download>() {
43 public boolean apply(Download download) {
44 return download.dccReceiver() != null;
48 /** {@link Comparator} that sorts downloads by their name. */
49 public static final Comparator<Download> BY_NAME = new Comparator<Download>() {
52 public int compare(Download leftDownload, Download rightDownload) {
53 return leftDownload.pack().name().compareToIgnoreCase(rightDownload.pack().name());
57 /** {@link Comparator} that sorts running downloads first. */
58 public static final Comparator<Download> BY_RUNNING = new Comparator<Download>() {
61 public int compare(Download leftDownload, Download rightDownload) {
62 return (leftDownload.dccReceiver() != null) ? -1 : (rightDownload.dccReceiver() != null) ? 1 : 0;
66 /** The bot that is being downloaded from. */
67 private final Bot bot;
69 /** The pack that is being downloaded. */
70 private final Pack pack;
72 /** The name of the file being downloaded. */
73 private final AtomicReference<String> filename = new AtomicReference<String>();
75 /** The size of the file being downloaded. */
76 private final AtomicLong filesize = new AtomicLong();
78 /** The remote address. */
79 private final AtomicReference<InetAddress> remoteAddress = new AtomicReference<InetAddress>();
81 /** The output stream. */
82 private final AtomicReference<OutputStream> outputStream = new AtomicReference<OutputStream>();
84 /** The DCC receiver. */
85 private final AtomicReference<DccReceiver> dccReceiver = new AtomicReference<DccReceiver>();
88 * Creates a new download.
91 * The bot offering the download
93 * The pack being downloaded
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");
105 * Returns the bot offering the download.
107 * @return The bot offering the download
114 * The pack that is being downloaded.
116 * @return The pack being downloaded
123 * The full name of the file being written.
125 * @return The full name of the file
127 public String filename() {
128 return filename.get();
132 * Returns the size of the file.
134 * @return The size of the file
136 public long filesize() {
137 return filesize.get();
141 * Returns the remote address to download from.
143 * @return The remote address to download from
145 public InetAddress remoteAddress() {
146 return remoteAddress.get();
150 * The output stream that writes to the file.
152 * @return The output stream
154 public OutputStream outputStream() {
155 return outputStream.get();
159 * The DCC receiver that is downloading the file
161 * @return The DCC receiver
163 public DccReceiver dccReceiver() {
164 return dccReceiver.get();
172 * Sets the full name of the file being downloaded.
175 * The full name of the file
176 * @return This download
178 public Download filename(String filename) {
179 this.filename.set(filename);
184 * Sets the size of the download.
187 * The size of the download
188 * @return This download
190 public Download filesize(long filesize) {
191 this.filesize.set(filesize);
196 * Sets the remote address of the download.
198 * @param remoteAddress
199 * The remote address of the download
200 * @return This download
202 public Download remoteAddress(InetAddress remoteAddress) {
203 this.remoteAddress.set(remoteAddress);
208 * Sets the output stream the download is being written to
210 * @param outputStream
212 * @return This download
214 public Download outputStream(OutputStream outputStream) {
215 this.outputStream.set(outputStream);
220 * Sets the DCC receiver that downloads the file.
224 * @return This download
226 public Download dccReceiver(DccReceiver dccReceiver) {
227 this.dccReceiver.set(dccReceiver);
236 public boolean equals(Object object) {
237 if (!(object instanceof Download)) {
240 Download download = (Download) object;
241 return bot().equals(download.bot()) && pack().equals(download.pack());
245 public int hashCode() {
246 return bot().hashCode() ^ pack().hashCode();