Add methods to add and remove images to or from an 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 name of this album. */
46         private String name;
47
48         /** The description of this album. */
49         private String description;
50
51         /**
52          * Creates a new album with a random ID.
53          *
54          * @param sone
55          *            The Sone this album belongs to
56          */
57         public Album(Sone sone) {
58                 this(UUID.randomUUID().toString(), sone);
59         }
60
61         /**
62          * Creates a new album with the given ID.
63          *
64          * @param id
65          *            The ID of the album
66          * @param sone
67          *            The Sone this album belongs to
68          */
69         public Album(String id, Sone sone) {
70                 Validation.begin().isNotNull("Album ID", id).isNotNull("Album Owner", sone).check();
71                 this.id = id;
72                 this.sone = sone;
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          * Returns the nested albums.
99          *
100          * @return The nested albums
101          */
102         public List<Album> getNestedAlbums() {
103                 return new ArrayList<Album>(albums);
104         }
105
106         /**
107          * Adds an album to this album.
108          *
109          * @param album
110          *            The album to add
111          */
112         public void addAlbum(Album album) {
113                 Validation.begin().isNotNull("Album", album).check().isEqual("Album Owner", album.sone, sone).check();
114                 albums.add(album);
115         }
116
117         /**
118          * Removes an album from this album.
119          *
120          * @param album
121          *            The album to remove
122          */
123         public void removeAlbum(Album album) {
124                 Validation.begin().isNotNull("Album", album).check().isEqual("Album Owner", album.sone, sone).check();
125                 albums.remove(album);
126         }
127
128         /**
129          * Returns the images in this album.
130          *
131          * @return The images in this album
132          */
133         public List<Image> getImages() {
134                 return new ArrayList<Image>(images);
135         }
136
137         /**
138          * Adds the given image to this album.
139          *
140          * @param image
141          *            The image to add
142          */
143         public void addImage(Image image) {
144                 Validation.begin().isNotNull("Image", image).check().isEqual("Image Owner", image.getSone(), sone).check();
145                 images.add(image);
146         }
147
148         /**
149          * Removes the given image from this album.
150          *
151          * @param image
152          *            The image to remove
153          */
154         public void removeImage(Image image) {
155                 Validation.begin().isNotNull("Image", image).check().isEqual("Image Owner", image.getSone(), sone).check();
156                 images.remove(image);
157         }
158
159         /**
160          * Returns the name of this album.
161          *
162          * @return The name of this album
163          */
164         public String getName() {
165                 return name;
166         }
167
168         /**
169          * Sets the name of this album.
170          *
171          * @param name
172          *            The name of this album
173          * @return This album
174          */
175         public Album setName(String name) {
176                 Validation.begin().isNotNull("Album Name", name).check();
177                 this.name = name;
178                 return this;
179         }
180
181         /**
182          * Returns the description of this album.
183          *
184          * @return The description of this album
185          */
186         public String getDescription() {
187                 return description;
188         }
189
190         /**
191          * Sets the description of this album.
192          *
193          * @param description
194          *            The description of this album
195          * @return This album
196          */
197         public Album setDescription(String description) {
198                 Validation.begin().isNotNull("Album Description", description).check();
199                 this.description = description;
200                 return this;
201         }
202
203         //
204         // OBJECT METHODS
205         //
206
207         /**
208          * {@inheritDoc}
209          */
210         @Override
211         public boolean equals(Object object) {
212                 if (!(object instanceof Album)) {
213                         return false;
214                 }
215                 Album album = (Album) object;
216                 return sone.equals(album.sone) && id.equals(album.id);
217         }
218
219 }