2 * Sone - Album.java - Copyright © 2011 David Roden
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.
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.
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/>.
18 package net.pterodactylus.sone.data;
20 import java.util.ArrayList;
21 import java.util.List;
22 import java.util.UUID;
24 import net.pterodactylus.util.validation.Validation;
27 * Container for images that can also contain nested {@link Album}s.
29 * @author <a href="mailto:bombe@pterodactylus.net">David ‘Bombe’ Roden</a>
31 public class Album implements Fingerprintable {
33 /** The ID of this album. */
34 private final String id;
36 /** The Sone this album belongs to. */
40 private final List<Album> albums = new ArrayList<Album>();
42 /** The images in this album. */
43 private final List<Image> images = new ArrayList<Image>();
45 /** The parent album. */
48 /** The name of this album. */
51 /** The description of this album. */
52 private String description;
55 * Creates a new album with a random ID.
58 this(UUID.randomUUID().toString());
62 * Creates a new album with the given ID.
67 public Album(String id) {
68 Validation.begin().isNotNull("Album ID", id).check();
77 * Returns the ID of this album.
79 * @return The ID of this album
81 public String getId() {
86 * Returns the Sone this album belongs to.
88 * @return The Sone this album belongs to
90 public Sone getSone() {
95 * Sets the owner of the album. The owner can only be set as long as the
96 * current owner is {@code null}.
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();
109 * Returns the nested albums.
111 * @return The nested albums
113 public List<Album> getAlbums() {
114 return new ArrayList<Album>(albums);
118 * Adds an album to this album.
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();
126 album.setParent(this);
130 * Removes an album from this album.
133 * The album to remove
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();
142 * Returns the images in this album.
144 * @return The images in this album
146 public List<Image> getImages() {
147 return new ArrayList<Image>(images);
151 * Adds the given image to this album.
156 public void addImage(Image image) {
157 Validation.begin().isNotNull("Image", image).check().isEqual("Image Owner", image.getSone(), sone).check();
162 * Removes the given image from this album.
165 * The image to remove
167 public void removeImage(Image image) {
168 Validation.begin().isNotNull("Image", image).check().isEqual("Image Owner", image.getSone(), sone).check();
169 images.remove(image);
173 * Returns the parent album of this album.
175 * @return The parent album of this album, or {@code null} if this album
176 * does not have a parent
178 public Album getParent() {
183 * Sets the parent album of this album.
186 * The new parent album of this album
189 protected Album setParent(Album parent) {
190 Validation.begin().isNotNull("Album Parent", parent).check();
191 this.parent = parent;
196 * Removes the parent album of this album.
200 protected Album removeParent() {
201 Validation.begin().isNotNull("Album Parent", parent).check();
207 * Returns the name of this album.
209 * @return The name of this album
211 public String getName() {
216 * Sets the name of this album.
219 * The name of this album
222 public Album setName(String name) {
223 Validation.begin().isNotNull("Album Name", name).check();
229 * Returns the description of this album.
231 * @return The description of this album
233 public String getDescription() {
238 * Sets the description of this album.
241 * The description of this album
244 public Album setDescription(String description) {
245 Validation.begin().isNotNull("Album Description", description).check();
246 this.description = description;
251 // FINGERPRINTABLE METHODS
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(')');
265 /* add nested albums. */
266 fingerprint.append("Albums(");
267 for (Album album : albums) {
268 fingerprint.append(album.getFingerprint());
270 fingerprint.append(')');
273 fingerprint.append("Images(");
274 for (Image image : images) {
275 fingerprint.append(image.getFingerprint());
277 fingerprint.append(')');
279 fingerprint.append(')');
280 return fingerprint.toString();
291 public boolean equals(Object object) {
292 if (!(object instanceof Album)) {
295 Album album = (Album) object;
296 return id.equals(album.id);