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