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;
24 import java.util.ArrayList;
25 import java.util.Comparator;
26 import java.util.HashMap;
27 import java.util.List;
29 import java.util.UUID;
31 import net.pterodactylus.util.object.Default;
33 import com.google.common.base.Function;
34 import com.google.common.base.Predicates;
35 import com.google.common.collect.Collections2;
38 * Container for images that can also contain nested {@link Album}s.
40 * @author <a href="mailto:bombe@pterodactylus.net">David ‘Bombe’ Roden</a>
42 public class Album implements Fingerprintable {
44 /** Compares two {@link Album}s by {@link #getTitle()}. */
45 public static final Comparator<Album> TITLE_COMPARATOR = new Comparator<Album>() {
48 public int compare(Album leftAlbum, Album rightAlbum) {
49 return leftAlbum.getTitle().compareToIgnoreCase(rightAlbum.getTitle());
53 /** The ID of this album. */
54 private final String id;
56 /** The Sone this album belongs to. */
60 private final List<Album> albums = new ArrayList<Album>();
62 /** The image IDs in order. */
63 private final List<String> imageIds = new ArrayList<String>();
65 /** The images in this album. */
66 private final Map<String, Image> images = new HashMap<String, Image>();
68 /** The parent album. */
71 /** The title of this album. */
74 /** The description of this album. */
75 private String description;
77 /** The ID of the album picture. */
78 private String albumImage;
81 * Creates a new album with a random ID.
84 this(UUID.randomUUID().toString());
88 * Creates a new album with the given ID.
93 public Album(String id) {
94 this.id = checkNotNull(id, "id must not be null");
102 * Returns the ID of this album.
104 * @return The ID of this album
106 public String getId() {
111 * Returns the Sone this album belongs to.
113 * @return The Sone this album belongs to
115 public Sone getSone() {
120 * Sets the owner of the album. The owner can only be set as long as the
121 * current owner is {@code null}.
127 public Album setSone(Sone sone) {
128 checkNotNull(sone, "sone must not be null");
129 checkState((this.sone == null) || (this.sone.equals(sone)), "album owner must not already be set to some other Sone");
135 * Returns the nested albums.
137 * @return The nested albums
139 public List<Album> getAlbums() {
140 return new ArrayList<Album>(albums);
144 * Adds an album to this album.
149 public void addAlbum(Album album) {
150 checkNotNull(album, "album must not be null");
151 checkArgument(album.getSone().equals(sone), "album must belong to the same Sone as this album");
152 checkState((this.parent == null) || (this.parent.equals(album.parent)), "album must not already be set to some other Sone");
153 album.setParent(this);
154 if (!albums.contains(album)) {
160 * Removes an album from this album.
163 * The album to remove
165 public void removeAlbum(Album album) {
166 checkNotNull(album, "album must not be null");
167 checkArgument(album.sone.equals(sone), "album must belong this album’s Sone");
168 checkArgument(equals(album.parent), "album must belong to this album");
169 albums.remove(album);
170 album.removeParent();
174 * Moves the given album up in this album’s albums. If the album is already
175 * the first album, nothing happens.
178 * The album to move up
179 * @return The album that the given album swapped the place with, or
180 * <code>null</code> if the album did not change its place
182 public Album moveAlbumUp(Album album) {
183 checkNotNull(album, "album must not be null");
184 checkArgument(album.sone.equals(sone), "album must belong to the same Sone as this album");
185 checkArgument(equals(album.parent), "album must belong to this album");
186 int oldIndex = albums.indexOf(album);
190 albums.remove(oldIndex);
191 albums.add(oldIndex - 1, album);
192 return albums.get(oldIndex);
196 * Moves the given album down in this album’s albums. If the album is
197 * already the last album, nothing happens.
200 * The album to move down
201 * @return The album that the given album swapped the place with, or
202 * <code>null</code> if the album did not change its place
204 public Album moveAlbumDown(Album album) {
205 checkNotNull(album, "album must not be null");
206 checkArgument(album.sone.equals(sone), "album must belong to the same Sone as this album");
207 checkArgument(equals(album.parent), "album must belong to this album");
208 int oldIndex = albums.indexOf(album);
209 if ((oldIndex < 0) || (oldIndex >= (albums.size() - 1))) {
212 albums.remove(oldIndex);
213 albums.add(oldIndex + 1, album);
214 return albums.get(oldIndex);
218 * Returns the images in this album.
220 * @return The images in this album
222 public List<Image> getImages() {
223 return new ArrayList<Image>(Collections2.filter(Collections2.transform(imageIds, new Function<String, Image>() {
226 @SuppressWarnings("synthetic-access")
227 public Image apply(String imageId) {
228 return images.get(imageId);
230 }), Predicates.notNull()));
234 * Adds the given image to this album.
239 public void addImage(Image image) {
240 checkNotNull(image, "image must not be null");
241 checkNotNull(image.getSone(), "image must have an owner");
242 checkArgument(image.getSone().equals(sone), "image must belong to the same Sone as this album");
243 if (image.getAlbum() != null) {
244 image.getAlbum().removeImage(image);
246 image.setAlbum(this);
247 if (imageIds.isEmpty() && (albumImage == null)) {
248 albumImage = image.getId();
250 if (!imageIds.contains(image.getId())) {
251 imageIds.add(image.getId());
252 images.put(image.getId(), image);
257 * Removes the given image from this album.
260 * The image to remove
262 public void removeImage(Image image) {
263 checkNotNull(image, "image must not be null");
264 checkNotNull(image.getSone(), "image must have an owner");
265 checkArgument(image.getSone().equals(sone), "image must belong to the same Sone as this album");
266 imageIds.remove(image.getId());
267 images.remove(image.getId());
268 if (image.getId().equals(albumImage)) {
269 if (images.isEmpty()) {
272 albumImage = images.values().iterator().next().getId();
278 * Moves the given image up in this album’s images. If the image is already
279 * the first image, nothing happens.
282 * The image to move up
283 * @return The image that the given image swapped the place with, or
284 * <code>null</code> if the image did not change its place
286 public Image moveImageUp(Image image) {
287 checkNotNull(image, "image must not be null");
288 checkNotNull(image.getSone(), "image must have an owner");
289 checkArgument(image.getSone().equals(sone), "image must belong to the same Sone as this album");
290 checkArgument(image.getAlbum().equals(this), "image must belong to this album");
291 int oldIndex = imageIds.indexOf(image.getId());
295 imageIds.remove(image.getId());
296 imageIds.add(oldIndex - 1, image.getId());
297 return images.get(imageIds.get(oldIndex));
301 * Moves the given image down in this album’s images. If the image is
302 * already the last image, nothing happens.
305 * The image to move down
306 * @return The image that the given image swapped the place with, or
307 * <code>null</code> if the image did not change its place
309 public Image moveImageDown(Image image) {
310 checkNotNull(image, "image must not be null");
311 checkNotNull(image.getSone(), "image must have an owner");
312 checkArgument(image.getSone().equals(sone), "image must belong to the same Sone as this album");
313 checkArgument(image.getAlbum().equals(this), "image must belong to this album");
314 int oldIndex = imageIds.indexOf(image.getId());
315 if ((oldIndex == -1) || (oldIndex >= (imageIds.size() - 1))) {
318 imageIds.remove(image.getId());
319 imageIds.add(oldIndex + 1, image.getId());
320 return images.get(imageIds.get(oldIndex));
324 * Returns the album image of this album, or {@code null} if no album image
327 * @return The image to show when this album is listed
329 public Image getAlbumImage() {
330 if (albumImage == null) {
333 return Default.forNull(images.get(albumImage), images.values().iterator().next());
337 * Sets the ID of the album image.
340 * The ID of the album image
343 public Album setAlbumImage(String id) {
344 this.albumImage = id;
349 * Returns whether this album contains any other albums or images.
351 * @return {@code true} if this album is empty, {@code false} otherwise
353 public boolean isEmpty() {
354 return albums.isEmpty() && images.isEmpty();
358 * Returns the parent album of this album.
360 * @return The parent album of this album, or {@code null} if this album
361 * does not have a parent
363 public Album getParent() {
368 * Sets the parent album of this album.
371 * The new parent album of this album
374 protected Album setParent(Album parent) {
375 this.parent = checkNotNull(parent, "parent must not be null");
380 * Removes the parent album of this album.
384 protected Album removeParent() {
390 * Returns the title of this album.
392 * @return The title of this album
394 public String getTitle() {
399 * Sets the title of this album.
402 * The title of this album
405 public Album setTitle(String title) {
406 this.title = checkNotNull(title, "title must not be null");
411 * Returns the description of this album.
413 * @return The description of this album
415 public String getDescription() {
420 * Sets the description of this album.
423 * The description of this album
426 public Album setDescription(String description) {
427 this.description = checkNotNull(description, "description must not be null");
432 // FINGERPRINTABLE METHODS
439 public String getFingerprint() {
440 StringBuilder fingerprint = new StringBuilder();
441 fingerprint.append("Album(");
442 fingerprint.append("ID(").append(id).append(')');
443 fingerprint.append("Title(").append(title).append(')');
444 fingerprint.append("Description(").append(description).append(')');
445 if (albumImage != null) {
446 fingerprint.append("AlbumImage(").append(albumImage).append(')');
449 /* add nested albums. */
450 fingerprint.append("Albums(");
451 for (Album album : albums) {
452 fingerprint.append(album.getFingerprint());
454 fingerprint.append(')');
457 fingerprint.append("Images(");
458 for (Image image : getImages()) {
459 if (image.isInserted()) {
460 fingerprint.append(image.getFingerprint());
463 fingerprint.append(')');
465 fingerprint.append(')');
466 return fingerprint.toString();
477 public int hashCode() {
478 return id.hashCode();
485 public boolean equals(Object object) {
486 if (!(object instanceof Album)) {
489 Album album = (Album) object;
490 return id.equals(album.id);