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