A linked hash map is no longer required, the order is now stored elsewhere.
[Sone.git] / src / main / java / net / pterodactylus / sone / data / Album.java
index 0abc59c..7d10e72 100644 (file)
 package net.pterodactylus.sone.data;
 
 import java.util.ArrayList;
+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;
 
 /**
@@ -39,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 List<Image> images = new ArrayList<Image>();
+       private final Map<String, Image> images = new HashMap<String, Image>();
 
        /** The parent album. */
        private Album parent;
@@ -51,8 +58,8 @@ public class Album implements Fingerprintable {
        /** The description of this album. */
        private String description;
 
-       /** The index of the album picture. */
-       private int albumImage = -1;
+       /** The ID of the album picture. */
+       private String albumImage;
 
        /**
         * Creates a new album with a random ID.
@@ -125,8 +132,10 @@ public class Album implements Fingerprintable {
         */
        public void addAlbum(Album album) {
                Validation.begin().isNotNull("Album", album).check().isEqual("Album Owner", album.sone, sone).isEither("Old Album Parent", this.parent, null, album.parent).check();
-               albums.add(album);
                album.setParent(this);
+               if (!albums.contains(album)) {
+                       albums.add(album);
+               }
        }
 
        /**
@@ -147,7 +156,15 @@ public class Album implements Fingerprintable {
         * @return The images in this album
         */
        public List<Image> getImages() {
-               return new ArrayList<Image>(images);
+               return Mappers.mappedList(imageIds, new Mapper<String, Image>() {
+
+                       @Override
+                       @SuppressWarnings("synthetic-access")
+                       public Image map(String imageId) {
+                               return images.get(imageId);
+                       }
+
+               });
        }
 
        /**
@@ -158,9 +175,16 @@ public class Album implements Fingerprintable {
         */
        public void addImage(Image image) {
                Validation.begin().isNotNull("Image", image).check().isNotNull("Image Owner", image.getSone()).check().isEqual("Image Owner", image.getSone(), sone).check();
+               if (image.getAlbum() != null) {
+                       image.getAlbum().removeImage(image);
+               }
                image.setAlbum(this);
-               if (!images.contains(image)) {
-                       images.add(image);
+               if (imageIds.isEmpty()) {
+                       albumImage = image.getId();
+               }
+               if (!imageIds.contains(image.getId())) {
+                       imageIds.add(image.getId());
+                       images.put(image.getId(), image);
                }
        }
 
@@ -172,7 +196,15 @@ 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();
+                       }
+               }
        }
 
        /**
@@ -182,13 +214,25 @@ public class Album implements Fingerprintable {
         * @return The image to show when this album is listed
         */
        public Image getAlbumImage() {
-               if (albumImage == -1) {
+               if (albumImage == null) {
                        return null;
                }
                return images.get(albumImage);
        }
 
        /**
+        * 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
@@ -298,7 +342,7 @@ public class Album implements Fingerprintable {
 
                /* add images. */
                fingerprint.append("Images(");
-               for (Image image : images) {
+               for (Image image : images.values()) {
                        if (image.isInserted()) {
                                fingerprint.append(image.getFingerprint());
                        }