From: David ‘Bombe’ Roden Date: Sat, 12 Oct 2013 15:36:06 +0000 (+0200) Subject: Move method to remove image from Album to Image. X-Git-Url: https://git.pterodactylus.net/?a=commitdiff_plain;h=a77d4a36080bc90c8c09efb753eb9e46f4e677d9;p=Sone.git Move method to remove image from Album to Image. --- diff --git a/src/main/java/net/pterodactylus/sone/core/Core.java b/src/main/java/net/pterodactylus/sone/core/Core.java index 8756c60..4e7f18d 100644 --- a/src/main/java/net/pterodactylus/sone/core/Core.java +++ b/src/main/java/net/pterodactylus/sone/core/Core.java @@ -1534,8 +1534,7 @@ public class Core extends AbstractService implements SoneProvider, PostProvider, checkNotNull(image, "image must not be null"); checkArgument(image.getSone().isLocal(), "image must belong to a local Sone"); deleteTemporaryImage(image.getId()); - image.getAlbum().removeImage(image); - database.removeImage(image); + image.remove(); touchConfiguration(); } diff --git a/src/main/java/net/pterodactylus/sone/data/Album.java b/src/main/java/net/pterodactylus/sone/data/Album.java index ea5365e..5b9d368 100644 --- a/src/main/java/net/pterodactylus/sone/data/Album.java +++ b/src/main/java/net/pterodactylus/sone/data/Album.java @@ -158,14 +158,6 @@ public interface Album extends Identified, Fingerprintable { List getImages(); /** - * Removes the given image from this album. - * - * @param image - * The image to remove - */ - void removeImage(Image image); - - /** * Moves the given image up in this album’s images. If the image is already the * first image, nothing happens. * diff --git a/src/main/java/net/pterodactylus/sone/data/Image.java b/src/main/java/net/pterodactylus/sone/data/Image.java index 82c831d..3fbcc02 100644 --- a/src/main/java/net/pterodactylus/sone/data/Image.java +++ b/src/main/java/net/pterodactylus/sone/data/Image.java @@ -106,6 +106,8 @@ public interface Image extends Identified, Fingerprintable { Modifier modify() throws IllegalStateException; + void remove() throws IllegalStateException; + interface Modifier { Modifier setKey(String key); 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 3235f17..74b66e7 100644 --- a/src/main/java/net/pterodactylus/sone/data/impl/DefaultAlbum.java +++ b/src/main/java/net/pterodactylus/sone/data/impl/DefaultAlbum.java @@ -51,10 +51,10 @@ public class DefaultAlbum extends AbstractAlbum { 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(); + final Map images = new HashMap(); /** The parent album. */ private Album parent; @@ -139,22 +139,6 @@ public class DefaultAlbum extends AbstractAlbum { } @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"); diff --git a/src/main/java/net/pterodactylus/sone/data/impl/DefaultImage.java b/src/main/java/net/pterodactylus/sone/data/impl/DefaultImage.java index 54e6495..156eb81 100644 --- a/src/main/java/net/pterodactylus/sone/data/impl/DefaultImage.java +++ b/src/main/java/net/pterodactylus/sone/data/impl/DefaultImage.java @@ -17,6 +17,8 @@ package net.pterodactylus.sone.data.impl; +import static com.google.common.collect.FluentIterable.from; + import net.pterodactylus.sone.data.Album; import net.pterodactylus.sone.data.Image; import net.pterodactylus.sone.data.Sone; @@ -47,4 +49,15 @@ public class DefaultImage extends AbstractImage { return album; } + @Override + public void remove() throws IllegalStateException { + synchronized (album) { + album.images.remove(getId()); + album.imageIds.remove(getId()); + if (getId().equals(album.albumImage)) { + album.albumImage = from(album.images.values()).transform(GET_ID).first().orNull(); + } + } + } + } diff --git a/src/main/java/net/pterodactylus/sone/database/memory/MemoryImage.java b/src/main/java/net/pterodactylus/sone/database/memory/MemoryImage.java index a694720..338da0a 100644 --- a/src/main/java/net/pterodactylus/sone/database/memory/MemoryImage.java +++ b/src/main/java/net/pterodactylus/sone/database/memory/MemoryImage.java @@ -50,4 +50,9 @@ public class MemoryImage extends AbstractImage { return memoryDatabase.getAlbum(albumId).get(); } + @Override + public void remove() throws IllegalStateException { + memoryDatabase.removeImage(this); + } + }