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.collection.Mapper;
27 import net.pterodactylus.util.collection.Mappers;
28 import net.pterodactylus.util.validation.Validation;
31 * Container for images that can also contain nested {@link Album}s.
33 * @author <a href="mailto:bombe@pterodactylus.net">David ‘Bombe’ Roden</a>
35 public class Album implements Fingerprintable {
37 /** The ID of this album. */
38 private final String id;
40 /** The Sone this album belongs to. */
44 private final List<Album> albums = new ArrayList<Album>();
46 /** The image IDs in order. */
47 private final List<String> imageIds = new ArrayList<String>();
49 /** The images in this album. */
50 private final Map<String, Image> images = new LinkedHashMap<String, Image>();
52 /** The parent album. */
55 /** The title of this album. */
58 /** The description of this album. */
59 private String description;
61 /** The ID of the album picture. */
62 private String albumImage;
65 * Creates a new album with a random ID.
68 this(UUID.randomUUID().toString());
72 * Creates a new album with the given ID.
77 public Album(String id) {
78 Validation.begin().isNotNull("Album ID", id).check();
87 * Returns the ID of this album.
89 * @return The ID of this album
91 public String getId() {
96 * Returns the Sone this album belongs to.
98 * @return The Sone this album belongs to
100 public Sone getSone() {
105 * Sets the owner of the album. The owner can only be set as long as the
106 * current owner is {@code null}.
112 public Album setSone(Sone sone) {
113 Validation.begin().isNotNull("New Album Owner", sone).isEither("Old Album Owner", this.sone, null, sone).check();
119 * Returns the nested albums.
121 * @return The nested albums
123 public List<Album> getAlbums() {
124 return new ArrayList<Album>(albums);
128 * Adds an album to this album.
133 public void addAlbum(Album album) {
134 Validation.begin().isNotNull("Album", album).check().isEqual("Album Owner", album.sone, sone).isEither("Old Album Parent", this.parent, null, album.parent).check();
135 album.setParent(this);
136 if (!albums.contains(album)) {
142 * Removes an album from this album.
145 * The album to remove
147 public void removeAlbum(Album album) {
148 Validation.begin().isNotNull("Album", album).check().isEqual("Album Owner", album.sone, sone).isEqual("Album Parent", album.parent, this).check();
149 albums.remove(album);
150 album.removeParent();
154 * Returns the images in this album.
156 * @return The images in this album
158 public List<Image> getImages() {
159 return Mappers.mappedList(imageIds, new Mapper<String, Image>() {
162 @SuppressWarnings("synthetic-access")
163 public Image map(String imageId) {
164 return images.get(imageId);
171 * Adds the given image to this album.
176 public void addImage(Image image) {
177 Validation.begin().isNotNull("Image", image).check().isNotNull("Image Owner", image.getSone()).check().isEqual("Image Owner", image.getSone(), sone).check();
178 if (image.getAlbum() != null) {
179 image.getAlbum().removeImage(image);
181 image.setAlbum(this);
182 if (imageIds.isEmpty()) {
183 albumImage = image.getId();
185 if (!imageIds.contains(image.getId())) {
186 imageIds.add(image.getId());
187 images.put(image.getId(), image);
192 * Removes the given image from this album.
195 * The image to remove
197 public void removeImage(Image image) {
198 Validation.begin().isNotNull("Image", image).check().isEqual("Image Owner", image.getSone(), sone).check();
199 imageIds.remove(image.getId());
200 images.remove(image.getId());
201 if (image.getId().equals(albumImage)) {
202 if (images.isEmpty()) {
205 albumImage = images.values().iterator().next().getId();
211 * Returns the album image of this album, or {@code null} if no album image
214 * @return The image to show when this album is listed
216 public Image getAlbumImage() {
217 if (albumImage == null) {
220 return images.get(albumImage);
224 * Sets the ID of the album image.
227 * The ID of the album image
230 public Album setAlbumImage(String id) {
231 this.albumImage = id;
236 * Returns whether this album contains any other albums or images.
238 * @return {@code true} if this album is empty, {@code false} otherwise
240 public boolean isEmpty() {
241 return albums.isEmpty() && images.isEmpty();
245 * Returns the parent album of this album.
247 * @return The parent album of this album, or {@code null} if this album
248 * does not have a parent
250 public Album getParent() {
255 * Sets the parent album of this album.
258 * The new parent album of this album
261 protected Album setParent(Album parent) {
262 Validation.begin().isNotNull("Album Parent", parent).check();
263 this.parent = parent;
268 * Removes the parent album of this album.
272 protected Album removeParent() {
278 * Returns the title of this album.
280 * @return The title of this album
282 public String getTitle() {
287 * Sets the title of this album.
290 * The title of this album
293 public Album setTitle(String title) {
294 Validation.begin().isNotNull("Album Title", title).check();
300 * Returns the description of this album.
302 * @return The description of this album
304 public String getDescription() {
309 * Sets the description of this album.
312 * The description of this album
315 public Album setDescription(String description) {
316 Validation.begin().isNotNull("Album Description", description).check();
317 this.description = description;
322 // FINGERPRINTABLE METHODS
329 public String getFingerprint() {
330 StringBuilder fingerprint = new StringBuilder();
331 fingerprint.append("Album(");
332 fingerprint.append("ID(").append(id).append(')');
333 fingerprint.append("Title(").append(title).append(')');
334 fingerprint.append("Description(").append(description).append(')');
336 /* add nested albums. */
337 fingerprint.append("Albums(");
338 for (Album album : albums) {
339 fingerprint.append(album.getFingerprint());
341 fingerprint.append(')');
344 fingerprint.append("Images(");
345 for (Image image : images.values()) {
346 if (image.isInserted()) {
347 fingerprint.append(image.getFingerprint());
350 fingerprint.append(')');
352 fingerprint.append(')');
353 return fingerprint.toString();
364 public int hashCode() {
365 return id.hashCode();
372 public boolean equals(Object object) {
373 if (!(object instanceof Album)) {
376 Album album = (Album) object;
377 return id.equals(album.id);