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