Ensure that an image is always set as album image, unless the album is empty.
[Sone.git] / src / main / java / net / pterodactylus / sone / data / Album.java
index d0d078b..89823a8 100644 (file)
@@ -166,6 +166,9 @@ public class Album implements Fingerprintable {
                        image.getAlbum().removeImage(image);
                }
                image.setAlbum(this);
+               if (images.isEmpty()) {
+                       albumImage = image.getId();
+               }
                if (!images.containsKey(image.getId())) {
                        images.put(image.getId(), image);
                }
@@ -180,6 +183,13 @@ public class Album implements Fingerprintable {
        public void removeImage(Image image) {
                Validation.begin().isNotNull("Image", image).check().isEqual("Image Owner", image.getSone(), sone).check();
                images.remove(image);
+               if (image.getId().equals(albumImage)) {
+                       if (images.isEmpty()) {
+                               albumImage = null;
+                       } else {
+                               albumImage = images.values().iterator().next().getId();
+                       }
+               }
        }
 
        /**
@@ -196,6 +206,18 @@ public class Album implements Fingerprintable {
        }
 
        /**
+        * Sets the ID of the album image.
+        *
+        * @param id
+        *            The ID of the album image
+        * @return This album
+        */
+       public Album setAlbumImage(String id) {
+               this.albumImage = id;
+               return this;
+       }
+
+       /**
         * Returns whether this album contains any other albums or images.
         *
         * @return {@code true} if this album is empty, {@code false} otherwise