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