2 * Sone - Album.java - Copyright © 2011–2012 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.IterableWrapper;
28 import net.pterodactylus.util.collection.filter.NotNullFilter;
29 import net.pterodactylus.util.collection.mapper.Mapper;
30 import net.pterodactylus.util.object.Default;
31 import net.pterodactylus.util.validation.Validation;
34 * Container for images that can also contain nested {@link Album}s.
36 * @author <a href="mailto:bombe@pterodactylus.net">David ‘Bombe’ Roden</a>
38 public class Album implements Fingerprintable {
40 /** Compares two {@link Album}s by {@link #getTitle()}. */
41 public static final Comparator<Album> TITLE_COMPARATOR = new Comparator<Album>() {
44 public int compare(Album leftAlbum, Album rightAlbum) {
45 return leftAlbum.getTitle().compareToIgnoreCase(rightAlbum.getTitle());
49 /** The ID of this album. */
50 private final String id;
52 /** The Sone this album belongs to. */
56 private final List<Album> albums = new ArrayList<Album>();
58 /** The image IDs in order. */
59 private final List<String> imageIds = new ArrayList<String>();
61 /** The images in this album. */
62 private final Map<String, Image> images = new HashMap<String, Image>();
64 /** The parent album. */
67 /** The title of this album. */
70 /** The description of this album. */
71 private String description;
73 /** The ID of the album picture. */
74 private String albumImage;
77 * Creates a new album with a random ID.
80 this(UUID.randomUUID().toString());
84 * Creates a new album with the given ID.
89 public Album(String id) {
90 Validation.begin().isNotNull("Album ID", id).check();
99 * Returns the ID of this album.
101 * @return The ID of this album
103 public String getId() {
108 * Returns the Sone this album belongs to.
110 * @return The Sone this album belongs to
112 public Sone getSone() {
117 * Sets the owner of the album. The owner can only be set as long as the
118 * current owner is {@code null}.
124 public Album setSone(Sone sone) {
125 Validation.begin().isNotNull("New Album Owner", sone).isEither("Old Album Owner", this.sone, null, sone).check();
131 * Returns the nested albums.
133 * @return The nested albums
135 public List<Album> getAlbums() {
136 return new ArrayList<Album>(albums);
140 * Adds an album to this album.
145 public void addAlbum(Album album) {
146 Validation.begin().isNotNull("Album", album).check().isEqual("Album Owner", album.sone, sone).isEither("Old Album Parent", this.parent, null, album.parent).check();
147 album.setParent(this);
148 if (!albums.contains(album)) {
154 * Removes an album from this album.
157 * The album to remove
159 public void removeAlbum(Album album) {
160 Validation.begin().isNotNull("Album", album).check().isEqual("Album Owner", album.sone, sone).isEqual("Album Parent", album.parent, this).check();
161 albums.remove(album);
162 album.removeParent();
166 * Moves the given album up in this album’s albums. If the album is already
167 * the first album, nothing happens.
170 * The album to move up
171 * @return The album that the given album swapped the place with, or
172 * <code>null</code> if the album did not change its place
174 public Album moveAlbumUp(Album album) {
175 Validation.begin().isNotNull("Album", album).check().isEqual("Album Owner", album.sone, sone).isEqual("Album Parent", album.parent, this).check();
176 int oldIndex = albums.indexOf(album);
180 albums.remove(oldIndex);
181 albums.add(oldIndex - 1, album);
182 return albums.get(oldIndex);
186 * Moves the given album down in this album’s albums. If the album is
187 * already the last album, nothing happens.
190 * The album to move down
191 * @return The album that the given album swapped the place with, or
192 * <code>null</code> if the album did not change its place
194 public Album moveAlbumDown(Album album) {
195 Validation.begin().isNotNull("Album", album).check().isEqual("Album Owner", album.sone, sone).isEqual("Album Parent", album.parent, this).check();
196 int oldIndex = albums.indexOf(album);
197 if ((oldIndex < 0) || (oldIndex >= (albums.size() - 1))) {
200 albums.remove(oldIndex);
201 albums.add(oldIndex + 1, album);
202 return albums.get(oldIndex);
206 * Returns the images in this album.
208 * @return The images in this album
210 public List<Image> getImages() {
211 return IterableWrapper.wrap(imageIds).map(new Mapper<String, Image>() {
214 @SuppressWarnings("synthetic-access")
215 public Image map(String imageId) {
216 return images.get(imageId);
219 }).filter(new NotNullFilter()).list();
223 * Adds the given image to this album.
228 public void addImage(Image image) {
229 Validation.begin().isNotNull("Image", image).check().isNotNull("Image Owner", image.getSone()).check().isEqual("Image Owner", image.getSone(), sone).check();
230 if (image.getAlbum() != null) {
231 image.getAlbum().removeImage(image);
233 image.setAlbum(this);
234 if (imageIds.isEmpty() && (albumImage == null)) {
235 albumImage = image.getId();
237 if (!imageIds.contains(image.getId())) {
238 imageIds.add(image.getId());
239 images.put(image.getId(), image);
244 * Removes the given image from this album.
247 * The image to remove
249 public void removeImage(Image image) {
250 Validation.begin().isNotNull("Image", image).check().isEqual("Image Owner", image.getSone(), sone).check();
251 imageIds.remove(image.getId());
252 images.remove(image.getId());
253 if (image.getId().equals(albumImage)) {
254 if (images.isEmpty()) {
257 albumImage = images.values().iterator().next().getId();
263 * Moves the given image up in this album’s images. If the image is already
264 * the first image, nothing happens.
267 * The image to move up
268 * @return The image that the given image swapped the place with, or
269 * <code>null</code> if the image did not change its place
271 public Image moveImageUp(Image image) {
272 Validation.begin().isNotNull("Image", image).check().isEqual("Image Album", image.getAlbum(), this).isEqual("Album Owner", image.getAlbum().getSone(), sone).check();
273 int oldIndex = imageIds.indexOf(image.getId());
277 imageIds.remove(image.getId());
278 imageIds.add(oldIndex - 1, image.getId());
279 return images.get(imageIds.get(oldIndex));
283 * Moves the given image down in this album’s images. If the image is
284 * already the last image, nothing happens.
287 * The image to move down
288 * @return The image that the given image swapped the place with, or
289 * <code>null</code> if the image did not change its place
291 public Image moveImageDown(Image image) {
292 Validation.begin().isNotNull("Image", image).check().isEqual("Image Album", image.getAlbum(), this).isEqual("Album Owner", image.getAlbum().getSone(), sone).check();
293 int oldIndex = imageIds.indexOf(image.getId());
294 if ((oldIndex == -1) || (oldIndex >= (imageIds.size() - 1))) {
297 imageIds.remove(image.getId());
298 imageIds.add(oldIndex + 1, image.getId());
299 return images.get(imageIds.get(oldIndex));
303 * Returns the album image of this album, or {@code null} if no album image
306 * @return The image to show when this album is listed
308 public Image getAlbumImage() {
309 if (albumImage == null) {
312 return Default.forNull(images.get(albumImage), images.values().iterator().next());
316 * Sets the ID of the album image.
319 * The ID of the album image
322 public Album setAlbumImage(String id) {
323 this.albumImage = id;
328 * Returns whether this album contains any other albums or images.
330 * @return {@code true} if this album is empty, {@code false} otherwise
332 public boolean isEmpty() {
333 return albums.isEmpty() && images.isEmpty();
337 * Returns the parent album of this album.
339 * @return The parent album of this album, or {@code null} if this album
340 * does not have a parent
342 public Album getParent() {
347 * Sets the parent album of this album.
350 * The new parent album of this album
353 protected Album setParent(Album parent) {
354 Validation.begin().isNotNull("Album Parent", parent).check();
355 this.parent = parent;
360 * Removes the parent album of this album.
364 protected Album removeParent() {
370 * Returns the title of this album.
372 * @return The title of this album
374 public String getTitle() {
379 * Sets the title of this album.
382 * The title of this album
385 public Album setTitle(String title) {
386 Validation.begin().isNotNull("Album Title", title).check();
392 * Returns the description of this album.
394 * @return The description of this album
396 public String getDescription() {
401 * Sets the description of this album.
404 * The description of this album
407 public Album setDescription(String description) {
408 Validation.begin().isNotNull("Album Description", description).check();
409 this.description = description;
414 // FINGERPRINTABLE METHODS
421 public String getFingerprint() {
422 StringBuilder fingerprint = new StringBuilder();
423 fingerprint.append("Album(");
424 fingerprint.append("ID(").append(id).append(')');
425 fingerprint.append("Title(").append(title).append(')');
426 fingerprint.append("Description(").append(description).append(')');
427 if (albumImage != null) {
428 fingerprint.append("AlbumImage(").append(albumImage).append(')');
431 /* add nested albums. */
432 fingerprint.append("Albums(");
433 for (Album album : albums) {
434 fingerprint.append(album.getFingerprint());
436 fingerprint.append(')');
439 fingerprint.append("Images(");
440 for (Image image : getImages()) {
441 if (image.isInserted()) {
442 fingerprint.append(image.getFingerprint());
445 fingerprint.append(')');
447 fingerprint.append(')');
448 return fingerprint.toString();
459 public int hashCode() {
460 return id.hashCode();
467 public boolean equals(Object object) {
468 if (!(object instanceof Album)) {
471 Album album = (Album) object;
472 return id.equals(album.id);