Add method to set the album image ID.
[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.containsKey(image.getId())) {
170                         images.put(image.getId(), image);
171                 }
172         }
173
174         /**
175          * Removes the given image from this album.
176          *
177          * @param image
178          *            The image to remove
179          */
180         public void removeImage(Image image) {
181                 Validation.begin().isNotNull("Image", image).check().isEqual("Image Owner", image.getSone(), sone).check();
182                 images.remove(image);
183         }
184
185         /**
186          * Returns the album image of this album, or {@code null} if no album image
187          * has been set.
188          *
189          * @return The image to show when this album is listed
190          */
191         public Image getAlbumImage() {
192                 if (albumImage == null) {
193                         return null;
194                 }
195                 return images.get(albumImage);
196         }
197
198         /**
199          * Sets the ID of the album image.
200          *
201          * @param id
202          *            The ID of the album image
203          * @return This album
204          */
205         public Album setAlbumImage(String id) {
206                 this.albumImage = id;
207                 return this;
208         }
209
210         /**
211          * Returns whether this album contains any other albums or images.
212          *
213          * @return {@code true} if this album is empty, {@code false} otherwise
214          */
215         public boolean isEmpty() {
216                 return albums.isEmpty() && images.isEmpty();
217         }
218
219         /**
220          * Returns the parent album of this album.
221          *
222          * @return The parent album of this album, or {@code null} if this album
223          *         does not have a parent
224          */
225         public Album getParent() {
226                 return parent;
227         }
228
229         /**
230          * Sets the parent album of this album.
231          *
232          * @param parent
233          *            The new parent album of this album
234          * @return This album
235          */
236         protected Album setParent(Album parent) {
237                 Validation.begin().isNotNull("Album Parent", parent).check();
238                 this.parent = parent;
239                 return this;
240         }
241
242         /**
243          * Removes the parent album of this album.
244          *
245          * @return This album
246          */
247         protected Album removeParent() {
248                 this.parent = null;
249                 return this;
250         }
251
252         /**
253          * Returns the title of this album.
254          *
255          * @return The title of this album
256          */
257         public String getTitle() {
258                 return title;
259         }
260
261         /**
262          * Sets the title of this album.
263          *
264          * @param title
265          *            The title of this album
266          * @return This album
267          */
268         public Album setTitle(String title) {
269                 Validation.begin().isNotNull("Album Title", title).check();
270                 this.title = title;
271                 return this;
272         }
273
274         /**
275          * Returns the description of this album.
276          *
277          * @return The description of this album
278          */
279         public String getDescription() {
280                 return description;
281         }
282
283         /**
284          * Sets the description of this album.
285          *
286          * @param description
287          *            The description of this album
288          * @return This album
289          */
290         public Album setDescription(String description) {
291                 Validation.begin().isNotNull("Album Description", description).check();
292                 this.description = description;
293                 return this;
294         }
295
296         //
297         // FINGERPRINTABLE METHODS
298         //
299
300         /**
301          * {@inheritDoc}
302          */
303         @Override
304         public String getFingerprint() {
305                 StringBuilder fingerprint = new StringBuilder();
306                 fingerprint.append("Album(");
307                 fingerprint.append("ID(").append(id).append(')');
308                 fingerprint.append("Title(").append(title).append(')');
309                 fingerprint.append("Description(").append(description).append(')');
310
311                 /* add nested albums. */
312                 fingerprint.append("Albums(");
313                 for (Album album : albums) {
314                         fingerprint.append(album.getFingerprint());
315                 }
316                 fingerprint.append(')');
317
318                 /* add images. */
319                 fingerprint.append("Images(");
320                 for (Image image : images.values()) {
321                         if (image.isInserted()) {
322                                 fingerprint.append(image.getFingerprint());
323                         }
324                 }
325                 fingerprint.append(')');
326
327                 fingerprint.append(')');
328                 return fingerprint.toString();
329         }
330
331         //
332         // OBJECT METHODS
333         //
334
335         /**
336          * {@inheritDoc}
337          */
338         @Override
339         public int hashCode() {
340                 return id.hashCode();
341         }
342
343         /**
344          * {@inheritDoc}
345          */
346         @Override
347         public boolean equals(Object object) {
348                 if (!(object instanceof Album)) {
349                         return false;
350                 }
351                 Album album = (Album) object;
352                 return id.equals(album.id);
353         }
354
355 }