Merge branch 'next' into dev/image
[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).isEither("Old Album Owner", this.sone, null, 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).isEither("Old Album Parent", this.parent, null, album.parent).check();
128                 album.setParent(this);
129                 if (!albums.contains(album)) {
130                         albums.add(album);
131                 }
132         }
133
134         /**
135          * Removes an album from this album.
136          *
137          * @param album
138          *            The album to remove
139          */
140         public void removeAlbum(Album album) {
141                 Validation.begin().isNotNull("Album", album).check().isEqual("Album Owner", album.sone, sone).isEqual("Album Parent", album.parent, this).check();
142                 albums.remove(album);
143                 album.removeParent();
144         }
145
146         /**
147          * Returns the images in this album.
148          *
149          * @return The images in this album
150          */
151         public List<Image> getImages() {
152                 return new ArrayList<Image>(images);
153         }
154
155         /**
156          * Adds the given image to this album.
157          *
158          * @param image
159          *            The image to add
160          */
161         public void addImage(Image image) {
162                 Validation.begin().isNotNull("Image", image).check().isNotNull("Image Owner", image.getSone()).check().isEqual("Image Owner", image.getSone(), sone).check();
163                 if (image.getAlbum() != null) {
164                         image.getAlbum().removeImage(image);
165                 }
166                 image.setAlbum(this);
167                 if (!images.contains(image)) {
168                         images.add(image);
169                 }
170         }
171
172         /**
173          * Removes the given image from this album.
174          *
175          * @param image
176          *            The image to remove
177          */
178         public void removeImage(Image image) {
179                 Validation.begin().isNotNull("Image", image).check().isEqual("Image Owner", image.getSone(), sone).check();
180                 images.remove(image);
181         }
182
183         /**
184          * Returns the album image of this album, or {@code null} if no album image
185          * has been set.
186          *
187          * @return The image to show when this album is listed
188          */
189         public Image getAlbumImage() {
190                 if (albumImage == -1) {
191                         return null;
192                 }
193                 return images.get(albumImage);
194         }
195
196         /**
197          * Returns whether this album contains any other albums or images.
198          *
199          * @return {@code true} if this album is empty, {@code false} otherwise
200          */
201         public boolean isEmpty() {
202                 return albums.isEmpty() && images.isEmpty();
203         }
204
205         /**
206          * Returns the parent album of this album.
207          *
208          * @return The parent album of this album, or {@code null} if this album
209          *         does not have a parent
210          */
211         public Album getParent() {
212                 return parent;
213         }
214
215         /**
216          * Sets the parent album of this album.
217          *
218          * @param parent
219          *            The new parent album of this album
220          * @return This album
221          */
222         protected Album setParent(Album parent) {
223                 Validation.begin().isNotNull("Album Parent", parent).check();
224                 this.parent = parent;
225                 return this;
226         }
227
228         /**
229          * Removes the parent album of this album.
230          *
231          * @return This album
232          */
233         protected Album removeParent() {
234                 this.parent = null;
235                 return this;
236         }
237
238         /**
239          * Returns the title of this album.
240          *
241          * @return The title of this album
242          */
243         public String getTitle() {
244                 return title;
245         }
246
247         /**
248          * Sets the title of this album.
249          *
250          * @param title
251          *            The title of this album
252          * @return This album
253          */
254         public Album setTitle(String title) {
255                 Validation.begin().isNotNull("Album Title", title).check();
256                 this.title = title;
257                 return this;
258         }
259
260         /**
261          * Returns the description of this album.
262          *
263          * @return The description of this album
264          */
265         public String getDescription() {
266                 return description;
267         }
268
269         /**
270          * Sets the description of this album.
271          *
272          * @param description
273          *            The description of this album
274          * @return This album
275          */
276         public Album setDescription(String description) {
277                 Validation.begin().isNotNull("Album Description", description).check();
278                 this.description = description;
279                 return this;
280         }
281
282         //
283         // FINGERPRINTABLE METHODS
284         //
285
286         /**
287          * {@inheritDoc}
288          */
289         @Override
290         public String getFingerprint() {
291                 StringBuilder fingerprint = new StringBuilder();
292                 fingerprint.append("Album(");
293                 fingerprint.append("ID(").append(id).append(')');
294                 fingerprint.append("Title(").append(title).append(')');
295                 fingerprint.append("Description(").append(description).append(')');
296
297                 /* add nested albums. */
298                 fingerprint.append("Albums(");
299                 for (Album album : albums) {
300                         fingerprint.append(album.getFingerprint());
301                 }
302                 fingerprint.append(')');
303
304                 /* add images. */
305                 fingerprint.append("Images(");
306                 for (Image image : images) {
307                         if (image.isInserted()) {
308                                 fingerprint.append(image.getFingerprint());
309                         }
310                 }
311                 fingerprint.append(')');
312
313                 fingerprint.append(')');
314                 return fingerprint.toString();
315         }
316
317         //
318         // OBJECT METHODS
319         //
320
321         /**
322          * {@inheritDoc}
323          */
324         @Override
325         public int hashCode() {
326                 return id.hashCode();
327         }
328
329         /**
330          * {@inheritDoc}
331          */
332         @Override
333         public boolean equals(Object object) {
334                 if (!(object instanceof Album)) {
335                         return false;
336                 }
337                 Album album = (Album) object;
338                 return id.equals(album.id);
339         }
340
341 }