/*
- * Sone - Album.java - Copyright © 2011–2013 David Roden
+ * Sone - MemoryAlbum.java - Copyright © 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.impl;
-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.HashMap;
import java.util.List;
-import java.util.Map;
-import java.util.UUID;
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.Database;
import net.pterodactylus.sone.database.ImageBuilder;
-import com.google.common.base.Function;
import com.google.common.base.Optional;
-import com.google.common.base.Predicates;
-import com.google.common.collect.Collections2;
/**
- * Dumb, store-everything-in-memory implementation of an {@link Album}.
+ * TODO
*
* @author <a href="mailto:bombe@pterodactylus.net">David ‘Bombe’ Roden</a>
*/
public class DefaultAlbum extends AbstractAlbum {
- /** The Sone this album belongs to. */
- private Sone sone;
+ private final Database database;
+ private final Sone sone; /* TODO - only store sone ID. */
- /** Nested albums. */
- private final List<Album> albums = new ArrayList<Album>();
-
- /** The image IDs in order. */
- private 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;
-
- /** Creates a new album with a random ID. */
- public DefaultAlbum() {
- this(UUID.randomUUID().toString());
- }
-
- /**
- * Creates a new album with the given ID.
- *
- * @param id
- * The ID of the album
- */
- public DefaultAlbum(String id) {
- super(id);
+ protected DefaultAlbum(Database database, String id, Sone sone, String parentId) {
+ super(id, parentId);
+ this.database = database;
+ this.sone = sone;
}
- //
- // ACCESSORS
- //
-
@Override
public Sone getSone() {
return sone;
}
@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);
- }
+ return database.getAlbums(this);
}
@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();
+ public List<Image> getImages() {
+ return database.getImages(this);
}
@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);
+ public Optional<Image> getAlbumImage() {
+ return database.getImage(albumImage);
}
@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);
+ public Album getParent() {
+ return database.getAlbum(parentId).get();
}
@Override
- public List<Image> getImages() {
- return new ArrayList<Image>(Collections2.filter(Collections2.transform(imageIds, new Function<String, Image>() {
-
+ public AlbumBuilder newAlbumBuilder() throws IllegalStateException {
+ return new AbstractAlbumBuilder() {
@Override
- @SuppressWarnings("synthetic-access")
- public Image apply(String imageId) {
- return images.get(imageId);
+ public Album build() throws IllegalStateException {
+ validate();
+ DefaultAlbum memoryAlbum = new DefaultAlbum(database, getId(), sone, DefaultAlbum.this.id);
+ database.storeAlbum(memoryAlbum);
+ return memoryAlbum;
}
- }), Predicates.notNull()));
+ };
}
@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();
+ public ImageBuilder newImageBuilder() throws IllegalStateException {
+ return new AbstractImageBuilder() {
+ @Override
+ public Image build(Optional<ImageCreated> imageCreated) throws IllegalStateException {
+ validate();
+ DefaultImage image = new DefaultImage(database, getId(), sone, DefaultAlbum.this.id, key, getCreationTime(), width, height);
+ database.storeImage(image);
+ if (imageCreated.isPresent()) {
+ imageCreated.get().imageCreated(image);
+ }
+ return image;
}
- }
+ };
}
@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));
+ public void moveUp() {
+ database.moveUp(this);
}
@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));
+ public void moveDown() {
+ database.moveDown(this);
}
@Override
- public Image getAlbumImage() {
- if (albumImage == null) {
- return null;
+ public void remove() throws IllegalStateException {
+ checkState(!isRoot(), "can not remove root album");
+ for (Album album : getAlbums()) {
+ album.remove();
}
- return Optional.fromNullable(images.get(albumImage)).or(images.values().iterator().next());
- }
-
- @Override
- public Album getParent() {
- return parent;
- }
-
- @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;
- }
-
- @Override
- public ImageBuilder newImageBuilder() throws IllegalStateException {
- return new DefaultImageBuilder(sone, this) {
- @Override
- public Image build() throws IllegalStateException {
- Image image = super.build();
- if (images.isEmpty() && (albumImage == null)) {
- albumImage = image.getId();
- }
- images.put(image.getId(), image);
- imageIds.add(image.getId());
- return image;
- }
- };
+ for (Image image : getImages()) {
+ image.remove();
+ }
+ database.removeAlbum(this);
}
}