X-Git-Url: https://git.pterodactylus.net/?a=blobdiff_plain;ds=inline;f=src%2Fmain%2Fjava%2Fnet%2Fpterodactylus%2Fsone%2Fdata%2Fimpl%2FDefaultAlbum.java;h=295555d7645ae091bfa4ac6674b637c8783fffd5;hb=45f452c0098821ba37b28888ffc75bccae80216d;hp=32052d579ee664d83080d48e85ff4a01aa147b59;hpb=31a732a2e18008ea65a31a4e8180c337458b2df9;p=Sone.git
diff --git a/src/main/java/net/pterodactylus/sone/data/impl/DefaultAlbum.java b/src/main/java/net/pterodactylus/sone/data/impl/DefaultAlbum.java
index 32052d5..295555d 100644
--- a/src/main/java/net/pterodactylus/sone/data/impl/DefaultAlbum.java
+++ b/src/main/java/net/pterodactylus/sone/data/impl/DefaultAlbum.java
@@ -1,5 +1,5 @@
/*
- * 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
@@ -19,66 +19,33 @@ package net.pterodactylus.sone.data.impl;
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 David âBombeâ Roden
*/
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. */
- /** The parent album. */
- private final DefaultAlbum parent;
-
- /** Nested albums. */
- private final List albums = new ArrayList();
-
- /** The image IDs in order. */
- final List imageIds = new ArrayList();
-
- /** The images in this album. */
- final Map images = new HashMap();
-
- /** Creates a new album with a random ID. */
- public DefaultAlbum(Sone sone, DefaultAlbum parent) {
- this(UUID.randomUUID().toString(), sone, parent);
- }
-
- /**
- * Creates a new album with the given ID.
- *
- * @param id
- * The ID of the album
- */
- public DefaultAlbum(String id, Sone sone, DefaultAlbum parent) {
- super(id);
+ protected DefaultAlbum(Database database, String id, Sone sone, String parentId) {
+ super(id, parentId);
+ this.database = database;
this.sone = sone;
- this.parent = parent;
}
- //
- // ACCESSORS
- //
-
@Override
public Sone getSone() {
return sone;
@@ -86,57 +53,48 @@ public class DefaultAlbum extends AbstractAlbum {
@Override
public List getAlbums() {
- return new ArrayList(albums);
+ return database.getAlbums(this);
}
@Override
public List getImages() {
- return new ArrayList(Collections2.filter(Collections2.transform(imageIds, new Function() {
-
- @Override
- @SuppressWarnings("synthetic-access")
- public Image apply(String imageId) {
- return images.get(imageId);
- }
- }), Predicates.notNull()));
+ return database.getImages(this);
}
@Override
- public Image getAlbumImage() {
- if (albumImage == null) {
- return null;
- }
- return Optional.fromNullable(images.get(albumImage)).or(images.values().iterator().next());
+ public Optional getAlbumImage() {
+ return database.getImage(albumImage);
}
@Override
public Album getParent() {
- return parent;
+ return database.getAlbum(parentId).get();
}
@Override
- public AlbumBuilder newAlbumBuilder() {
- return new DefaultAlbumBuilder(sone, this) {
+ public AlbumBuilder newAlbumBuilder() throws IllegalStateException {
+ return new AbstractAlbumBuilder() {
@Override
public Album build() throws IllegalStateException {
- Album album = super.build();
- albums.add(album);
- return album;
+ validate();
+ DefaultAlbum memoryAlbum = new DefaultAlbum(database, getId(), sone, DefaultAlbum.this.id);
+ database.storeAlbum(memoryAlbum);
+ return memoryAlbum;
}
};
}
@Override
public ImageBuilder newImageBuilder() throws IllegalStateException {
- return new DefaultImageBuilder(sone, this) {
+ return new AbstractImageBuilder() {
@Override
- public Image build() throws IllegalStateException {
- Image image = super.build();
- if (images.isEmpty() && (albumImage == null)) {
- albumImage = image.getId();
+ public Image build(Optional 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);
}
- images.put(image.getId(), image);
- imageIds.add(image.getId());
return image;
}
};
@@ -144,36 +102,24 @@ public class DefaultAlbum extends AbstractAlbum {
@Override
public void moveUp() {
- int oldIndex = parent.albums.indexOf(this);
- parent.albums.remove(this);
- parent.albums.add(Math.max(0, oldIndex - 1), this);
+ database.moveUp(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);
+ database.moveDown(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) {
+ for (Album album : getAlbums()) {
album.remove();
}
+ for (Image image : getImages()) {
+ image.remove();
+ }
+ database.removeAlbum(this);
}
}