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.HashMap;
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 HashMap<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 * Moves the given image up in this album’s images. If the image is already
212 * the first image, nothing happens.
215 * The image to move up
217 public void moveImageUp(Image image) {
218 Validation.begin().isNotNull("Image", image).check().isEqual("Image Album", image.getAlbum(), this).isEqual("Album Owner", image.getAlbum().getSone(), sone).check();
219 int oldIndex = imageIds.indexOf(image.getId());
223 imageIds.remove(image.getId());
224 imageIds.add(oldIndex - 1, image.getId());
228 * Move the given image down in this album’s images. If the image is already
229 * the last image, nothing happens.
232 * The image to move down
234 public void moveImageDown(Image image) {
235 Validation.begin().isNotNull("Image", image).check().isEqual("Image Album", image.getAlbum(), this).isEqual("Album Owner", image.getAlbum().getSone(), sone).check();
236 int oldIndex = imageIds.indexOf(image.getId());
237 if ((oldIndex == -1) || (oldIndex >= (imageIds.size() - 1))) {
240 imageIds.remove(image.getId());
241 imageIds.add(oldIndex + 1, image.getId());
245 * Returns the album image of this album, or {@code null} if no album image
248 * @return The image to show when this album is listed
250 public Image getAlbumImage() {
251 if (albumImage == null) {
254 return images.get(albumImage);
258 * Sets the ID of the album image.
261 * The ID of the album image
264 public Album setAlbumImage(String id) {
265 this.albumImage = id;
270 * Returns whether this album contains any other albums or images.
272 * @return {@code true} if this album is empty, {@code false} otherwise
274 public boolean isEmpty() {
275 return albums.isEmpty() && images.isEmpty();
279 * Returns the parent album of this album.
281 * @return The parent album of this album, or {@code null} if this album
282 * does not have a parent
284 public Album getParent() {
289 * Sets the parent album of this album.
292 * The new parent album of this album
295 protected Album setParent(Album parent) {
296 Validation.begin().isNotNull("Album Parent", parent).check();
297 this.parent = parent;
302 * Removes the parent album of this album.
306 protected Album removeParent() {
312 * Returns the title of this album.
314 * @return The title of this album
316 public String getTitle() {
321 * Sets the title of this album.
324 * The title of this album
327 public Album setTitle(String title) {
328 Validation.begin().isNotNull("Album Title", title).check();
334 * Returns the description of this album.
336 * @return The description of this album
338 public String getDescription() {
343 * Sets the description of this album.
346 * The description of this album
349 public Album setDescription(String description) {
350 Validation.begin().isNotNull("Album Description", description).check();
351 this.description = description;
356 // FINGERPRINTABLE METHODS
363 public String getFingerprint() {
364 StringBuilder fingerprint = new StringBuilder();
365 fingerprint.append("Album(");
366 fingerprint.append("ID(").append(id).append(')');
367 fingerprint.append("Title(").append(title).append(')');
368 fingerprint.append("Description(").append(description).append(')');
370 /* add nested albums. */
371 fingerprint.append("Albums(");
372 for (Album album : albums) {
373 fingerprint.append(album.getFingerprint());
375 fingerprint.append(')');
378 fingerprint.append("Images(");
379 for (Image image : images.values()) {
380 if (image.isInserted()) {
381 fingerprint.append(image.getFingerprint());
384 fingerprint.append(')');
386 fingerprint.append(')');
387 return fingerprint.toString();
398 public int hashCode() {
399 return id.hashCode();
406 public boolean equals(Object object) {
407 if (!(object instanceof Album)) {
410 Album album = (Album) object;
411 return id.equals(album.id);