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.Comparator;
22 import java.util.HashMap;
23 import java.util.List;
25 import java.util.UUID;
27 import net.pterodactylus.util.collection.Mapper;
28 import net.pterodactylus.util.collection.Mappers;
29 import net.pterodactylus.util.object.Default;
30 import net.pterodactylus.util.validation.Validation;
33 * Container for images that can also contain nested {@link Album}s.
35 * @author <a href="mailto:bombe@pterodactylus.net">David ‘Bombe’ Roden</a>
37 public class Album implements Fingerprintable {
39 /** Compares two {@link Album}s by {@link #getTitle()}. */
40 public static final Comparator<Album> TITLE_COMPARATOR = new Comparator<Album>() {
43 public int compare(Album leftAlbum, Album rightAlbum) {
44 return leftAlbum.getTitle().compareToIgnoreCase(rightAlbum.getTitle());
48 /** The ID of this album. */
49 private final String id;
51 /** The Sone this album belongs to. */
55 private final List<Album> albums = new ArrayList<Album>();
57 /** The image IDs in order. */
58 private final List<String> imageIds = new ArrayList<String>();
60 /** The images in this album. */
61 private final Map<String, Image> images = new HashMap<String, Image>();
63 /** The parent album. */
66 /** The title of this album. */
69 /** The description of this album. */
70 private String description;
72 /** The ID of the album picture. */
73 private String albumImage;
76 * Creates a new album with a random ID.
79 this(UUID.randomUUID().toString());
83 * Creates a new album with the given ID.
88 public Album(String id) {
89 Validation.begin().isNotNull("Album ID", id).check();
98 * Returns the ID of this album.
100 * @return The ID of this album
102 public String getId() {
107 * Returns the Sone this album belongs to.
109 * @return The Sone this album belongs to
111 public Sone getSone() {
116 * Sets the owner of the album. The owner can only be set as long as the
117 * current owner is {@code null}.
123 public Album setSone(Sone sone) {
124 Validation.begin().isNotNull("New Album Owner", sone).isEither("Old Album Owner", this.sone, null, sone).check();
130 * Returns the nested albums.
132 * @return The nested albums
134 public List<Album> getAlbums() {
135 return new ArrayList<Album>(albums);
139 * Adds an album to this album.
144 public void addAlbum(Album album) {
145 Validation.begin().isNotNull("Album", album).check().isEqual("Album Owner", album.sone, sone).isEither("Old Album Parent", this.parent, null, album.parent).check();
146 album.setParent(this);
147 if (!albums.contains(album)) {
153 * Removes an album from this album.
156 * The album to remove
158 public void removeAlbum(Album album) {
159 Validation.begin().isNotNull("Album", album).check().isEqual("Album Owner", album.sone, sone).isEqual("Album Parent", album.parent, this).check();
160 albums.remove(album);
161 album.removeParent();
165 * Moves the given album up in this album’s albums. If the album is already
166 * the first album, nothing happens.
169 * The album to move up
170 * @return The album that the given album swapped the place with, or
171 * <code>null</code> if the album did not change its place
173 public Album moveAlbumUp(Album album) {
174 Validation.begin().isNotNull("Album", album).check().isEqual("Album Owner", album.sone, sone).isEqual("Album Parent", album.parent, this).check();
175 int oldIndex = albums.indexOf(album);
179 albums.remove(oldIndex);
180 albums.add(oldIndex - 1, album);
181 return albums.get(oldIndex);
185 * Moves the given album down in this album’s albums. If the album is
186 * already the last album, nothing happens.
189 * The album to move down
190 * @return The album that the given album swapped the place with, or
191 * <code>null</code> if the album did not change its place
193 public Album moveAlbumDown(Album album) {
194 Validation.begin().isNotNull("Album", album).check().isEqual("Album Owner", album.sone, sone).isEqual("Album Parent", album.parent, this).check();
195 int oldIndex = albums.indexOf(album);
196 if ((oldIndex < 0) || (oldIndex >= (albums.size() - 1))) {
199 albums.remove(oldIndex);
200 albums.add(oldIndex + 1, album);
201 return albums.get(oldIndex);
205 * Returns the images in this album.
207 * @return The images in this album
209 public List<Image> getImages() {
210 return Mappers.mappedList(imageIds, new Mapper<String, Image>() {
213 @SuppressWarnings("synthetic-access")
214 public Image map(String imageId) {
215 return images.get(imageId);
222 * Adds the given image to this album.
227 public void addImage(Image image) {
228 Validation.begin().isNotNull("Image", image).check().isNotNull("Image Owner", image.getSone()).check().isEqual("Image Owner", image.getSone(), sone).check();
229 if (image.getAlbum() != null) {
230 image.getAlbum().removeImage(image);
232 image.setAlbum(this);
233 if (imageIds.isEmpty() && (albumImage == null)) {
234 albumImage = image.getId();
236 if (!imageIds.contains(image.getId())) {
237 imageIds.add(image.getId());
238 images.put(image.getId(), image);
243 * Removes the given image from this album.
246 * The image to remove
248 public void removeImage(Image image) {
249 Validation.begin().isNotNull("Image", image).check().isEqual("Image Owner", image.getSone(), sone).check();
250 imageIds.remove(image.getId());
251 images.remove(image.getId());
252 if (image.getId().equals(albumImage)) {
253 if (images.isEmpty()) {
256 albumImage = images.values().iterator().next().getId();
262 * Moves the given image up in this album’s images. If the image is already
263 * the first image, nothing happens.
266 * The image to move up
267 * @return The image that the given image swapped the place with, or
268 * <code>null</code> if the image did not change its place
270 public Image moveImageUp(Image image) {
271 Validation.begin().isNotNull("Image", image).check().isEqual("Image Album", image.getAlbum(), this).isEqual("Album Owner", image.getAlbum().getSone(), sone).check();
272 int oldIndex = imageIds.indexOf(image.getId());
276 imageIds.remove(image.getId());
277 imageIds.add(oldIndex - 1, image.getId());
278 return images.get(imageIds.get(oldIndex));
282 * Moves the given image down in this album’s images. If the image is
283 * already the last image, nothing happens.
286 * The image to move down
287 * @return The image that the given image swapped the place with, or
288 * <code>null</code> if the image did not change its place
290 public Image moveImageDown(Image image) {
291 Validation.begin().isNotNull("Image", image).check().isEqual("Image Album", image.getAlbum(), this).isEqual("Album Owner", image.getAlbum().getSone(), sone).check();
292 int oldIndex = imageIds.indexOf(image.getId());
293 if ((oldIndex == -1) || (oldIndex >= (imageIds.size() - 1))) {
296 imageIds.remove(image.getId());
297 imageIds.add(oldIndex + 1, image.getId());
298 return images.get(imageIds.get(oldIndex));
302 * Returns the album image of this album, or {@code null} if no album image
305 * @return The image to show when this album is listed
307 public Image getAlbumImage() {
308 if (albumImage == null) {
311 return Default.forNull(images.get(albumImage), images.values().iterator().next());
315 * Sets the ID of the album image.
318 * The ID of the album image
321 public Album setAlbumImage(String id) {
322 this.albumImage = id;
327 * Returns whether this album contains any other albums or images.
329 * @return {@code true} if this album is empty, {@code false} otherwise
331 public boolean isEmpty() {
332 return albums.isEmpty() && images.isEmpty();
336 * Returns the parent album of this album.
338 * @return The parent album of this album, or {@code null} if this album
339 * does not have a parent
341 public Album getParent() {
346 * Sets the parent album of this album.
349 * The new parent album of this album
352 protected Album setParent(Album parent) {
353 Validation.begin().isNotNull("Album Parent", parent).check();
354 this.parent = parent;
359 * Removes the parent album of this album.
363 protected Album removeParent() {
369 * Returns the title of this album.
371 * @return The title of this album
373 public String getTitle() {
378 * Sets the title of this album.
381 * The title of this album
384 public Album setTitle(String title) {
385 Validation.begin().isNotNull("Album Title", title).check();
391 * Returns the description of this album.
393 * @return The description of this album
395 public String getDescription() {
400 * Sets the description of this album.
403 * The description of this album
406 public Album setDescription(String description) {
407 Validation.begin().isNotNull("Album Description", description).check();
408 this.description = description;
413 // FINGERPRINTABLE METHODS
420 public String getFingerprint() {
421 StringBuilder fingerprint = new StringBuilder();
422 fingerprint.append("Album(");
423 fingerprint.append("ID(").append(id).append(')');
424 fingerprint.append("Title(").append(title).append(')');
425 fingerprint.append("Description(").append(description).append(')');
426 if (albumImage != null) {
427 fingerprint.append("AlbumImage(").append(albumImage).append(')');
430 /* add nested albums. */
431 fingerprint.append("Albums(");
432 for (Album album : albums) {
433 fingerprint.append(album.getFingerprint());
435 fingerprint.append(')');
438 fingerprint.append("Images(");
439 for (Image image : getImages()) {
440 if (image.isInserted()) {
441 fingerprint.append(image.getFingerprint());
444 fingerprint.append(')');
446 fingerprint.append(')');
447 return fingerprint.toString();
458 public int hashCode() {
459 return id.hashCode();
466 public boolean equals(Object object) {
467 if (!(object instanceof Album)) {
470 Album album = (Album) object;
471 return id.equals(album.id);