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 title of this album. */
51 /** The description of this album. */
52 private String description;
54 /** The index of the album picture. */
55 private int albumImage = -1;
58 * Creates a new album with a random ID.
61 this(UUID.randomUUID().toString());
65 * Creates a new album with the given ID.
70 public Album(String id) {
71 Validation.begin().isNotNull("Album ID", id).check();
80 * Returns the ID of this album.
82 * @return The ID of this album
84 public String getId() {
89 * Returns the Sone this album belongs to.
91 * @return The Sone this album belongs to
93 public Sone getSone() {
98 * Sets the owner of the album. The owner can only be set as long as the
99 * current owner is {@code null}.
105 public Album setSone(Sone sone) {
106 Validation.begin().isNotNull("New Album Owner", sone).isEither("Old Album Owner", this.sone, null, sone).check();
112 * Returns the nested albums.
114 * @return The nested albums
116 public List<Album> getAlbums() {
117 return new ArrayList<Album>(albums);
121 * Adds an album to this album.
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)) {
135 * Removes an album from this album.
138 * The album to remove
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();
147 * Returns the images in this album.
149 * @return The images in this album
151 public List<Image> getImages() {
152 return new ArrayList<Image>(images);
156 * Adds the given image to this album.
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 image.setAlbum(this);
164 if (!images.contains(image)) {
170 * Removes the given image from this album.
173 * The image to remove
175 public void removeImage(Image image) {
176 Validation.begin().isNotNull("Image", image).check().isEqual("Image Owner", image.getSone(), sone).check();
177 images.remove(image);
181 * Returns the album image of this album, or {@code null} if no album image
184 * @return The image to show when this album is listed
186 public Image getAlbumImage() {
187 if (albumImage == -1) {
190 return images.get(albumImage);
194 * Returns whether this album contains any other albums or images.
196 * @return {@code true} if this album is empty, {@code false} otherwise
198 public boolean isEmpty() {
199 return albums.isEmpty() && images.isEmpty();
203 * Returns the parent album of this album.
205 * @return The parent album of this album, or {@code null} if this album
206 * does not have a parent
208 public Album getParent() {
213 * Sets the parent album of this album.
216 * The new parent album of this album
219 protected Album setParent(Album parent) {
220 Validation.begin().isNotNull("Album Parent", parent).check();
221 this.parent = parent;
226 * Removes the parent album of this album.
230 protected Album removeParent() {
236 * Returns the title of this album.
238 * @return The title of this album
240 public String getTitle() {
245 * Sets the title of this album.
248 * The title of this album
251 public Album setTitle(String title) {
252 Validation.begin().isNotNull("Album Title", title).check();
258 * Returns the description of this album.
260 * @return The description of this album
262 public String getDescription() {
267 * Sets the description of this album.
270 * The description of this album
273 public Album setDescription(String description) {
274 Validation.begin().isNotNull("Album Description", description).check();
275 this.description = description;
280 // FINGERPRINTABLE METHODS
287 public String getFingerprint() {
288 StringBuilder fingerprint = new StringBuilder();
289 fingerprint.append("Album(");
290 fingerprint.append("ID(").append(id).append(')');
291 fingerprint.append("Title(").append(title).append(')');
292 fingerprint.append("Description(").append(description).append(')');
294 /* add nested albums. */
295 fingerprint.append("Albums(");
296 for (Album album : albums) {
297 fingerprint.append(album.getFingerprint());
299 fingerprint.append(')');
302 fingerprint.append("Images(");
303 for (Image image : images) {
304 if (image.isInserted()) {
305 fingerprint.append(image.getFingerprint());
308 fingerprint.append(')');
310 fingerprint.append(')');
311 return fingerprint.toString();
322 public int hashCode() {
323 return id.hashCode();
330 public boolean equals(Object object) {
331 if (!(object instanceof Album)) {
334 Album album = (Album) object;
335 return id.equals(album.id);