/*
- * Sone - Album.java - Copyright © 2011 David Roden
+ * Sone - Album.java - Copyright © 2011–2013 David Roden
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
package net.pterodactylus.sone.data;
+import static com.google.common.base.Preconditions.checkArgument;
+import static com.google.common.base.Preconditions.checkNotNull;
+import static com.google.common.base.Preconditions.checkState;
+
import java.util.ArrayList;
+import java.util.Comparator;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.UUID;
-import net.pterodactylus.util.collection.Mapper;
-import net.pterodactylus.util.collection.Mappers;
-import net.pterodactylus.util.object.Default;
-import net.pterodactylus.util.validation.Validation;
+import com.google.common.base.Function;
+import com.google.common.base.Optional;
+import com.google.common.base.Predicates;
+import com.google.common.collect.Collections2;
/**
* Container for images that can also contain nested {@link Album}s.
*/
public class Album implements Fingerprintable {
+ /** Compares two {@link Album}s by {@link #getTitle()}. */
+ public static final Comparator<Album> TITLE_COMPARATOR = new Comparator<Album>() {
+
+ @Override
+ public int compare(Album leftAlbum, Album rightAlbum) {
+ return leftAlbum.getTitle().compareToIgnoreCase(rightAlbum.getTitle());
+ }
+ };
+
/** The ID of this album. */
private final String id;
* The ID of the album
*/
public Album(String id) {
- Validation.begin().isNotNull("Album ID", id).check();
- this.id = id;
+ this.id = checkNotNull(id, "id must not be null");
}
//
* @return This album
*/
public Album setSone(Sone sone) {
- Validation.begin().isNotNull("New Album Owner", sone).isEither("Old Album Owner", this.sone, null, sone).check();
+ checkNotNull(sone, "sone must not be null");
+ checkState((this.sone == null) || (this.sone.equals(sone)), "album owner must not already be set to some other Sone");
this.sone = sone;
return this;
}
* The album to add
*/
public void addAlbum(Album album) {
- Validation.begin().isNotNull("Album", album).check().isEqual("Album Owner", album.sone, sone).isEither("Old Album Parent", this.parent, null, album.parent).check();
+ checkNotNull(album, "album must not be null");
+ checkArgument(album.getSone().equals(sone), "album must belong to the same Sone as this album");
+ checkState((this.parent == null) || (this.parent.equals(album.parent)), "album must not already be set to some other Sone");
album.setParent(this);
if (!albums.contains(album)) {
albums.add(album);
* The album to remove
*/
public void removeAlbum(Album album) {
- Validation.begin().isNotNull("Album", album).check().isEqual("Album Owner", album.sone, sone).isEqual("Album Parent", album.parent, this).check();
+ checkNotNull(album, "album must not be null");
+ checkArgument(album.sone.equals(sone), "album must belong this album’s Sone");
+ checkArgument(equals(album.parent), "album must belong to this album");
albums.remove(album);
album.removeParent();
}
/**
+ * Moves the given album up in this album’s albums. If the album is already
+ * the first album, nothing happens.
+ *
+ * @param album
+ * The album to move up
+ * @return The album that the given album swapped the place with, or
+ * <code>null</code> if the album did not change its place
+ */
+ public Album moveAlbumUp(Album album) {
+ checkNotNull(album, "album must not be null");
+ checkArgument(album.sone.equals(sone), "album must belong to the same Sone as this album");
+ checkArgument(equals(album.parent), "album must belong to this album");
+ int oldIndex = albums.indexOf(album);
+ if (oldIndex <= 0) {
+ return null;
+ }
+ albums.remove(oldIndex);
+ albums.add(oldIndex - 1, album);
+ return albums.get(oldIndex);
+ }
+
+ /**
+ * Moves the given album down in this album’s albums. If the album is
+ * already the last album, nothing happens.
+ *
+ * @param album
+ * The album to move down
+ * @return The album that the given album swapped the place with, or
+ * <code>null</code> if the album did not change its place
+ */
+ public Album moveAlbumDown(Album album) {
+ checkNotNull(album, "album must not be null");
+ checkArgument(album.sone.equals(sone), "album must belong to the same Sone as this album");
+ checkArgument(equals(album.parent), "album must belong to this album");
+ int oldIndex = albums.indexOf(album);
+ if ((oldIndex < 0) || (oldIndex >= (albums.size() - 1))) {
+ return null;
+ }
+ albums.remove(oldIndex);
+ albums.add(oldIndex + 1, album);
+ return albums.get(oldIndex);
+ }
+
+ /**
* Returns the images in this album.
*
* @return The images in this album
*/
public List<Image> getImages() {
- return Mappers.mappedList(imageIds, new Mapper<String, Image>() {
+ return new ArrayList<Image>(Collections2.filter(Collections2.transform(imageIds, new Function<String, Image>() {
@Override
@SuppressWarnings("synthetic-access")
- public Image map(String imageId) {
+ public Image apply(String imageId) {
return images.get(imageId);
}
-
- });
+ }), Predicates.notNull()));
}
/**
* The image to add
*/
public void addImage(Image image) {
- Validation.begin().isNotNull("Image", image).check().isNotNull("Image Owner", image.getSone()).check().isEqual("Image Owner", image.getSone(), sone).check();
+ checkNotNull(image, "image must not be null");
+ checkNotNull(image.getSone(), "image must have an owner");
+ checkArgument(image.getSone().equals(sone), "image must belong to the same Sone as this album");
if (image.getAlbum() != null) {
image.getAlbum().removeImage(image);
}
* The image to remove
*/
public void removeImage(Image image) {
- Validation.begin().isNotNull("Image", image).check().isEqual("Image Owner", image.getSone(), sone).check();
+ checkNotNull(image, "image must not be null");
+ checkNotNull(image.getSone(), "image must have an owner");
+ checkArgument(image.getSone().equals(sone), "image must belong to the same Sone as this album");
imageIds.remove(image.getId());
images.remove(image.getId());
if (image.getId().equals(albumImage)) {
* <code>null</code> if the image did not change its place
*/
public Image moveImageUp(Image image) {
- Validation.begin().isNotNull("Image", image).check().isEqual("Image Album", image.getAlbum(), this).isEqual("Album Owner", image.getAlbum().getSone(), sone).check();
+ checkNotNull(image, "image must not be null");
+ checkNotNull(image.getSone(), "image must have an owner");
+ checkArgument(image.getSone().equals(sone), "image must belong to the same Sone as this album");
+ checkArgument(image.getAlbum().equals(this), "image must belong to this album");
int oldIndex = imageIds.indexOf(image.getId());
if (oldIndex <= 0) {
return null;
}
/**
- * Move the given image down in this album’s images. If the image is already
- * the last image, nothing happens.
+ * Moves the given image down in this album’s images. If the image is
+ * already the last image, nothing happens.
*
* @param image
* The image to move down
* <code>null</code> if the image did not change its place
*/
public Image moveImageDown(Image image) {
- Validation.begin().isNotNull("Image", image).check().isEqual("Image Album", image.getAlbum(), this).isEqual("Album Owner", image.getAlbum().getSone(), sone).check();
+ checkNotNull(image, "image must not be null");
+ checkNotNull(image.getSone(), "image must have an owner");
+ checkArgument(image.getSone().equals(sone), "image must belong to the same Sone as this album");
+ checkArgument(image.getAlbum().equals(this), "image must belong to this album");
int oldIndex = imageIds.indexOf(image.getId());
if ((oldIndex == -1) || (oldIndex >= (imageIds.size() - 1))) {
return null;
if (albumImage == null) {
return null;
}
- return Default.forNull(images.get(albumImage), images.values().iterator().next());
+ return Optional.fromNullable(images.get(albumImage)).or(images.values().iterator().next());
}
/**
* @return This album
*/
protected Album setParent(Album parent) {
- Validation.begin().isNotNull("Album Parent", parent).check();
- this.parent = parent;
+ this.parent = checkNotNull(parent, "parent must not be null");
return this;
}
* @return This album
*/
public Album setTitle(String title) {
- Validation.begin().isNotNull("Album Title", title).check();
- this.title = title;
+ this.title = checkNotNull(title, "title must not be null");
return this;
}
* @return This album
*/
public Album setDescription(String description) {
- Validation.begin().isNotNull("Album Description", description).check();
- this.description = description;
+ this.description = checkNotNull(description, "description must not be null");
return this;
}