package net.pterodactylus.sone.data.impl;
-import static com.google.common.base.Preconditions.checkArgument;
-import static com.google.common.base.Preconditions.checkNotNull;
+import static com.google.common.base.Optional.absent;
+import static com.google.common.base.Optional.fromNullable;
import static com.google.common.base.Preconditions.checkState;
import java.util.ArrayList;
import net.pterodactylus.sone.data.Album;
import net.pterodactylus.sone.data.Image;
import net.pterodactylus.sone.data.Sone;
+import net.pterodactylus.sone.database.AlbumBuilder;
import net.pterodactylus.sone.database.ImageBuilder;
import com.google.common.base.Function;
/** The Sone this album belongs to. */
private Sone sone;
+ /** The parent album. */
+ private final DefaultAlbum parent;
+
/** Nested albums. */
private final List<Album> albums = new ArrayList<Album>();
/** The image IDs in order. */
- private final List<String> imageIds = new ArrayList<String>();
+ final List<String> imageIds = new ArrayList<String>();
/** The images in this album. */
- private final Map<String, Image> images = new HashMap<String, Image>();
-
- /** The parent album. */
- private Album parent;
+ final Map<String, Image> images = new HashMap<String, Image>();
/** Creates a new album with a random ID. */
- public DefaultAlbum() {
- this(UUID.randomUUID().toString());
+ public DefaultAlbum(Sone sone, DefaultAlbum parent) {
+ this(UUID.randomUUID().toString(), sone, parent);
}
/**
* @param id
* The ID of the album
*/
- public DefaultAlbum(String id) {
+ public DefaultAlbum(String id, Sone sone, DefaultAlbum parent) {
super(id);
+ this.sone = sone;
+ this.parent = parent;
}
//
}
@Override
- public Album setSone(Sone sone) {
- 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;
- }
-
- @Override
public List<Album> getAlbums() {
return new ArrayList<Album>(albums);
}
@Override
- public void addAlbum(Album album) {
- checkNotNull(album, "album must not be null");
- checkArgument(album.getSone().equals(sone), "album must belong to the same Sone as this album");
- album.setParent(this);
- if (!albums.contains(album)) {
- albums.add(album);
- }
- }
-
- @Override
- public void removeAlbum(Album album) {
- checkNotNull(album, "album must not be null");
- checkArgument(album.getSone().equals(sone), "album must belong this album’s Sone");
- checkArgument(equals(album.getParent()), "album must belong to this album");
- albums.remove(album);
- album.removeParent();
- }
-
- @Override
- public Album moveAlbumUp(Album album) {
- checkNotNull(album, "album must not be null");
- checkArgument(album.getSone().equals(sone), "album must belong to the same Sone as this album");
- checkArgument(equals(album.getParent()), "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);
- }
-
- @Override
- public Album moveAlbumDown(Album album) {
- checkNotNull(album, "album must not be null");
- checkArgument(album.getSone().equals(sone), "album must belong to the same Sone as this album");
- checkArgument(equals(album.getParent()), "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);
- }
-
- @Override
public List<Image> getImages() {
return new ArrayList<Image>(Collections2.filter(Collections2.transform(imageIds, new Function<String, Image>() {
}
@Override
- public void removeImage(Image image) {
- 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)) {
- if (images.isEmpty()) {
- albumImage = null;
- } else {
- albumImage = images.values().iterator().next().getId();
- }
- }
- }
-
- @Override
- public Image moveImageUp(Image image) {
- 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;
- }
- imageIds.remove(image.getId());
- imageIds.add(oldIndex - 1, image.getId());
- return images.get(imageIds.get(oldIndex));
- }
-
- @Override
- public Image moveImageDown(Image image) {
- 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;
- }
- imageIds.remove(image.getId());
- imageIds.add(oldIndex + 1, image.getId());
- return images.get(imageIds.get(oldIndex));
- }
-
- @Override
- public Image getAlbumImage() {
+ public Optional<Image> getAlbumImage() {
if (albumImage == null) {
- return null;
+ return absent();
}
- return Optional.fromNullable(images.get(albumImage)).or(images.values().iterator().next());
+ return fromNullable(fromNullable(images.get(albumImage)).or(images.values().iterator().next()));
}
@Override
}
@Override
- public Album setParent(Album parent) {
- this.parent = checkNotNull(parent, "parent must not be null");
- return this;
- }
-
- @Override
- public Album removeParent() {
- this.parent = null;
- return this;
+ public AlbumBuilder newAlbumBuilder() {
+ return new DefaultAlbumBuilder(sone, this) {
+ @Override
+ public Album build() throws IllegalStateException {
+ Album album = super.build();
+ albums.add(album);
+ return album;
+ }
+ };
}
@Override
};
}
+ @Override
+ public void moveUp() {
+ int oldIndex = parent.albums.indexOf(this);
+ parent.albums.remove(this);
+ parent.albums.add(Math.max(0, oldIndex - 1), this);
+ }
+
+ @Override
+ public void moveDown() {
+ int oldIndex = parent.albums.indexOf(this);
+ parent.albums.remove(this);
+ parent.albums.add(Math.min(parent.albums.size(), oldIndex + 1), this);
+ }
+
+ @Override
+ public void remove() throws IllegalStateException {
+ checkState(!isRoot(), "can not remove root album");
+ removeAllAlbums();
+ removeAllImages();
+ parent.albums.remove(this);
+ }
+
+ private void removeAllImages() {
+ for (Image image : images.values()) {
+ image.remove();
+ }
+ }
+
+ private void removeAllAlbums() {
+ for (Album album: albums) {
+ album.remove();
+ }
+ }
+
}