Don’t require an owner.
[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 name of this album. */
49         private String name;
50
51         /** The description of this album. */
52         private String description;
53
54         /**
55          * Creates a new album with a random ID.
56          */
57         public Album() {
58                 this(UUID.randomUUID().toString());
59         }
60
61         /**
62          * Creates a new album with the given ID.
63          *
64          * @param id
65          *            The ID of the album
66          */
67         public Album(String id) {
68                 Validation.begin().isNotNull("Album ID", id).check();
69                 this.id = id;
70         }
71
72         //
73         // ACCESSORS
74         //
75
76         /**
77          * Returns the ID of this album.
78          *
79          * @return The ID of this album
80          */
81         public String getId() {
82                 return id;
83         }
84
85         /**
86          * Returns the Sone this album belongs to.
87          *
88          * @return The Sone this album belongs to
89          */
90         public Sone getSone() {
91                 return sone;
92         }
93
94         /**
95          * Sets the owner of the album. The owner can only be set as long as the
96          * current owner is {@code null}.
97          *
98          * @param sone
99          *            The album owner
100          * @return This album
101          */
102         public Album setSone(Sone sone) {
103                 Validation.begin().isNull("Current Album Owner", this.sone).isNotNull("New Album Owner", sone).check().isEqual("New Album Owner", sone, this.sone).check();
104                 this.sone = sone;
105                 return this;
106         }
107
108         /**
109          * Returns the nested albums.
110          *
111          * @return The nested albums
112          */
113         public List<Album> getNestedAlbums() {
114                 return new ArrayList<Album>(albums);
115         }
116
117         /**
118          * Adds an album to this album.
119          *
120          * @param album
121          *            The album to add
122          */
123         public void addAlbum(Album album) {
124                 Validation.begin().isNotNull("Album", album).check().isEqual("Album Owner", album.sone, sone).isNull("Album Parent", album.parent).check();
125                 albums.add(album);
126                 album.setParent(this);
127         }
128
129         /**
130          * Removes an album from this album.
131          *
132          * @param album
133          *            The album to remove
134          */
135         public void removeAlbum(Album album) {
136                 Validation.begin().isNotNull("Album", album).check().isEqual("Album Owner", album.sone, sone).isEqual("Album Parent", album.parent, this).check();
137                 albums.remove(album);
138                 album.removeParent();
139         }
140
141         /**
142          * Returns the images in this album.
143          *
144          * @return The images in this album
145          */
146         public List<Image> getImages() {
147                 return new ArrayList<Image>(images);
148         }
149
150         /**
151          * Adds the given image to this album.
152          *
153          * @param image
154          *            The image to add
155          */
156         public void addImage(Image image) {
157                 Validation.begin().isNotNull("Image", image).check().isEqual("Image Owner", image.getSone(), sone).check();
158                 images.add(image);
159         }
160
161         /**
162          * Removes the given image from this album.
163          *
164          * @param image
165          *            The image to remove
166          */
167         public void removeImage(Image image) {
168                 Validation.begin().isNotNull("Image", image).check().isEqual("Image Owner", image.getSone(), sone).check();
169                 images.remove(image);
170         }
171
172         /**
173          * Returns the parent album of this album.
174          *
175          * @return The parent album of this album, or {@code null} if this album
176          *         does not have a parent
177          */
178         public Album getParent() {
179                 return parent;
180         }
181
182         /**
183          * Sets the parent album of this album.
184          *
185          * @param parent
186          *            The new parent album of this album
187          * @return This album
188          */
189         protected Album setParent(Album parent) {
190                 Validation.begin().isNotNull("Album Parent", parent).check();
191                 this.parent = parent;
192                 return this;
193         }
194
195         /**
196          * Removes the parent album of this album.
197          *
198          * @return This album
199          */
200         protected Album removeParent() {
201                 Validation.begin().isNotNull("Album Parent", parent).check();
202                 this.parent = null;
203                 return this;
204         }
205
206         /**
207          * Returns the name of this album.
208          *
209          * @return The name of this album
210          */
211         public String getName() {
212                 return name;
213         }
214
215         /**
216          * Sets the name of this album.
217          *
218          * @param name
219          *            The name of this album
220          * @return This album
221          */
222         public Album setName(String name) {
223                 Validation.begin().isNotNull("Album Name", name).check();
224                 this.name = name;
225                 return this;
226         }
227
228         /**
229          * Returns the description of this album.
230          *
231          * @return The description of this album
232          */
233         public String getDescription() {
234                 return description;
235         }
236
237         /**
238          * Sets the description of this album.
239          *
240          * @param description
241          *            The description of this album
242          * @return This album
243          */
244         public Album setDescription(String description) {
245                 Validation.begin().isNotNull("Album Description", description).check();
246                 this.description = description;
247                 return this;
248         }
249
250         //
251         // FINGERPRINTABLE METHODS
252         //
253
254         /**
255          * {@inheritDoc}
256          */
257         @Override
258         public String getFingerprint() {
259                 StringBuilder fingerprint = new StringBuilder();
260                 fingerprint.append("Album(");
261                 fingerprint.append("ID(").append(id).append(')');
262                 fingerprint.append("Name(").append(name).append(')');
263                 fingerprint.append("Description(").append(description).append(')');
264
265                 /* add nested albums. */
266                 fingerprint.append("Albums(");
267                 for (Album album : albums) {
268                         fingerprint.append(album.getFingerprint());
269                 }
270                 fingerprint.append(')');
271
272                 /* add images. */
273                 fingerprint.append("Images(");
274                 for (Image image : images) {
275                         fingerprint.append(image.getFingerprint());
276                 }
277                 fingerprint.append(')');
278
279                 fingerprint.append(')');
280                 return fingerprint.toString();
281         }
282
283         //
284         // OBJECT METHODS
285         //
286
287         /**
288          * {@inheritDoc}
289          */
290         @Override
291         public boolean equals(Object object) {
292                 if (!(object instanceof Album)) {
293                         return false;
294                 }
295                 Album album = (Album) object;
296                 return id.equals(album.id);
297         }
298
299 }