Move method to remove image from Album to Image.
[Sone.git] / src / main / java / net / pterodactylus / sone / data / impl / DefaultImage.java
index 085091a..156eb81 100644 (file)
@@ -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;
@@ -29,9 +31,9 @@ import net.pterodactylus.sone.data.Sone;
 public class DefaultImage extends AbstractImage {
 
        private final Sone sone;
-       private final Album album;
+       private final DefaultAlbum album;
 
-       public DefaultImage(String id, Sone sone, Album album, String key, long creationTime, int width, int height) {
+       public DefaultImage(String id, Sone sone, DefaultAlbum album, String key, long creationTime, int width, int height) {
                super(id, key, creationTime, width, height);
                this.sone = sone;
                this.album = album;
@@ -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();
+                       }
+               }
+       }
+
 }