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