Ensure that an image is always set as album image, unless the album is empty.
[Sone.git] / src / main / java / net / pterodactylus / sone / data / Album.java
1 /*
2  * Sone - Album.java - Copyright © 2011 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.LinkedHashMap;
22 import java.util.List;
23 import java.util.Map;
24 import java.util.UUID;
25
26 import net.pterodactylus.util.validation.Validation;
27
28 /**
29  * Container for images that can also contain nested {@link Album}s.
30  *
31  * @author <a href="mailto:bombe@pterodactylus.net">David ‘Bombe’ Roden</a>
32  */
33 public class Album implements Fingerprintable {
34
35         /** The ID of this album. */
36         private final String id;
37
38         /** The Sone this album belongs to. */
39         private Sone sone;
40
41         /** Nested albums. */
42         private final List<Album> albums = new ArrayList<Album>();
43
44         /** The images in this album. */
45         private final Map<String, Image> images = new LinkedHashMap<String, Image>();
46
47         /** The parent album. */
48         private Album parent;
49
50         /** The title of this album. */
51         private String title;
52
53         /** The description of this album. */
54         private String description;
55
56         /** The ID of the album picture. */
57         private String albumImage;
58
59         /**
60          * Creates a new album with a random ID.
61          */
62         public Album() {
63                 this(UUID.randomUUID().toString());
64         }
65
66         /**
67          * Creates a new album with the given ID.
68          *
69          * @param id
70          *            The ID of the album
71          */
72         public Album(String id) {
73                 Validation.begin().isNotNull("Album ID", id).check();
74                 this.id = id;
75         }
76
77         //
78         // ACCESSORS
79         //
80
81         /**
82          * Returns the ID of this album.
83          *
84          * @return The ID of this album
85          */
86         public String getId() {
87                 return id;
88         }
89
90         /**
91          * Returns the Sone this album belongs to.
92          *
93          * @return The Sone this album belongs to
94          */
95         public Sone getSone() {
96                 return sone;
97         }
98
99         /**
100          * Sets the owner of the album. The owner can only be set as long as the
101          * current owner is {@code null}.
102          *
103          * @param sone
104          *            The album owner
105          * @return This album
106          */
107         public Album setSone(Sone sone) {
108                 Validation.begin().isNotNull("New Album Owner", sone).isEither("Old Album Owner", this.sone, null, sone).check();
109                 this.sone = sone;
110                 return this;
111         }
112
113         /**
114          * Returns the nested albums.
115          *
116          * @return The nested albums
117          */
118         public List<Album> getAlbums() {
119                 return new ArrayList<Album>(albums);
120         }
121
122         /**
123          * Adds an album to this album.
124          *
125          * @param album
126          *            The album to add
127          */
128         public void addAlbum(Album album) {
129                 Validation.begin().isNotNull("Album", album).check().isEqual("Album Owner", album.sone, sone).isEither("Old Album Parent", this.parent, null, album.parent).check();
130                 album.setParent(this);
131                 if (!albums.contains(album)) {
132                         albums.add(album);
133                 }
134         }
135
136         /**
137          * Removes an album from this album.
138          *
139          * @param album
140          *            The album to remove
141          */
142         public void removeAlbum(Album album) {
143                 Validation.begin().isNotNull("Album", album).check().isEqual("Album Owner", album.sone, sone).isEqual("Album Parent", album.parent, this).check();
144                 albums.remove(album);
145                 album.removeParent();
146         }
147
148         /**
149          * Returns the images in this album.
150          *
151          * @return The images in this album
152          */
153         public List<Image> getImages() {
154                 return new ArrayList<Image>(images.values());
155         }
156
157         /**
158          * Adds the given image to this album.
159          *
160          * @param image
161          *            The image to add
162          */
163         public void addImage(Image image) {
164                 Validation.begin().isNotNull("Image", image).check().isNotNull("Image Owner", image.getSone()).check().isEqual("Image Owner", image.getSone(), sone).check();
165                 if (image.getAlbum() != null) {
166                         image.getAlbum().removeImage(image);
167                 }
168                 image.setAlbum(this);
169                 if (images.isEmpty()) {
170                         albumImage = image.getId();
171                 }
172                 if (!images.containsKey(image.getId())) {
173                         images.put(image.getId(), image);
174                 }
175         }
176
177         /**
178          * Removes the given image from this album.
179          *
180          * @param image
181          *            The image to remove
182          */
183         public void removeImage(Image image) {
184                 Validation.begin().isNotNull("Image", image).check().isEqual("Image Owner", image.getSone(), sone).check();
185                 images.remove(image);
186                 if (image.getId().equals(albumImage)) {
187                         if (images.isEmpty()) {
188                                 albumImage = null;
189                         } else {
190                                 albumImage = images.values().iterator().next().getId();
191                         }
192                 }
193         }
194
195         /**
196          * Returns the album image of this album, or {@code null} if no album image
197          * has been set.
198          *
199          * @return The image to show when this album is listed
200          */
201         public Image getAlbumImage() {
202                 if (albumImage == null) {
203                         return null;
204                 }
205                 return images.get(albumImage);
206         }
207
208         /**
209          * Sets the ID of the album image.
210          *
211          * @param id
212          *            The ID of the album image
213          * @return This album
214          */
215         public Album setAlbumImage(String id) {
216                 this.albumImage = id;
217                 return this;
218         }
219
220         /**
221          * Returns whether this album contains any other albums or images.
222          *
223          * @return {@code true} if this album is empty, {@code false} otherwise
224          */
225         public boolean isEmpty() {
226                 return albums.isEmpty() && images.isEmpty();
227         }
228
229         /**
230          * Returns the parent album of this album.
231          *
232          * @return The parent album of this album, or {@code null} if this album
233          *         does not have a parent
234          */
235         public Album getParent() {
236                 return parent;
237         }
238
239         /**
240          * Sets the parent album of this album.
241          *
242          * @param parent
243          *            The new parent album of this album
244          * @return This album
245          */
246         protected Album setParent(Album parent) {
247                 Validation.begin().isNotNull("Album Parent", parent).check();
248                 this.parent = parent;
249                 return this;
250         }
251
252         /**
253          * Removes the parent album of this album.
254          *
255          * @return This album
256          */
257         protected Album removeParent() {
258                 this.parent = null;
259                 return this;
260         }
261
262         /**
263          * Returns the title of this album.
264          *
265          * @return The title of this album
266          */
267         public String getTitle() {
268                 return title;
269         }
270
271         /**
272          * Sets the title of this album.
273          *
274          * @param title
275          *            The title of this album
276          * @return This album
277          */
278         public Album setTitle(String title) {
279                 Validation.begin().isNotNull("Album Title", title).check();
280                 this.title = title;
281                 return this;
282         }
283
284         /**
285          * Returns the description of this album.
286          *
287          * @return The description of this album
288          */
289         public String getDescription() {
290                 return description;
291         }
292
293         /**
294          * Sets the description of this album.
295          *
296          * @param description
297          *            The description of this album
298          * @return This album
299          */
300         public Album setDescription(String description) {
301                 Validation.begin().isNotNull("Album Description", description).check();
302                 this.description = description;
303                 return this;
304         }
305
306         //
307         // FINGERPRINTABLE METHODS
308         //
309
310         /**
311          * {@inheritDoc}
312          */
313         @Override
314         public String getFingerprint() {
315                 StringBuilder fingerprint = new StringBuilder();
316                 fingerprint.append("Album(");
317                 fingerprint.append("ID(").append(id).append(')');
318                 fingerprint.append("Title(").append(title).append(')');
319                 fingerprint.append("Description(").append(description).append(')');
320
321                 /* add nested albums. */
322                 fingerprint.append("Albums(");
323                 for (Album album : albums) {
324                         fingerprint.append(album.getFingerprint());
325                 }
326                 fingerprint.append(')');
327
328                 /* add images. */
329                 fingerprint.append("Images(");
330                 for (Image image : images.values()) {
331                         if (image.isInserted()) {
332                                 fingerprint.append(image.getFingerprint());
333                         }
334                 }
335                 fingerprint.append(')');
336
337                 fingerprint.append(')');
338                 return fingerprint.toString();
339         }
340
341         //
342         // OBJECT METHODS
343         //
344
345         /**
346          * {@inheritDoc}
347          */
348         @Override
349         public int hashCode() {
350                 return id.hashCode();
351         }
352
353         /**
354          * {@inheritDoc}
355          */
356         @Override
357         public boolean equals(Object object) {
358                 if (!(object instanceof Album)) {
359                         return false;
360                 }
361                 Album album = (Album) object;
362                 return id.equals(album.id);
363         }
364
365 }