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