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;
25 import net.pterodactylus.irc.DccReceiver;
27 import com.google.common.base.Predicate;
30 * Information about an ongoing download.
32 * @author <a href="mailto:bombe@pterodactylus.net">David ‘Bombe’ Roden</a>
34 public class Download {
36 /** Predicate that identifies downloads that have started. */
37 public static final Predicate<Download> FILTER_RUNNING = new Predicate<Download>() {
40 public boolean apply(Download download) {
41 return download.dccReceiver() != null;
45 /** The bot that is being downloaded from. */
46 private final Bot bot;
48 /** The pack that is being downloaded. */
49 private final Pack pack;
51 /** The name of the file being downloaded. */
52 private String filename;
54 /** The size of the file being downloaded. */
55 private long filesize;
57 /** The remote address. */
58 private InetAddress remoteAddress;
60 /** The output stream. */
61 private OutputStream outputStream;
63 /** The DCC receiver. */
64 private DccReceiver dccReceiver;
67 * Creates a new download.
70 * The bot offering the download
72 * The pack being downloaded
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");
84 * Returns the bot offering the download.
86 * @return The bot offering the download
93 * The pack that is being downloaded.
95 * @return The pack being downloaded
102 * The full name of the file being written.
104 * @return The full name of the file
106 public String filename() {
111 * Returns the size of the file.
113 * @return The size of the file
115 public long filesize() {
120 * Returns the remote address to download from.
122 * @return The remote address to download from
124 public InetAddress remoteAddress() {
125 return remoteAddress;
129 * The output stream that writes to the file.
131 * @return The output stream
133 public OutputStream outputStream() {
138 * The DCC receiver that is downloading the file
140 * @return The DCC receiver
142 public DccReceiver dccReceiver() {
151 * Sets the full name of the file being downloaded.
154 * The full name of the file
155 * @return This download
157 public Download filename(String filename) {
158 this.filename = filename;
163 * Sets the size of the download.
166 * The size of the download
167 * @return This download
169 public Download filesize(long filesize) {
170 this.filesize = filesize;
175 * Sets the remote address of the download.
177 * @param remoteAddress
178 * The remote address of the download
179 * @return This download
181 public Download remoteAddress(InetAddress remoteAddress) {
182 this.remoteAddress = remoteAddress;
187 * Sets the output stream the download is being written to
189 * @param outputStream
191 * @return This download
193 public Download outputStream(OutputStream outputStream) {
194 this.outputStream = outputStream;
199 * Sets the DCC receiver that downloads the file.
203 * @return This download
205 public Download dccReceiver(DccReceiver dccReceiver) {
206 this.dccReceiver = dccReceiver;
215 public boolean equals(Object object) {
216 if (!(object instanceof Download)) {
219 Download download = (Download) object;
220 return bot().equals(download.bot()) && pack().equals(download.pack());
224 public int hashCode() {
225 return bot().hashCode() ^ pack().hashCode();