Use SHA-256 hashes for fingerprinting.
[Sone.git] / src / main / java / net / pterodactylus / sone / data / Album.java
1 /*
2  * Sone - Album.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.ArrayList;
25 import java.util.Comparator;
26 import java.util.HashMap;
27 import java.util.List;
28 import java.util.Map;
29 import java.util.UUID;
30
31 import com.google.common.base.Function;
32 import com.google.common.base.Optional;
33 import com.google.common.base.Predicates;
34 import com.google.common.collect.Collections2;
35 import com.google.common.hash.Hasher;
36 import com.google.common.hash.Hashing;
37
38 /**
39  * Container for images that can also contain nested {@link Album}s.
40  *
41  * @author <a href="mailto:bombe@pterodactylus.net">David ‘Bombe’ Roden</a>
42  */
43 public class Album implements Fingerprintable {
44
45         /** Compares two {@link Album}s by {@link #getTitle()}. */
46         public static final Comparator<Album> TITLE_COMPARATOR = new Comparator<Album>() {
47
48                 @Override
49                 public int compare(Album leftAlbum, Album rightAlbum) {
50                         return leftAlbum.getTitle().compareToIgnoreCase(rightAlbum.getTitle());
51                 }
52         };
53
54         /** The ID of this album. */
55         private final String id;
56
57         /** The Sone this album belongs to. */
58         private Sone sone;
59
60         /** Nested albums. */
61         private final List<Album> albums = new ArrayList<Album>();
62
63         /** The image IDs in order. */
64         private final List<String> imageIds = new ArrayList<String>();
65
66         /** The images in this album. */
67         private final Map<String, Image> images = new HashMap<String, Image>();
68
69         /** The parent album. */
70         private Album parent;
71
72         /** The title of this album. */
73         private String title;
74
75         /** The description of this album. */
76         private String description;
77
78         /** The ID of the album picture. */
79         private String albumImage;
80
81         /**
82          * Creates a new album with a random ID.
83          */
84         public Album() {
85                 this(UUID.randomUUID().toString());
86         }
87
88         /**
89          * Creates a new album with the given ID.
90          *
91          * @param id
92          *            The ID of the album
93          */
94         public Album(String id) {
95                 this.id = checkNotNull(id, "id must not be null");
96         }
97
98         //
99         // ACCESSORS
100         //
101
102         /**
103          * Returns the ID of this album.
104          *
105          * @return The ID of this album
106          */
107         public String getId() {
108                 return id;
109         }
110
111         /**
112          * Returns the Sone this album belongs to.
113          *
114          * @return The Sone this album belongs to
115          */
116         public Sone getSone() {
117                 return sone;
118         }
119
120         /**
121          * Sets the owner of the album. The owner can only be set as long as the
122          * current owner is {@code null}.
123          *
124          * @param sone
125          *            The album owner
126          * @return This album
127          */
128         public Album setSone(Sone sone) {
129                 checkNotNull(sone, "sone must not be null");
130                 checkState((this.sone == null) || (this.sone.equals(sone)), "album owner must not already be set to some other Sone");
131                 this.sone = sone;
132                 return this;
133         }
134
135         /**
136          * Returns the nested albums.
137          *
138          * @return The nested albums
139          */
140         public List<Album> getAlbums() {
141                 return new ArrayList<Album>(albums);
142         }
143
144         /**
145          * Adds an album to this album.
146          *
147          * @param album
148          *            The album to add
149          */
150         public void addAlbum(Album album) {
151                 checkNotNull(album, "album must not be null");
152                 checkArgument(album.getSone().equals(sone), "album must belong to the same Sone as this album");
153                 checkState((this.parent == null) || (this.parent.equals(album.parent)), "album must not already be set to some other Sone");
154                 album.setParent(this);
155                 if (!albums.contains(album)) {
156                         albums.add(album);
157                 }
158         }
159
160         /**
161          * Removes an album from this album.
162          *
163          * @param album
164          *            The album to remove
165          */
166         public void removeAlbum(Album album) {
167                 checkNotNull(album, "album must not be null");
168                 checkArgument(album.sone.equals(sone), "album must belong this album’s Sone");
169                 checkArgument(equals(album.parent), "album must belong to this album");
170                 albums.remove(album);
171                 album.removeParent();
172         }
173
174         /**
175          * Moves the given album up in this album’s albums. If the album is already
176          * the first album, nothing happens.
177          *
178          * @param album
179          *            The album to move up
180          * @return The album that the given album swapped the place with, or
181          *         <code>null</code> if the album did not change its place
182          */
183         public Album moveAlbumUp(Album album) {
184                 checkNotNull(album, "album must not be null");
185                 checkArgument(album.sone.equals(sone), "album must belong to the same Sone as this album");
186                 checkArgument(equals(album.parent), "album must belong to this album");
187                 int oldIndex = albums.indexOf(album);
188                 if (oldIndex <= 0) {
189                         return null;
190                 }
191                 albums.remove(oldIndex);
192                 albums.add(oldIndex - 1, album);
193                 return albums.get(oldIndex);
194         }
195
196         /**
197          * Moves the given album down in this album’s albums. If the album is
198          * already the last album, nothing happens.
199          *
200          * @param album
201          *            The album to move down
202          * @return The album that the given album swapped the place with, or
203          *         <code>null</code> if the album did not change its place
204          */
205         public Album moveAlbumDown(Album album) {
206                 checkNotNull(album, "album must not be null");
207                 checkArgument(album.sone.equals(sone), "album must belong to the same Sone as this album");
208                 checkArgument(equals(album.parent), "album must belong to this album");
209                 int oldIndex = albums.indexOf(album);
210                 if ((oldIndex < 0) || (oldIndex >= (albums.size() - 1))) {
211                         return null;
212                 }
213                 albums.remove(oldIndex);
214                 albums.add(oldIndex + 1, album);
215                 return albums.get(oldIndex);
216         }
217
218         /**
219          * Returns the images in this album.
220          *
221          * @return The images in this album
222          */
223         public List<Image> getImages() {
224                 return new ArrayList<Image>(Collections2.filter(Collections2.transform(imageIds, new Function<String, Image>() {
225
226                         @Override
227                         @SuppressWarnings("synthetic-access")
228                         public Image apply(String imageId) {
229                                 return images.get(imageId);
230                         }
231                 }), Predicates.notNull()));
232         }
233
234         /**
235          * Adds the given image to this album.
236          *
237          * @param image
238          *            The image to add
239          */
240         public void addImage(Image image) {
241                 checkNotNull(image, "image must not be null");
242                 checkNotNull(image.getSone(), "image must have an owner");
243                 checkArgument(image.getSone().equals(sone), "image must belong to the same Sone as this album");
244                 if (image.getAlbum() != null) {
245                         image.getAlbum().removeImage(image);
246                 }
247                 image.setAlbum(this);
248                 if (imageIds.isEmpty() && (albumImage == null)) {
249                         albumImage = image.getId();
250                 }
251                 if (!imageIds.contains(image.getId())) {
252                         imageIds.add(image.getId());
253                         images.put(image.getId(), image);
254                 }
255         }
256
257         /**
258          * Removes the given image from this album.
259          *
260          * @param image
261          *            The image to remove
262          */
263         public void removeImage(Image image) {
264                 checkNotNull(image, "image must not be null");
265                 checkNotNull(image.getSone(), "image must have an owner");
266                 checkArgument(image.getSone().equals(sone), "image must belong to the same Sone as this album");
267                 imageIds.remove(image.getId());
268                 images.remove(image.getId());
269                 if (image.getId().equals(albumImage)) {
270                         if (images.isEmpty()) {
271                                 albumImage = null;
272                         } else {
273                                 albumImage = images.values().iterator().next().getId();
274                         }
275                 }
276         }
277
278         /**
279          * Moves the given image up in this album’s images. If the image is already
280          * the first image, nothing happens.
281          *
282          * @param image
283          *            The image to move up
284          * @return The image that the given image swapped the place with, or
285          *         <code>null</code> if the image did not change its place
286          */
287         public Image moveImageUp(Image image) {
288                 checkNotNull(image, "image must not be null");
289                 checkNotNull(image.getSone(), "image must have an owner");
290                 checkArgument(image.getSone().equals(sone), "image must belong to the same Sone as this album");
291                 checkArgument(image.getAlbum().equals(this), "image must belong to this album");
292                 int oldIndex = imageIds.indexOf(image.getId());
293                 if (oldIndex <= 0) {
294                         return null;
295                 }
296                 imageIds.remove(image.getId());
297                 imageIds.add(oldIndex - 1, image.getId());
298                 return images.get(imageIds.get(oldIndex));
299         }
300
301         /**
302          * Moves the given image down in this album’s images. If the image is
303          * already the last image, nothing happens.
304          *
305          * @param image
306          *            The image to move down
307          * @return The image that the given image swapped the place with, or
308          *         <code>null</code> if the image did not change its place
309          */
310         public Image moveImageDown(Image image) {
311                 checkNotNull(image, "image must not be null");
312                 checkNotNull(image.getSone(), "image must have an owner");
313                 checkArgument(image.getSone().equals(sone), "image must belong to the same Sone as this album");
314                 checkArgument(image.getAlbum().equals(this), "image must belong to this album");
315                 int oldIndex = imageIds.indexOf(image.getId());
316                 if ((oldIndex == -1) || (oldIndex >= (imageIds.size() - 1))) {
317                         return null;
318                 }
319                 imageIds.remove(image.getId());
320                 imageIds.add(oldIndex + 1, image.getId());
321                 return images.get(imageIds.get(oldIndex));
322         }
323
324         /**
325          * Returns the album image of this album, or {@code null} if no album image
326          * has been set.
327          *
328          * @return The image to show when this album is listed
329          */
330         public Image getAlbumImage() {
331                 if (albumImage == null) {
332                         return null;
333                 }
334                 return Optional.fromNullable(images.get(albumImage)).or(images.values().iterator().next());
335         }
336
337         /**
338          * Sets the ID of the album image.
339          *
340          * @param id
341          *            The ID of the album image
342          * @return This album
343          */
344         public Album setAlbumImage(String id) {
345                 this.albumImage = id;
346                 return this;
347         }
348
349         /**
350          * Returns whether this album contains any other albums or images.
351          *
352          * @return {@code true} if this album is empty, {@code false} otherwise
353          */
354         public boolean isEmpty() {
355                 return albums.isEmpty() && images.isEmpty();
356         }
357
358         /**
359          * Returns the parent album of this album.
360          *
361          * @return The parent album of this album, or {@code null} if this album
362          *         does not have a parent
363          */
364         public Album getParent() {
365                 return parent;
366         }
367
368         /**
369          * Sets the parent album of this album.
370          *
371          * @param parent
372          *            The new parent album of this album
373          * @return This album
374          */
375         protected Album setParent(Album parent) {
376                 this.parent = checkNotNull(parent, "parent must not be null");
377                 return this;
378         }
379
380         /**
381          * Removes the parent album of this album.
382          *
383          * @return This album
384          */
385         protected Album removeParent() {
386                 this.parent = null;
387                 return this;
388         }
389
390         /**
391          * Returns the title of this album.
392          *
393          * @return The title of this album
394          */
395         public String getTitle() {
396                 return title;
397         }
398
399         /**
400          * Sets the title of this album.
401          *
402          * @param title
403          *            The title of this album
404          * @return This album
405          */
406         public Album setTitle(String title) {
407                 this.title = checkNotNull(title, "title must not be null");
408                 return this;
409         }
410
411         /**
412          * Returns the description of this album.
413          *
414          * @return The description of this album
415          */
416         public String getDescription() {
417                 return description;
418         }
419
420         /**
421          * Sets the description of this album.
422          *
423          * @param description
424          *            The description of this album
425          * @return This album
426          */
427         public Album setDescription(String description) {
428                 this.description = checkNotNull(description, "description must not be null");
429                 return this;
430         }
431
432         //
433         // FINGERPRINTABLE METHODS
434         //
435
436         /**
437          * {@inheritDoc}
438          */
439         @Override
440         public String getFingerprint() {
441                 Hasher hash = Hashing.sha256().newHasher();
442                 hash.putString("Album(");
443                 hash.putString("ID(").putString(id).putString(")");
444                 hash.putString("Title(").putString(title).putString(")");
445                 hash.putString("Description(").putString(description).putString(")");
446                 if (albumImage != null) {
447                         hash.putString("AlbumImage(").putString(albumImage).putString(")");
448                 }
449
450                 /* add nested albums. */
451                 hash.putString("Albums(");
452                 for (Album album : albums) {
453                         hash.putString(album.getFingerprint());
454                 }
455                 hash.putString(")");
456
457                 /* add images. */
458                 hash.putString("Images(");
459                 for (Image image : getImages()) {
460                         if (image.isInserted()) {
461                                 hash.putString(image.getFingerprint());
462                         }
463                 }
464                 hash.putString(")");
465
466                 hash.putString(")");
467                 return hash.hash().toString();
468         }
469
470         //
471         // OBJECT METHODS
472         //
473
474         /**
475          * {@inheritDoc}
476          */
477         @Override
478         public int hashCode() {
479                 return id.hashCode();
480         }
481
482         /**
483          * {@inheritDoc}
484          */
485         @Override
486         public boolean equals(Object object) {
487                 if (!(object instanceof Album)) {
488                         return false;
489                 }
490                 Album album = (Album) object;
491                 return id.equals(album.id);
492         }
493
494 }