Add link to parent album.
[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 {
32
33         /** The ID of this album. */
34         private final String id;
35
36         /** The Sone this album belongs to. */
37         private final 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          * @param sone
58          *            The Sone this album belongs to
59          */
60         public Album(Sone sone) {
61                 this(UUID.randomUUID().toString(), sone);
62         }
63
64         /**
65          * Creates a new album with the given ID.
66          *
67          * @param id
68          *            The ID of the album
69          * @param sone
70          *            The Sone this album belongs to
71          */
72         public Album(String id, Sone sone) {
73                 Validation.begin().isNotNull("Album ID", id).isNotNull("Album Owner", sone).check();
74                 this.id = id;
75                 this.sone = sone;
76         }
77
78         //
79         // ACCESSORS
80         //
81
82         /**
83          * Returns the ID of this album.
84          *
85          * @return The ID of this album
86          */
87         public String getId() {
88                 return id;
89         }
90
91         /**
92          * Returns the Sone this album belongs to.
93          *
94          * @return The Sone this album belongs to
95          */
96         public Sone getSone() {
97                 return sone;
98         }
99
100         /**
101          * Returns the nested albums.
102          *
103          * @return The nested albums
104          */
105         public List<Album> getNestedAlbums() {
106                 return new ArrayList<Album>(albums);
107         }
108
109         /**
110          * Adds an album to this album.
111          *
112          * @param album
113          *            The album to add
114          */
115         public void addAlbum(Album album) {
116                 Validation.begin().isNotNull("Album", album).check().isEqual("Album Owner", album.sone, sone).isNull("Album Parent", album.parent).check();
117                 albums.add(album);
118                 album.setParent(this);
119         }
120
121         /**
122          * Removes an album from this album.
123          *
124          * @param album
125          *            The album to remove
126          */
127         public void removeAlbum(Album album) {
128                 Validation.begin().isNotNull("Album", album).check().isEqual("Album Owner", album.sone, sone).isEqual("Album Parent", album.parent, this).check();
129                 albums.remove(album);
130                 album.removeParent();
131         }
132
133         /**
134          * Returns the images in this album.
135          *
136          * @return The images in this album
137          */
138         public List<Image> getImages() {
139                 return new ArrayList<Image>(images);
140         }
141
142         /**
143          * Adds the given image to this album.
144          *
145          * @param image
146          *            The image to add
147          */
148         public void addImage(Image image) {
149                 Validation.begin().isNotNull("Image", image).check().isEqual("Image Owner", image.getSone(), sone).check();
150                 images.add(image);
151         }
152
153         /**
154          * Removes the given image from this album.
155          *
156          * @param image
157          *            The image to remove
158          */
159         public void removeImage(Image image) {
160                 Validation.begin().isNotNull("Image", image).check().isEqual("Image Owner", image.getSone(), sone).check();
161                 images.remove(image);
162         }
163
164         /**
165          * Returns the parent album of this album.
166          *
167          * @return The parent album of this album, or {@code null} if this album
168          *         does not have a parent
169          */
170         public Album getParent() {
171                 return parent;
172         }
173
174         /**
175          * Sets the parent album of this album.
176          *
177          * @param parent
178          *            The new parent album of this album
179          * @return This album
180          */
181         protected Album setParent(Album parent) {
182                 Validation.begin().isNotNull("Album Parent", parent).check();
183                 this.parent = parent;
184                 return this;
185         }
186
187         /**
188          * Removes the parent album of this album.
189          *
190          * @return This album
191          */
192         protected Album removeParent() {
193                 Validation.begin().isNotNull("Album Parent", parent).check();
194                 this.parent = null;
195                 return this;
196         }
197
198         /**
199          * Returns the name of this album.
200          *
201          * @return The name of this album
202          */
203         public String getName() {
204                 return name;
205         }
206
207         /**
208          * Sets the name of this album.
209          *
210          * @param name
211          *            The name of this album
212          * @return This album
213          */
214         public Album setName(String name) {
215                 Validation.begin().isNotNull("Album Name", name).check();
216                 this.name = name;
217                 return this;
218         }
219
220         /**
221          * Returns the description of this album.
222          *
223          * @return The description of this album
224          */
225         public String getDescription() {
226                 return description;
227         }
228
229         /**
230          * Sets the description of this album.
231          *
232          * @param description
233          *            The description of this album
234          * @return This album
235          */
236         public Album setDescription(String description) {
237                 Validation.begin().isNotNull("Album Description", description).check();
238                 this.description = description;
239                 return this;
240         }
241
242         //
243         // OBJECT METHODS
244         //
245
246         /**
247          * {@inheritDoc}
248          */
249         @Override
250         public boolean equals(Object object) {
251                 if (!(object instanceof Album)) {
252                         return false;
253                 }
254                 Album album = (Album) object;
255                 return sone.equals(album.sone) && id.equals(album.id);
256         }
257
258 }