Replace utils’ filter with Guava’s predicate.
[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.object.Default;
28 import net.pterodactylus.util.validation.Validation;
29
30 import com.google.common.base.Function;
31 import com.google.common.base.Predicates;
32 import com.google.common.collect.Collections2;
33
34 /**
35  * Container for images that can also contain nested {@link Album}s.
36  *
37  * @author <a href="mailto:bombe@pterodactylus.net">David ‘Bombe’ Roden</a>
38  */
39 public class Album implements Fingerprintable {
40
41         /** Compares two {@link Album}s by {@link #getTitle()}. */
42         public static final Comparator<Album> TITLE_COMPARATOR = new Comparator<Album>() {
43
44                 @Override
45                 public int compare(Album leftAlbum, Album rightAlbum) {
46                         return leftAlbum.getTitle().compareToIgnoreCase(rightAlbum.getTitle());
47                 }
48         };
49
50         /** The ID of this album. */
51         private final String id;
52
53         /** The Sone this album belongs to. */
54         private Sone sone;
55
56         /** Nested albums. */
57         private final List<Album> albums = new ArrayList<Album>();
58
59         /** The image IDs in order. */
60         private final List<String> imageIds = new ArrayList<String>();
61
62         /** The images in this album. */
63         private final Map<String, Image> images = new HashMap<String, Image>();
64
65         /** The parent album. */
66         private Album parent;
67
68         /** The title of this album. */
69         private String title;
70
71         /** The description of this album. */
72         private String description;
73
74         /** The ID of the album picture. */
75         private String albumImage;
76
77         /**
78          * Creates a new album with a random ID.
79          */
80         public Album() {
81                 this(UUID.randomUUID().toString());
82         }
83
84         /**
85          * Creates a new album with the given ID.
86          *
87          * @param id
88          *            The ID of the album
89          */
90         public Album(String id) {
91                 Validation.begin().isNotNull("Album ID", id).check();
92                 this.id = id;
93         }
94
95         //
96         // ACCESSORS
97         //
98
99         /**
100          * Returns the ID of this album.
101          *
102          * @return The ID of this album
103          */
104         public String getId() {
105                 return id;
106         }
107
108         /**
109          * Returns the Sone this album belongs to.
110          *
111          * @return The Sone this album belongs to
112          */
113         public Sone getSone() {
114                 return sone;
115         }
116
117         /**
118          * Sets the owner of the album. The owner can only be set as long as the
119          * current owner is {@code null}.
120          *
121          * @param sone
122          *            The album owner
123          * @return This album
124          */
125         public Album setSone(Sone sone) {
126                 Validation.begin().isNotNull("New Album Owner", sone).isEither("Old Album Owner", this.sone, null, sone).check();
127                 this.sone = sone;
128                 return this;
129         }
130
131         /**
132          * Returns the nested albums.
133          *
134          * @return The nested albums
135          */
136         public List<Album> getAlbums() {
137                 return new ArrayList<Album>(albums);
138         }
139
140         /**
141          * Adds an album to this album.
142          *
143          * @param album
144          *            The album to add
145          */
146         public void addAlbum(Album album) {
147                 Validation.begin().isNotNull("Album", album).check().isEqual("Album Owner", album.sone, sone).isEither("Old Album Parent", this.parent, null, album.parent).check();
148                 album.setParent(this);
149                 if (!albums.contains(album)) {
150                         albums.add(album);
151                 }
152         }
153
154         /**
155          * Removes an album from this album.
156          *
157          * @param album
158          *            The album to remove
159          */
160         public void removeAlbum(Album album) {
161                 Validation.begin().isNotNull("Album", album).check().isEqual("Album Owner", album.sone, sone).isEqual("Album Parent", album.parent, this).check();
162                 albums.remove(album);
163                 album.removeParent();
164         }
165
166         /**
167          * Moves the given album up in this album’s albums. If the album is already
168          * the first album, nothing happens.
169          *
170          * @param album
171          *            The album to move up
172          * @return The album that the given album swapped the place with, or
173          *         <code>null</code> if the album did not change its place
174          */
175         public Album moveAlbumUp(Album album) {
176                 Validation.begin().isNotNull("Album", album).check().isEqual("Album Owner", album.sone, sone).isEqual("Album Parent", album.parent, this).check();
177                 int oldIndex = albums.indexOf(album);
178                 if (oldIndex <= 0) {
179                         return null;
180                 }
181                 albums.remove(oldIndex);
182                 albums.add(oldIndex - 1, album);
183                 return albums.get(oldIndex);
184         }
185
186         /**
187          * Moves the given album down in this album’s albums. If the album is
188          * already the last album, nothing happens.
189          *
190          * @param album
191          *            The album to move down
192          * @return The album that the given album swapped the place with, or
193          *         <code>null</code> if the album did not change its place
194          */
195         public Album moveAlbumDown(Album album) {
196                 Validation.begin().isNotNull("Album", album).check().isEqual("Album Owner", album.sone, sone).isEqual("Album Parent", album.parent, this).check();
197                 int oldIndex = albums.indexOf(album);
198                 if ((oldIndex < 0) || (oldIndex >= (albums.size() - 1))) {
199                         return null;
200                 }
201                 albums.remove(oldIndex);
202                 albums.add(oldIndex + 1, album);
203                 return albums.get(oldIndex);
204         }
205
206         /**
207          * Returns the images in this album.
208          *
209          * @return The images in this album
210          */
211         public List<Image> getImages() {
212                 return new ArrayList<Image>(Collections2.filter(Collections2.transform(imageIds, new Function<String, Image>() {
213
214                         @Override
215                         @SuppressWarnings("synthetic-access")
216                         public Image apply(String imageId) {
217                                 return images.get(imageId);
218                         }
219                 }), Predicates.notNull()));
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 }