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.containsKey(image.getId())) {
170 images.put(image.getId(), image);
175 * Removes the given image from this album.
178 * The image to remove
180 public void removeImage(Image image) {
181 Validation.begin().isNotNull("Image", image).check().isEqual("Image Owner", image.getSone(), sone).check();
182 images.remove(image);
186 * Returns the album image of this album, or {@code null} if no album image
189 * @return The image to show when this album is listed
191 public Image getAlbumImage() {
192 if (albumImage == null) {
195 return images.get(albumImage);
199 * Returns whether this album contains any other albums or images.
201 * @return {@code true} if this album is empty, {@code false} otherwise
203 public boolean isEmpty() {
204 return albums.isEmpty() && images.isEmpty();
208 * Returns the parent album of this album.
210 * @return The parent album of this album, or {@code null} if this album
211 * does not have a parent
213 public Album getParent() {
218 * Sets the parent album of this album.
221 * The new parent album of this album
224 protected Album setParent(Album parent) {
225 Validation.begin().isNotNull("Album Parent", parent).check();
226 this.parent = parent;
231 * Removes the parent album of this album.
235 protected Album removeParent() {
241 * Returns the title of this album.
243 * @return The title of this album
245 public String getTitle() {
250 * Sets the title of this album.
253 * The title of this album
256 public Album setTitle(String title) {
257 Validation.begin().isNotNull("Album Title", title).check();
263 * Returns the description of this album.
265 * @return The description of this album
267 public String getDescription() {
272 * Sets the description of this album.
275 * The description of this album
278 public Album setDescription(String description) {
279 Validation.begin().isNotNull("Album Description", description).check();
280 this.description = description;
285 // FINGERPRINTABLE METHODS
292 public String getFingerprint() {
293 StringBuilder fingerprint = new StringBuilder();
294 fingerprint.append("Album(");
295 fingerprint.append("ID(").append(id).append(')');
296 fingerprint.append("Title(").append(title).append(')');
297 fingerprint.append("Description(").append(description).append(')');
299 /* add nested albums. */
300 fingerprint.append("Albums(");
301 for (Album album : albums) {
302 fingerprint.append(album.getFingerprint());
304 fingerprint.append(')');
307 fingerprint.append("Images(");
308 for (Image image : images.values()) {
309 if (image.isInserted()) {
310 fingerprint.append(image.getFingerprint());
313 fingerprint.append(')');
315 fingerprint.append(')');
316 return fingerprint.toString();
327 public int hashCode() {
328 return id.hashCode();
335 public boolean equals(Object object) {
336 if (!(object instanceof Album)) {
339 Album album = (Album) object;
340 return id.equals(album.id);