Fix ALL the logging!
[Sone.git] / src / main / java / net / pterodactylus / sone / data / Image.java
1 /*
2  * Sone - Image.java - Copyright © 2011 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.sone.data;
19
20 import java.util.UUID;
21
22 import net.pterodactylus.util.validation.Validation;
23
24 /**
25  * Container for image metadata.
26  *
27  * @author <a href="mailto:bombe@pterodactylus.net">David ‘Bombe’ Roden</a>
28  */
29 public class Image implements Fingerprintable {
30
31         /** The ID of the image. */
32         private final String id;
33
34         /** The Sone the image belongs to. */
35         private Sone sone;
36
37         /** The album this image belongs to. */
38         private Album album;
39
40         /** The request key of the image. */
41         private String key;
42
43         /** The creation time of the image. */
44         private long creationTime;
45
46         /** The width of the image. */
47         private int width;
48
49         /** The height of the image. */
50         private int height;
51
52         /** The title of the image. */
53         private String title;
54
55         /** The description of the image. */
56         private String description;
57
58         /**
59          * Creates a new image with a random ID.
60          */
61         public Image() {
62                 this(UUID.randomUUID().toString());
63                 setCreationTime(System.currentTimeMillis());
64         }
65
66         /**
67          * Creates a new image.
68          *
69          * @param id
70          *            The ID of the image
71          */
72         public Image(String id) {
73                 Validation.begin().isNotNull("Image ID", id).check();
74                 this.id = id;
75         }
76
77         //
78         // ACCESSORS
79         //
80
81         /**
82          * Returns the ID of this image.
83          *
84          * @return The ID of this image
85          */
86         public String getId() {
87                 return id;
88         }
89
90         /**
91          * Returns the Sone this image belongs to.
92          *
93          * @return The Sone this image belongs to
94          */
95         public Sone getSone() {
96                 return sone;
97         }
98
99         /**
100          * Sets the owner of this image. The owner can only be set if no owner has
101          * yet been set.
102          *
103          * @param sone
104          *            The new owner of this image
105          * @return This image
106          */
107         public Image setSone(Sone sone) {
108                 Validation.begin().isNotNull("New Image Owner", sone).isEither("Old Image Owner", this.sone, null, sone).check();
109                 this.sone = sone;
110                 return this;
111         }
112
113         /**
114          * Returns the album this image belongs to.
115          *
116          * @return The album this image belongs to
117          */
118         public Album getAlbum() {
119                 return album;
120         }
121
122         /**
123          * Sets the album this image belongs to. The album of an image can only be
124          * set once, and it is usually called by {@link Album#addImage(Image)}.
125          *
126          * @param album
127          *            The album this image belongs to
128          * @return This image
129          */
130         public Image setAlbum(Album album) {
131                 Validation.begin().isNotNull("New Album", album).check().isEqual("Album Owner and Image Owner", album.getSone(), getSone()).check();
132                 this.album = album;
133                 return this;
134         }
135
136         /**
137          * Returns the request key of this image.
138          *
139          * @return The request key of this image
140          */
141         public String getKey() {
142                 return key;
143         }
144
145         /**
146          * Sets the request key of this image. The request key can only be set as
147          * long as no request key has yet been set.
148          *
149          * @param key
150          *            The new request key of this image
151          * @return This image
152          */
153         public Image setKey(String key) {
154                 Validation.begin().isNotNull("New Image Key", key).isEither("Old Image Key", this.key, null, key).check();
155                 this.key = key;
156                 return this;
157         }
158
159         /**
160          * Returns whether the image has already been inserted. An image is
161          * considered as having been inserted it its {@link #getKey() key} is not
162          * {@code null}.
163          *
164          * @return {@code true} if there is a key for this image, {@code false}
165          *         otherwise
166          */
167         public boolean isInserted() {
168                 return key != null;
169         }
170
171         /**
172          * Returns the creation time of this image.
173          *
174          * @return The creation time of this image (in milliseconds since 1970, Jan
175          *         1, UTC)
176          */
177         public long getCreationTime() {
178                 return creationTime;
179         }
180
181         /**
182          * Sets the new creation time of this image. The creation time can only be
183          * set as long as no creation time has been set yet.
184          *
185          * @param creationTime
186          *            The new creation time of this image
187          * @return This image
188          */
189         public Image setCreationTime(long creationTime) {
190                 Validation.begin().isGreater("New Image Creation Time", creationTime, 0).isEither("Old Image Creation Time", this.creationTime, 0L, creationTime).check();
191                 this.creationTime = creationTime;
192                 return this;
193         }
194
195         /**
196          * Returns the width of this image.
197          *
198          * @return The width of this image (in pixels)
199          */
200         public int getWidth() {
201                 return width;
202         }
203
204         /**
205          * Sets the width of this image. The width can only be set as long as no
206          * width has been set yet.
207          *
208          * @param width
209          *            The new width of this image
210          * @return This image
211          */
212         public Image setWidth(int width) {
213                 Validation.begin().isGreater("New Image Width", width, 0).isEither("Old Image Width", this.width, 0, width).check();
214                 this.width = width;
215                 return this;
216         }
217
218         /**
219          * Returns the height of this image.
220          *
221          * @return The height of this image (in pixels)
222          */
223         public int getHeight() {
224                 return height;
225         }
226
227         /**
228          * Sets the new height of this image. The height can only be set as long as
229          * no height has yet been set.
230          *
231          * @param height
232          *            The new height of this image
233          * @return This image
234          */
235         public Image setHeight(int height) {
236                 Validation.begin().isGreater("New Image Height", height, 0).isEither("Old Image Height", this.height, 0, height).check();
237                 this.height = height;
238                 return this;
239         }
240
241         /**
242          * Returns the title of this image.
243          *
244          * @return The title of this image
245          */
246         public String getTitle() {
247                 return title;
248         }
249
250         /**
251          * Sets the title of this image.
252          *
253          * @param title
254          *            The title of this image
255          * @return This image
256          */
257         public Image setTitle(String title) {
258                 Validation.begin().isNotNull("Image Title", title).check();
259                 this.title = title;
260                 return this;
261         }
262
263         /**
264          * Returns the description of this image.
265          *
266          * @return The description of this image
267          */
268         public String getDescription() {
269                 return description;
270         }
271
272         /**
273          * Sets the description of this image.
274          *
275          * @param description
276          *            The description of this image
277          * @return This image
278          */
279         public Image setDescription(String description) {
280                 Validation.begin().isNotNull("Image Description", description).check();
281                 this.description = description;
282                 return this;
283         }
284
285         //
286         // FINGERPRINTABLE METHODS
287         //
288
289         /**
290          * {@inheritDoc}
291          */
292         @Override
293         public String getFingerprint() {
294                 StringBuilder fingerprint = new StringBuilder();
295                 fingerprint.append("Image(");
296                 fingerprint.append("ID(").append(id).append(')');
297                 fingerprint.append("Title(").append(title).append(')');
298                 fingerprint.append("Description(").append(description).append(')');
299                 fingerprint.append(')');
300                 return fingerprint.toString();
301         }
302
303         //
304         // OBJECT METHODS
305         //
306
307         /**
308          * {@inheritDoc}
309          */
310         @Override
311         public int hashCode() {
312                 return id.hashCode();
313         }
314
315         /**
316          * {@inheritDoc}
317          */
318         @Override
319         public boolean equals(Object object) {
320                 if (!(object instanceof Image)) {
321                         return false;
322                 }
323                 return ((Image) object).id.equals(id);
324         }
325
326 }