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.LinkedHashMap;
22 import java.util.List;
24 import java.util.UUID;
26 import net.pterodactylus.util.validation.Validation;
29 * Container for images that can also contain nested {@link Album}s.
31 * @author <a href="mailto:bombe@pterodactylus.net">David ‘Bombe’ Roden</a>
33 public class Album implements Fingerprintable {
35 /** The ID of this album. */
36 private final String id;
38 /** The Sone this album belongs to. */
42 private final List<Album> albums = new ArrayList<Album>();
44 /** The images in this album. */
45 private final Map<String, Image> images = new LinkedHashMap<String, Image>();
47 /** The parent album. */
50 /** The title of this album. */
53 /** The description of this album. */
54 private String description;
56 /** The ID of the album picture. */
57 private String albumImage;
60 * Creates a new album with a random ID.
63 this(UUID.randomUUID().toString());
67 * Creates a new album with the given ID.
72 public Album(String id) {
73 Validation.begin().isNotNull("Album ID", id).check();
82 * Returns the ID of this album.
84 * @return The ID of this album
86 public String getId() {
91 * Returns the Sone this album belongs to.
93 * @return The Sone this album belongs to
95 public Sone getSone() {
100 * Sets the owner of the album. The owner can only be set as long as the
101 * current owner is {@code null}.
107 public Album setSone(Sone sone) {
108 Validation.begin().isNotNull("New Album Owner", sone).isEither("Old Album Owner", this.sone, null, sone).check();
114 * Returns the nested albums.
116 * @return The nested albums
118 public List<Album> getAlbums() {
119 return new ArrayList<Album>(albums);
123 * Adds an album to this album.
128 public void addAlbum(Album album) {
129 Validation.begin().isNotNull("Album", album).check().isEqual("Album Owner", album.sone, sone).isEither("Old Album Parent", this.parent, null, album.parent).check();
130 album.setParent(this);
131 if (!albums.contains(album)) {
137 * Removes an album from this album.
140 * The album to remove
142 public void removeAlbum(Album album) {
143 Validation.begin().isNotNull("Album", album).check().isEqual("Album Owner", album.sone, sone).isEqual("Album Parent", album.parent, this).check();
144 albums.remove(album);
145 album.removeParent();
149 * Returns the images in this album.
151 * @return The images in this album
153 public List<Image> getImages() {
154 return new ArrayList<Image>(images.values());
158 * Adds the given image to this album.
163 public void addImage(Image image) {
164 Validation.begin().isNotNull("Image", image).check().isNotNull("Image Owner", image.getSone()).check().isEqual("Image Owner", image.getSone(), sone).check();
165 if (image.getAlbum() != null) {
166 image.getAlbum().removeImage(image);
168 image.setAlbum(this);
169 if (images.isEmpty()) {
170 albumImage = image.getId();
172 if (!images.containsKey(image.getId())) {
173 images.put(image.getId(), image);
178 * Removes the given image from this album.
181 * The image to remove
183 public void removeImage(Image image) {
184 Validation.begin().isNotNull("Image", image).check().isEqual("Image Owner", image.getSone(), sone).check();
185 images.remove(image);
186 if (image.getId().equals(albumImage)) {
187 if (images.isEmpty()) {
190 albumImage = images.values().iterator().next().getId();
196 * Returns the album image of this album, or {@code null} if no album image
199 * @return The image to show when this album is listed
201 public Image getAlbumImage() {
202 if (albumImage == null) {
205 return images.get(albumImage);
209 * Sets the ID of the album image.
212 * The ID of the album image
215 public Album setAlbumImage(String id) {
216 this.albumImage = id;
221 * Returns whether this album contains any other albums or images.
223 * @return {@code true} if this album is empty, {@code false} otherwise
225 public boolean isEmpty() {
226 return albums.isEmpty() && images.isEmpty();
230 * Returns the parent album of this album.
232 * @return The parent album of this album, or {@code null} if this album
233 * does not have a parent
235 public Album getParent() {
240 * Sets the parent album of this album.
243 * The new parent album of this album
246 protected Album setParent(Album parent) {
247 Validation.begin().isNotNull("Album Parent", parent).check();
248 this.parent = parent;
253 * Removes the parent album of this album.
257 protected Album removeParent() {
263 * Returns the title of this album.
265 * @return The title of this album
267 public String getTitle() {
272 * Sets the title of this album.
275 * The title of this album
278 public Album setTitle(String title) {
279 Validation.begin().isNotNull("Album Title", title).check();
285 * Returns the description of this album.
287 * @return The description of this album
289 public String getDescription() {
294 * Sets the description of this album.
297 * The description of this album
300 public Album setDescription(String description) {
301 Validation.begin().isNotNull("Album Description", description).check();
302 this.description = description;
307 // FINGERPRINTABLE METHODS
314 public String getFingerprint() {
315 StringBuilder fingerprint = new StringBuilder();
316 fingerprint.append("Album(");
317 fingerprint.append("ID(").append(id).append(')');
318 fingerprint.append("Title(").append(title).append(')');
319 fingerprint.append("Description(").append(description).append(')');
321 /* add nested albums. */
322 fingerprint.append("Albums(");
323 for (Album album : albums) {
324 fingerprint.append(album.getFingerprint());
326 fingerprint.append(')');
329 fingerprint.append("Images(");
330 for (Image image : images.values()) {
331 if (image.isInserted()) {
332 fingerprint.append(image.getFingerprint());
335 fingerprint.append(')');
337 fingerprint.append(')');
338 return fingerprint.toString();
349 public int hashCode() {
350 return id.hashCode();
357 public boolean equals(Object object) {
358 if (!(object instanceof Album)) {
361 Album album = (Album) object;
362 return id.equals(album.id);