2 * Sone - Album.java - Copyright © 2011–2013 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 static com.google.common.base.Preconditions.checkArgument;
21 import static com.google.common.base.Preconditions.checkNotNull;
22 import static com.google.common.base.Preconditions.checkState;
23 import static java.util.Arrays.asList;
25 import java.util.ArrayList;
26 import java.util.Comparator;
27 import java.util.HashMap;
28 import java.util.List;
30 import java.util.UUID;
32 import com.google.common.base.Function;
33 import com.google.common.base.Optional;
34 import com.google.common.base.Predicate;
35 import com.google.common.base.Predicates;
36 import com.google.common.collect.Collections2;
37 import com.google.common.collect.FluentIterable;
38 import com.google.common.collect.ImmutableList;
39 import com.google.common.hash.Hasher;
40 import com.google.common.hash.Hashing;
43 * Container for images that can also contain nested {@link Album}s.
45 * @author <a href="mailto:bombe@pterodactylus.net">David ‘Bombe’ Roden</a>
47 public class Album implements Fingerprintable {
49 /** Compares two {@link Album}s by {@link #getTitle()}. */
50 public static final Comparator<Album> TITLE_COMPARATOR = new Comparator<Album>() {
53 public int compare(Album leftAlbum, Album rightAlbum) {
54 return leftAlbum.getTitle().compareToIgnoreCase(rightAlbum.getTitle());
58 /** Function that flattens the given album and all albums beneath it. */
59 public static final Function<Album, List<Album>> FLATTENER = new Function<Album, List<Album>>() {
62 public List<Album> apply(Album album) {
63 List<Album> albums = new ArrayList<Album>();
65 for (Album subAlbum : album.getAlbums()) {
66 albums.addAll(FluentIterable.from(ImmutableList.of(subAlbum)).transformAndConcat(FLATTENER).toList());
73 * Filter that removes all albums that do not have any images in any album
76 public static final Predicate<Album> NOT_EMPTY = new Predicate<Album>() {
79 public boolean apply(Album album) {
80 return FluentIterable.from(asList(album)).transformAndConcat(FLATTENER).anyMatch(new Predicate<Album>() {
83 public boolean apply(Album album) {
84 return !album.getImages().isEmpty();
90 /** The ID of this album. */
91 private final String id;
93 /** The Sone this album belongs to. */
97 private final List<Album> albums = new ArrayList<Album>();
99 /** The image IDs in order. */
100 private final List<String> imageIds = new ArrayList<String>();
102 /** The images in this album. */
103 private final Map<String, Image> images = new HashMap<String, Image>();
105 /** The parent album. */
106 private Album parent;
108 /** The title of this album. */
109 private String title;
111 /** The description of this album. */
112 private String description;
114 /** The ID of the album picture. */
115 private String albumImage;
118 * Creates a new album with a random ID.
121 this(UUID.randomUUID().toString());
125 * Creates a new album with the given ID.
128 * The ID of the album
130 public Album(String id) {
131 this.id = checkNotNull(id, "id must not be null");
139 * Returns the ID of this album.
141 * @return The ID of this album
143 public String getId() {
148 * Returns the Sone this album belongs to.
150 * @return The Sone this album belongs to
152 public Sone getSone() {
157 * Sets the owner of the album. The owner can only be set as long as the
158 * current owner is {@code null}.
164 public Album setSone(Sone sone) {
165 checkNotNull(sone, "sone must not be null");
166 checkState((this.sone == null) || (this.sone.equals(sone)), "album owner must not already be set to some other Sone");
172 * Returns the nested albums.
174 * @return The nested albums
176 public List<Album> getAlbums() {
177 return new ArrayList<Album>(albums);
181 * Adds an album to this album.
186 public void addAlbum(Album album) {
187 checkNotNull(album, "album must not be null");
188 checkArgument(album.getSone().equals(sone), "album must belong to the same Sone as this album");
189 checkState((this.parent == null) || (this.parent.equals(album.parent)), "album must not already be set to some other Sone");
190 album.setParent(this);
191 if (!albums.contains(album)) {
197 * Removes an album from this album.
200 * The album to remove
202 public void removeAlbum(Album album) {
203 checkNotNull(album, "album must not be null");
204 checkArgument(album.sone.equals(sone), "album must belong this album’s Sone");
205 checkArgument(equals(album.parent), "album must belong to this album");
206 albums.remove(album);
207 album.removeParent();
211 * Moves the given album up in this album’s albums. If the album is already
212 * the first album, nothing happens.
215 * The album to move up
216 * @return The album that the given album swapped the place with, or
217 * <code>null</code> if the album did not change its place
219 public Album moveAlbumUp(Album album) {
220 checkNotNull(album, "album must not be null");
221 checkArgument(album.sone.equals(sone), "album must belong to the same Sone as this album");
222 checkArgument(equals(album.parent), "album must belong to this album");
223 int oldIndex = albums.indexOf(album);
227 albums.remove(oldIndex);
228 albums.add(oldIndex - 1, album);
229 return albums.get(oldIndex);
233 * Moves the given album down in this album’s albums. If the album is
234 * already the last album, nothing happens.
237 * The album to move down
238 * @return The album that the given album swapped the place with, or
239 * <code>null</code> if the album did not change its place
241 public Album moveAlbumDown(Album album) {
242 checkNotNull(album, "album must not be null");
243 checkArgument(album.sone.equals(sone), "album must belong to the same Sone as this album");
244 checkArgument(equals(album.parent), "album must belong to this album");
245 int oldIndex = albums.indexOf(album);
246 if ((oldIndex < 0) || (oldIndex >= (albums.size() - 1))) {
249 albums.remove(oldIndex);
250 albums.add(oldIndex + 1, album);
251 return albums.get(oldIndex);
255 * Returns the images in this album.
257 * @return The images in this album
259 public List<Image> getImages() {
260 return new ArrayList<Image>(Collections2.filter(Collections2.transform(imageIds, new Function<String, Image>() {
263 @SuppressWarnings("synthetic-access")
264 public Image apply(String imageId) {
265 return images.get(imageId);
267 }), Predicates.notNull()));
271 * Adds the given image to this album.
276 public void addImage(Image image) {
277 checkNotNull(image, "image must not be null");
278 checkNotNull(image.getSone(), "image must have an owner");
279 checkArgument(image.getSone().equals(sone), "image must belong to the same Sone as this album");
280 if (image.getAlbum() != null) {
281 image.getAlbum().removeImage(image);
283 image.setAlbum(this);
284 if (imageIds.isEmpty() && (albumImage == null)) {
285 albumImage = image.getId();
287 if (!imageIds.contains(image.getId())) {
288 imageIds.add(image.getId());
289 images.put(image.getId(), image);
294 * Removes the given image from this album.
297 * The image to remove
299 public void removeImage(Image image) {
300 checkNotNull(image, "image must not be null");
301 checkNotNull(image.getSone(), "image must have an owner");
302 checkArgument(image.getSone().equals(sone), "image must belong to the same Sone as this album");
303 imageIds.remove(image.getId());
304 images.remove(image.getId());
305 if (image.getId().equals(albumImage)) {
306 if (images.isEmpty()) {
309 albumImage = images.values().iterator().next().getId();
315 * Moves the given image up in this album’s images. If the image is already
316 * the first image, nothing happens.
319 * The image to move up
320 * @return The image that the given image swapped the place with, or
321 * <code>null</code> if the image did not change its place
323 public Image moveImageUp(Image image) {
324 checkNotNull(image, "image must not be null");
325 checkNotNull(image.getSone(), "image must have an owner");
326 checkArgument(image.getSone().equals(sone), "image must belong to the same Sone as this album");
327 checkArgument(image.getAlbum().equals(this), "image must belong to this album");
328 int oldIndex = imageIds.indexOf(image.getId());
332 imageIds.remove(image.getId());
333 imageIds.add(oldIndex - 1, image.getId());
334 return images.get(imageIds.get(oldIndex));
338 * Moves the given image down in this album’s images. If the image is
339 * already the last image, nothing happens.
342 * The image to move down
343 * @return The image that the given image swapped the place with, or
344 * <code>null</code> if the image did not change its place
346 public Image moveImageDown(Image image) {
347 checkNotNull(image, "image must not be null");
348 checkNotNull(image.getSone(), "image must have an owner");
349 checkArgument(image.getSone().equals(sone), "image must belong to the same Sone as this album");
350 checkArgument(image.getAlbum().equals(this), "image must belong to this album");
351 int oldIndex = imageIds.indexOf(image.getId());
352 if ((oldIndex == -1) || (oldIndex >= (imageIds.size() - 1))) {
355 imageIds.remove(image.getId());
356 imageIds.add(oldIndex + 1, image.getId());
357 return images.get(imageIds.get(oldIndex));
361 * Returns the album image of this album, or {@code null} if no album image
364 * @return The image to show when this album is listed
366 public Image getAlbumImage() {
367 if (albumImage == null) {
370 return Optional.fromNullable(images.get(albumImage)).or(images.values().iterator().next());
374 * Sets the ID of the album image.
377 * The ID of the album image
380 public Album setAlbumImage(String id) {
381 this.albumImage = id;
386 * Returns whether this album contains any other albums or images.
388 * @return {@code true} if this album is empty, {@code false} otherwise
390 public boolean isEmpty() {
391 return albums.isEmpty() && images.isEmpty();
395 * Returns the parent album of this album.
397 * @return The parent album of this album, or {@code null} if this album
398 * does not have a parent
400 public Album getParent() {
405 * Sets the parent album of this album.
408 * The new parent album of this album
411 protected Album setParent(Album parent) {
412 this.parent = checkNotNull(parent, "parent must not be null");
417 * Removes the parent album of this album.
421 protected Album removeParent() {
427 * Returns the title of this album.
429 * @return The title of this album
431 public String getTitle() {
436 * Sets the title of this album.
439 * The title of this album
442 public Album setTitle(String title) {
443 this.title = checkNotNull(title, "title must not be null");
448 * Returns the description of this album.
450 * @return The description of this album
452 public String getDescription() {
457 * Sets the description of this album.
460 * The description of this album
463 public Album setDescription(String description) {
464 this.description = checkNotNull(description, "description must not be null");
469 // FINGERPRINTABLE METHODS
476 public String getFingerprint() {
477 Hasher hash = Hashing.sha256().newHasher();
478 hash.putString("Album(");
479 hash.putString("ID(").putString(id).putString(")");
480 hash.putString("Title(").putString(title).putString(")");
481 hash.putString("Description(").putString(description).putString(")");
482 if (albumImage != null) {
483 hash.putString("AlbumImage(").putString(albumImage).putString(")");
486 /* add nested albums. */
487 hash.putString("Albums(");
488 for (Album album : albums) {
489 hash.putString(album.getFingerprint());
494 hash.putString("Images(");
495 for (Image image : getImages()) {
496 if (image.isInserted()) {
497 hash.putString(image.getFingerprint());
503 return hash.hash().toString();
514 public int hashCode() {
515 return id.hashCode();
522 public boolean equals(Object object) {
523 if (!(object instanceof Album)) {
526 Album album = (Album) object;
527 return id.equals(album.id);