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