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.object.Default;
28 import net.pterodactylus.util.validation.Validation;
30 import com.google.common.base.Function;
31 import com.google.common.base.Predicates;
32 import com.google.common.collect.Collections2;
35 * Container for images that can also contain nested {@link Album}s.
37 * @author <a href="mailto:bombe@pterodactylus.net">David ‘Bombe’ Roden</a>
39 public class Album implements Fingerprintable {
41 /** Compares two {@link Album}s by {@link #getTitle()}. */
42 public static final Comparator<Album> TITLE_COMPARATOR = new Comparator<Album>() {
45 public int compare(Album leftAlbum, Album rightAlbum) {
46 return leftAlbum.getTitle().compareToIgnoreCase(rightAlbum.getTitle());
50 /** The ID of this album. */
51 private final String id;
53 /** The Sone this album belongs to. */
57 private final List<Album> albums = new ArrayList<Album>();
59 /** The image IDs in order. */
60 private final List<String> imageIds = new ArrayList<String>();
62 /** The images in this album. */
63 private final Map<String, Image> images = new HashMap<String, Image>();
65 /** The parent album. */
68 /** The title of this album. */
71 /** The description of this album. */
72 private String description;
74 /** The ID of the album picture. */
75 private String albumImage;
78 * Creates a new album with a random ID.
81 this(UUID.randomUUID().toString());
85 * Creates a new album with the given ID.
90 public Album(String id) {
91 Validation.begin().isNotNull("Album ID", id).check();
100 * Returns the ID of this album.
102 * @return The ID of this album
104 public String getId() {
109 * Returns the Sone this album belongs to.
111 * @return The Sone this album belongs to
113 public Sone getSone() {
118 * Sets the owner of the album. The owner can only be set as long as the
119 * current owner is {@code null}.
125 public Album setSone(Sone sone) {
126 Validation.begin().isNotNull("New Album Owner", sone).isEither("Old Album Owner", this.sone, null, sone).check();
132 * Returns the nested albums.
134 * @return The nested albums
136 public List<Album> getAlbums() {
137 return new ArrayList<Album>(albums);
141 * Adds an album to this album.
146 public void addAlbum(Album album) {
147 Validation.begin().isNotNull("Album", album).check().isEqual("Album Owner", album.sone, sone).isEither("Old Album Parent", this.parent, null, album.parent).check();
148 album.setParent(this);
149 if (!albums.contains(album)) {
155 * Removes an album from this album.
158 * The album to remove
160 public void removeAlbum(Album album) {
161 Validation.begin().isNotNull("Album", album).check().isEqual("Album Owner", album.sone, sone).isEqual("Album Parent", album.parent, this).check();
162 albums.remove(album);
163 album.removeParent();
167 * Moves the given album up in this album’s albums. If the album is already
168 * the first album, nothing happens.
171 * The album to move up
172 * @return The album that the given album swapped the place with, or
173 * <code>null</code> if the album did not change its place
175 public Album moveAlbumUp(Album album) {
176 Validation.begin().isNotNull("Album", album).check().isEqual("Album Owner", album.sone, sone).isEqual("Album Parent", album.parent, this).check();
177 int oldIndex = albums.indexOf(album);
181 albums.remove(oldIndex);
182 albums.add(oldIndex - 1, album);
183 return albums.get(oldIndex);
187 * Moves the given album down in this album’s albums. If the album is
188 * already the last album, nothing happens.
191 * The album to move down
192 * @return The album that the given album swapped the place with, or
193 * <code>null</code> if the album did not change its place
195 public Album moveAlbumDown(Album album) {
196 Validation.begin().isNotNull("Album", album).check().isEqual("Album Owner", album.sone, sone).isEqual("Album Parent", album.parent, this).check();
197 int oldIndex = albums.indexOf(album);
198 if ((oldIndex < 0) || (oldIndex >= (albums.size() - 1))) {
201 albums.remove(oldIndex);
202 albums.add(oldIndex + 1, album);
203 return albums.get(oldIndex);
207 * Returns the images in this album.
209 * @return The images in this album
211 public List<Image> getImages() {
212 return new ArrayList<Image>(Collections2.filter(Collections2.transform(imageIds, new Function<String, Image>() {
215 @SuppressWarnings("synthetic-access")
216 public Image apply(String imageId) {
217 return images.get(imageId);
219 }), Predicates.notNull()));
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);