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.object.Default;
29 import net.pterodactylus.util.validation.Validation;
32 * Container for images that can also contain nested {@link Album}s.
34 * @author <a href="mailto:bombe@pterodactylus.net">David ‘Bombe’ Roden</a>
36 public class Album implements Fingerprintable {
38 /** The ID of this album. */
39 private final String id;
41 /** The Sone this album belongs to. */
45 private final List<Album> albums = new ArrayList<Album>();
47 /** The image IDs in order. */
48 private final List<String> imageIds = new ArrayList<String>();
50 /** The images in this album. */
51 private final Map<String, Image> images = new HashMap<String, Image>();
53 /** The parent album. */
56 /** The title of this album. */
59 /** The description of this album. */
60 private String description;
62 /** The ID of the album picture. */
63 private String albumImage;
66 * Creates a new album with a random ID.
69 this(UUID.randomUUID().toString());
73 * Creates a new album with the given ID.
78 public Album(String id) {
79 Validation.begin().isNotNull("Album ID", id).check();
88 * Returns the ID of this album.
90 * @return The ID of this album
92 public String getId() {
97 * Returns the Sone this album belongs to.
99 * @return The Sone this album belongs to
101 public Sone getSone() {
106 * Sets the owner of the album. The owner can only be set as long as the
107 * current owner is {@code null}.
113 public Album setSone(Sone sone) {
114 Validation.begin().isNotNull("New Album Owner", sone).isEither("Old Album Owner", this.sone, null, sone).check();
120 * Returns the nested albums.
122 * @return The nested albums
124 public List<Album> getAlbums() {
125 return new ArrayList<Album>(albums);
129 * Adds an album to this album.
134 public void addAlbum(Album album) {
135 Validation.begin().isNotNull("Album", album).check().isEqual("Album Owner", album.sone, sone).isEither("Old Album Parent", this.parent, null, album.parent).check();
136 album.setParent(this);
137 if (!albums.contains(album)) {
143 * Removes an album from this album.
146 * The album to remove
148 public void removeAlbum(Album album) {
149 Validation.begin().isNotNull("Album", album).check().isEqual("Album Owner", album.sone, sone).isEqual("Album Parent", album.parent, this).check();
150 albums.remove(album);
151 album.removeParent();
155 * Returns the images in this album.
157 * @return The images in this album
159 public List<Image> getImages() {
160 return Mappers.mappedList(imageIds, new Mapper<String, Image>() {
163 @SuppressWarnings("synthetic-access")
164 public Image map(String imageId) {
165 return images.get(imageId);
172 * Adds the given image to this album.
177 public void addImage(Image image) {
178 Validation.begin().isNotNull("Image", image).check().isNotNull("Image Owner", image.getSone()).check().isEqual("Image Owner", image.getSone(), sone).check();
179 if (image.getAlbum() != null) {
180 image.getAlbum().removeImage(image);
182 image.setAlbum(this);
183 if (imageIds.isEmpty() && (albumImage == null)) {
184 albumImage = image.getId();
186 if (!imageIds.contains(image.getId())) {
187 imageIds.add(image.getId());
188 images.put(image.getId(), image);
193 * Removes the given image from this album.
196 * The image to remove
198 public void removeImage(Image image) {
199 Validation.begin().isNotNull("Image", image).check().isEqual("Image Owner", image.getSone(), sone).check();
200 imageIds.remove(image.getId());
201 images.remove(image.getId());
202 if (image.getId().equals(albumImage)) {
203 if (images.isEmpty()) {
206 albumImage = images.values().iterator().next().getId();
212 * Moves the given image up in this album’s images. If the image is already
213 * the first image, nothing happens.
216 * The image to move up
218 public void moveImageUp(Image image) {
219 Validation.begin().isNotNull("Image", image).check().isEqual("Image Album", image.getAlbum(), this).isEqual("Album Owner", image.getAlbum().getSone(), sone).check();
220 int oldIndex = imageIds.indexOf(image.getId());
224 imageIds.remove(image.getId());
225 imageIds.add(oldIndex - 1, image.getId());
229 * Move the given image down in this album’s images. If the image is already
230 * the last image, nothing happens.
233 * The image to move down
235 public void moveImageDown(Image image) {
236 Validation.begin().isNotNull("Image", image).check().isEqual("Image Album", image.getAlbum(), this).isEqual("Album Owner", image.getAlbum().getSone(), sone).check();
237 int oldIndex = imageIds.indexOf(image.getId());
238 if ((oldIndex == -1) || (oldIndex >= (imageIds.size() - 1))) {
241 imageIds.remove(image.getId());
242 imageIds.add(oldIndex + 1, image.getId());
246 * Returns the album image of this album, or {@code null} if no album image
249 * @return The image to show when this album is listed
251 public Image getAlbumImage() {
252 if (albumImage == null) {
255 return Default.forNull(images.get(albumImage), images.values().iterator().next());
259 * Sets the ID of the album image.
262 * The ID of the album image
265 public Album setAlbumImage(String id) {
266 this.albumImage = id;
271 * Returns whether this album contains any other albums or images.
273 * @return {@code true} if this album is empty, {@code false} otherwise
275 public boolean isEmpty() {
276 return albums.isEmpty() && images.isEmpty();
280 * Returns the parent album of this album.
282 * @return The parent album of this album, or {@code null} if this album
283 * does not have a parent
285 public Album getParent() {
290 * Sets the parent album of this album.
293 * The new parent album of this album
296 protected Album setParent(Album parent) {
297 Validation.begin().isNotNull("Album Parent", parent).check();
298 this.parent = parent;
303 * Removes the parent album of this album.
307 protected Album removeParent() {
313 * Returns the title of this album.
315 * @return The title of this album
317 public String getTitle() {
322 * Sets the title of this album.
325 * The title of this album
328 public Album setTitle(String title) {
329 Validation.begin().isNotNull("Album Title", title).check();
335 * Returns the description of this album.
337 * @return The description of this album
339 public String getDescription() {
344 * Sets the description of this album.
347 * The description of this album
350 public Album setDescription(String description) {
351 Validation.begin().isNotNull("Album Description", description).check();
352 this.description = description;
357 // FINGERPRINTABLE METHODS
364 public String getFingerprint() {
365 StringBuilder fingerprint = new StringBuilder();
366 fingerprint.append("Album(");
367 fingerprint.append("ID(").append(id).append(')');
368 fingerprint.append("Title(").append(title).append(')');
369 fingerprint.append("Description(").append(description).append(')');
370 if (albumImage != null) {
371 fingerprint.append("AlbumImage(").append(albumImage).append(')');
374 /* add nested albums. */
375 fingerprint.append("Albums(");
376 for (Album album : albums) {
377 fingerprint.append(album.getFingerprint());
379 fingerprint.append(')');
382 fingerprint.append("Images(");
383 for (Image image : images.values()) {
384 if (image.isInserted()) {
385 fingerprint.append(image.getFingerprint());
388 fingerprint.append(')');
390 fingerprint.append(')');
391 return fingerprint.toString();
402 public int hashCode() {
403 return id.hashCode();
410 public boolean equals(Object object) {
411 if (!(object instanceof Album)) {
414 Album album = (Album) object;
415 return id.equals(album.id);