Add methods to move an image up and down.
[Sone.git] / src / main / java / net / pterodactylus / sone / data / Album.java
index d0d078b..ab5822b 100644 (file)
 package net.pterodactylus.sone.data;
 
 import java.util.ArrayList;
-import java.util.LinkedHashMap;
+import java.util.HashMap;
 import java.util.List;
 import java.util.Map;
 import java.util.UUID;
 
+import net.pterodactylus.util.collection.Mapper;
+import net.pterodactylus.util.collection.Mappers;
 import net.pterodactylus.util.validation.Validation;
 
 /**
@@ -41,8 +43,11 @@ public class Album implements Fingerprintable {
        /** Nested albums. */
        private final List<Album> albums = new ArrayList<Album>();
 
+       /** The image IDs in order. */
+       private final List<String> imageIds = new ArrayList<String>();
+
        /** The images in this album. */
-       private final Map<String, Image> images = new LinkedHashMap<String, Image>();
+       private final Map<String, Image> images = new HashMap<String, Image>();
 
        /** The parent album. */
        private Album parent;
@@ -151,7 +156,15 @@ public class Album implements Fingerprintable {
         * @return The images in this album
         */
        public List<Image> getImages() {
-               return new ArrayList<Image>(images.values());
+               return Mappers.mappedList(imageIds, new Mapper<String, Image>() {
+
+                       @Override
+                       @SuppressWarnings("synthetic-access")
+                       public Image map(String imageId) {
+                               return images.get(imageId);
+                       }
+
+               });
        }
 
        /**
@@ -166,7 +179,11 @@ public class Album implements Fingerprintable {
                        image.getAlbum().removeImage(image);
                }
                image.setAlbum(this);
-               if (!images.containsKey(image.getId())) {
+               if (imageIds.isEmpty()) {
+                       albumImage = image.getId();
+               }
+               if (!imageIds.contains(image.getId())) {
+                       imageIds.add(image.getId());
                        images.put(image.getId(), image);
                }
        }
@@ -179,7 +196,49 @@ 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);
+               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();
+                       }
+               }
+       }
+
+       /**
+        * Moves the given image up in this album’s images. If the image is already
+        * the first image, nothing happens.
+        *
+        * @param image
+        *            The image to move up
+        */
+       public void moveImageUp(Image image) {
+               Validation.begin().isNotNull("Image", image).check().isEqual("Image Album", image.getAlbum(), this).isEqual("Album Owner", image.getAlbum().getSone(), sone).check();
+               int oldIndex = imageIds.indexOf(image.getId());
+               if (oldIndex <= 0) {
+                       return;
+               }
+               imageIds.remove(image.getId());
+               imageIds.add(oldIndex - 1, image.getId());
+       }
+
+       /**
+        * Move the given image down in this album’s images. If the image is already
+        * the last image, nothing happens.
+        *
+        * @param image
+        *            The image to move down
+        */
+       public void moveImageDown(Image image) {
+               Validation.begin().isNotNull("Image", image).check().isEqual("Image Album", image.getAlbum(), this).isEqual("Album Owner", image.getAlbum().getSone(), sone).check();
+               int oldIndex = imageIds.indexOf(image.getId());
+               if ((oldIndex == -1) || (oldIndex >= (imageIds.size() - 1))) {
+                       return;
+               }
+               imageIds.remove(image.getId());
+               imageIds.add(oldIndex + 1, image.getId());
        }
 
        /**
@@ -196,6 +255,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