X-Git-Url: https://git.pterodactylus.net/?a=blobdiff_plain;f=src%2Fmain%2Fjava%2Fnet%2Fpterodactylus%2Fsone%2Fdata%2Fimpl%2FDefaultAlbum.java;h=7102bfe2935efb9f03d7298ccb1d6156c8c7b86d;hb=28b2e2b0d2ff2320f5b6a5ac36a858a604c7c7ba;hp=70fac0d9382d8d26a3411ba2c3e77f16364b9b95;hpb=20033c2725145859c3a4d8d75a3008b9fc099b3c;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 70fac0d..7102bfe 100644 --- a/src/main/java/net/pterodactylus/sone/data/impl/DefaultAlbum.java +++ b/src/main/java/net/pterodactylus/sone/data/impl/DefaultAlbum.java @@ -19,8 +19,6 @@ package net.pterodactylus.sone.data.impl; import static com.google.common.base.Optional.absent; import static com.google.common.base.Optional.fromNullable; -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; @@ -32,52 +30,39 @@ 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.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; -import com.google.common.hash.Hasher; -import com.google.common.hash.Hashing; /** - * Container for images that can also contain nested {@link Album}s. + * Dumb, store-everything-in-memory implementation of an {@link Album}. * * @author David ‘Bombe’ Roden */ -public class DefaultAlbum implements Album { - - /** The ID of this album. */ - private final String id; +public class DefaultAlbum extends AbstractAlbum { /** The Sone this album belongs to. */ private Sone sone; + /** The parent album. */ + private final DefaultAlbum parent; + /** Nested albums. */ private final List albums = new ArrayList(); /** The image IDs in order. */ - private final List imageIds = new ArrayList(); + final List imageIds = new ArrayList(); /** The images in this album. */ - private final Map images = new HashMap(); - - /** The parent album. */ - private Album parent; - - /** The title of this album. */ - private String title; - - /** The description of this album. */ - private String description; - - /** The ID of the album picture. */ - private String albumImage; + final Map images = new HashMap(); /** 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); } /** @@ -86,8 +71,10 @@ public class DefaultAlbum implements Album { * @param id * The ID of the album */ - public DefaultAlbum(String id) { - this.id = checkNotNull(id, "id must not be null"); + public DefaultAlbum(String id, Sone sone, DefaultAlbum parent) { + super(id); + this.sone = sone; + this.parent = parent; } // @@ -95,76 +82,16 @@ public class DefaultAlbum implements Album { // @Override - public String getId() { - return id; - } - - @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 getAlbums() { return new ArrayList(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 getImages() { return new ArrayList(Collections2.filter(Collections2.transform(imageIds, new Function() { @@ -177,67 +104,11 @@ public class DefaultAlbum implements Album { } @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 getAlbumImage() { if (albumImage == null) { - return null; + return absent(); } - return Optional.fromNullable(images.get(albumImage)).or(images.values().iterator().next()); - } - - @Override - public boolean isEmpty() { - return albums.isEmpty() && images.isEmpty(); - } - - @Override - public boolean isRoot() { - return parent == null; + return fromNullable(fromNullable(images.get(albumImage)).or(images.values().iterator().next())); } @Override @@ -246,30 +117,20 @@ public class DefaultAlbum implements Album { } @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 String getTitle() { - return title; - } - - @Override - public String getDescription() { - return description; + public AlbumBuilder newAlbumBuilder() { + return new DefaultAlbumBuilder(sone, this) { + @Override + public Album build() throws IllegalStateException { + Album album = super.build(); + albums.add(album); + return album; + } + }; } @Override public ImageBuilder newImageBuilder() throws IllegalStateException { - return new DefaultImageBuilder(this) { + return new DefaultImageBuilder(sone, this) { @Override public Image build() throws IllegalStateException { Image image = super.build(); @@ -284,100 +145,37 @@ public class DefaultAlbum implements Album { } @Override - public Modifier modify() throws IllegalStateException { - // TODO: reenable check for local Sones - return new Modifier() { - private Optional title = absent(); - - private Optional description = absent(); - - private Optional albumImage = absent(); - - @Override - public Modifier setTitle(String title) { - this.title = fromNullable(title); - return this; - } - - @Override - public Modifier setDescription(String description) { - this.description = fromNullable(description); - return this; - } - - @Override - public Modifier setAlbumImage(String imageId) { - this.albumImage = fromNullable(imageId); - return this; - } - - @Override - public Album update() throws IllegalStateException { - if (title.isPresent()) { - DefaultAlbum.this.title = title.get(); - } - if (description.isPresent()) { - DefaultAlbum.this.description = description.get(); - } - if (albumImage.isPresent()) { - DefaultAlbum.this.albumImage = albumImage.get(); - } - return DefaultAlbum.this; - } - }; + public void moveUp() { + int oldIndex = parent.albums.indexOf(this); + parent.albums.remove(this); + parent.albums.add(Math.max(0, oldIndex - 1), this); } - // - // FINGERPRINTABLE METHODS - // - @Override - public String getFingerprint() { - Hasher hash = Hashing.sha256().newHasher(); - hash.putString("Album("); - hash.putString("ID(").putString(id).putString(")"); - hash.putString("Title(").putString(title).putString(")"); - hash.putString("Description(").putString(description).putString(")"); - if (albumImage != null) { - hash.putString("AlbumImage(").putString(albumImage).putString(")"); - } - - /* add nested albums. */ - hash.putString("Albums("); - for (Album album : albums) { - hash.putString(album.getFingerprint()); - } - hash.putString(")"); - - /* add images. */ - hash.putString("Images("); - for (Image image : getImages()) { - if (image.isInserted()) { - hash.putString(image.getFingerprint()); - } - } - hash.putString(")"); - - hash.putString(")"); - return hash.hash().toString(); + public void moveDown() { + int oldIndex = parent.albums.indexOf(this); + parent.albums.remove(this); + parent.albums.add(Math.min(parent.albums.size(), oldIndex + 1), this); } - // - // OBJECT METHODS - // - @Override - public int hashCode() { - return id.hashCode(); + public void remove() throws IllegalStateException { + checkState(!isRoot(), "can not remove root album"); + removeAllAlbums(); + removeAllImages(); + parent.albums.remove(this); } - @Override - public boolean equals(Object object) { - if (!(object instanceof DefaultAlbum)) { - return false; + private void removeAllImages() { + for (Image image : images.values()) { + image.remove(); + } + } + + private void removeAllAlbums() { + for (Album album: albums) { + album.remove(); } - DefaultAlbum album = (DefaultAlbum) object; - return id.equals(album.id); } }