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
217 * @return The image that the given image swapped the place with, or
218 * <code>null</code> if the image did not change its place
220 public Image moveImageUp(Image image) {
221 Validation.begin().isNotNull("Image", image).check().isEqual("Image Album", image.getAlbum(), this).isEqual("Album Owner", image.getAlbum().getSone(), sone).check();
222 int oldIndex = imageIds.indexOf(image.getId());
226 imageIds.remove(image.getId());
227 imageIds.add(oldIndex - 1, image.getId());
228 return images.get(imageIds.get(oldIndex));
232 * Move the given image down in this album’s images. If the image is already
233 * the last image, nothing happens.
236 * The image to move down
237 * @return The image that the given image swapped the place with, or
238 * <code>null</code> if the image did not change its place
240 public Image moveImageDown(Image image) {
241 Validation.begin().isNotNull("Image", image).check().isEqual("Image Album", image.getAlbum(), this).isEqual("Album Owner", image.getAlbum().getSone(), sone).check();
242 int oldIndex = imageIds.indexOf(image.getId());
243 if ((oldIndex == -1) || (oldIndex >= (imageIds.size() - 1))) {
246 imageIds.remove(image.getId());
247 imageIds.add(oldIndex + 1, image.getId());
248 return images.get(imageIds.get(oldIndex));
252 * Returns the album image of this album, or {@code null} if no album image
255 * @return The image to show when this album is listed
257 public Image getAlbumImage() {
258 if (albumImage == null) {
261 return Default.forNull(images.get(albumImage), images.values().iterator().next());
265 * Sets the ID of the album image.
268 * The ID of the album image
271 public Album setAlbumImage(String id) {
272 this.albumImage = id;
277 * Returns whether this album contains any other albums or images.
279 * @return {@code true} if this album is empty, {@code false} otherwise
281 public boolean isEmpty() {
282 return albums.isEmpty() && images.isEmpty();
286 * Returns the parent album of this album.
288 * @return The parent album of this album, or {@code null} if this album
289 * does not have a parent
291 public Album getParent() {
296 * Sets the parent album of this album.
299 * The new parent album of this album
302 protected Album setParent(Album parent) {
303 Validation.begin().isNotNull("Album Parent", parent).check();
304 this.parent = parent;
309 * Removes the parent album of this album.
313 protected Album removeParent() {
319 * Returns the title of this album.
321 * @return The title of this album
323 public String getTitle() {
328 * Sets the title of this album.
331 * The title of this album
334 public Album setTitle(String title) {
335 Validation.begin().isNotNull("Album Title", title).check();
341 * Returns the description of this album.
343 * @return The description of this album
345 public String getDescription() {
350 * Sets the description of this album.
353 * The description of this album
356 public Album setDescription(String description) {
357 Validation.begin().isNotNull("Album Description", description).check();
358 this.description = description;
363 // FINGERPRINTABLE METHODS
370 public String getFingerprint() {
371 StringBuilder fingerprint = new StringBuilder();
372 fingerprint.append("Album(");
373 fingerprint.append("ID(").append(id).append(')');
374 fingerprint.append("Title(").append(title).append(')');
375 fingerprint.append("Description(").append(description).append(')');
376 if (albumImage != null) {
377 fingerprint.append("AlbumImage(").append(albumImage).append(')');
380 /* add nested albums. */
381 fingerprint.append("Albums(");
382 for (Album album : albums) {
383 fingerprint.append(album.getFingerprint());
385 fingerprint.append(')');
388 fingerprint.append("Images(");
389 for (Image image : getImages()) {
390 if (image.isInserted()) {
391 fingerprint.append(image.getFingerprint());
394 fingerprint.append(')');
396 fingerprint.append(')');
397 return fingerprint.toString();
408 public int hashCode() {
409 return id.hashCode();
416 public boolean equals(Object object) {
417 if (!(object instanceof Album)) {
420 Album album = (Album) object;
421 return id.equals(album.id);