Change all copyright headers to include 2012.
[Sone.git] / src / main / java / net / pterodactylus / sone / data / Album.java
index ac7e11d..9d990fe 100644 (file)
@@ -1,5 +1,5 @@
 /*
- * Sone - Album.java - Copyright © 2011 David Roden
+ * Sone - Album.java - Copyright © 2011–2012 David Roden
  *
  * This program is free software: you can redistribute it and/or modify
  * it under the terms of the GNU General Public License as published by
 package net.pterodactylus.sone.data;
 
 import java.util.ArrayList;
+import java.util.Comparator;
+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.object.Default;
 import net.pterodactylus.util.validation.Validation;
 
 /**
@@ -30,6 +36,15 @@ import net.pterodactylus.util.validation.Validation;
  */
 public class Album implements Fingerprintable {
 
+       /** Compares two {@link Album}s by {@link #getTitle()}. */
+       public static final Comparator<Album> TITLE_COMPARATOR = new Comparator<Album>() {
+
+               @Override
+               public int compare(Album leftAlbum, Album rightAlbum) {
+                       return leftAlbum.getTitle().compareToIgnoreCase(rightAlbum.getTitle());
+               }
+       };
+
        /** The ID of this album. */
        private final String id;
 
@@ -39,8 +54,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 +69,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.
@@ -103,7 +121,7 @@ public class Album implements Fingerprintable {
         * @return This album
         */
        public Album setSone(Sone sone) {
-               Validation.begin().isNull("Current Album Owner", this.sone).isNotNull("New Album Owner", sone).check();
+               Validation.begin().isNotNull("New Album Owner", sone).isEither("Old Album Owner", this.sone, null, sone).check();
                this.sone = sone;
                return this;
        }
@@ -124,9 +142,11 @@ public class Album implements Fingerprintable {
         *            The album to add
         */
        public void addAlbum(Album album) {
-               Validation.begin().isNotNull("Album", album).check().isEqual("Album Owner", album.sone, sone).isNull("Album Parent", album.parent).check();
-               albums.add(album);
+               Validation.begin().isNotNull("Album", album).check().isEqual("Album Owner", album.sone, sone).isEither("Old Album Parent", this.parent, null, album.parent).check();
                album.setParent(this);
+               if (!albums.contains(album)) {
+                       albums.add(album);
+               }
        }
 
        /**
@@ -142,12 +162,60 @@ public class Album implements Fingerprintable {
        }
 
        /**
+        * Moves the given album up in this album’s albums. If the album is already
+        * the first album, nothing happens.
+        *
+        * @param album
+        *            The album to move up
+        * @return The album that the given album swapped the place with, or
+        *         <code>null</code> if the album did not change its place
+        */
+       public Album moveAlbumUp(Album album) {
+               Validation.begin().isNotNull("Album", album).check().isEqual("Album Owner", album.sone, sone).isEqual("Album Parent", album.parent, this).check();
+               int oldIndex = albums.indexOf(album);
+               if (oldIndex <= 0) {
+                       return null;
+               }
+               albums.remove(oldIndex);
+               albums.add(oldIndex - 1, album);
+               return albums.get(oldIndex);
+       }
+
+       /**
+        * Moves the given album down in this album’s albums. If the album is
+        * already the last album, nothing happens.
+        *
+        * @param album
+        *            The album to move down
+        * @return The album that the given album swapped the place with, or
+        *         <code>null</code> if the album did not change its place
+        */
+       public Album moveAlbumDown(Album album) {
+               Validation.begin().isNotNull("Album", album).check().isEqual("Album Owner", album.sone, sone).isEqual("Album Parent", album.parent, this).check();
+               int oldIndex = albums.indexOf(album);
+               if ((oldIndex < 0) || (oldIndex >= (albums.size() - 1))) {
+                       return null;
+               }
+               albums.remove(oldIndex);
+               albums.add(oldIndex + 1, album);
+               return albums.get(oldIndex);
+       }
+
+       /**
         * Returns the images in this album.
         *
         * @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,8 +226,17 @@ 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);
-               images.add(image);
+               if (imageIds.isEmpty() && (albumImage == null)) {
+                       albumImage = image.getId();
+               }
+               if (!imageIds.contains(image.getId())) {
+                       imageIds.add(image.getId());
+                       images.put(image.getId(), image);
+               }
        }
 
        /**
@@ -170,7 +247,55 @@ 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
+        * @return The image that the given image swapped the place with, or
+        *         <code>null</code> if the image did not change its place
+        */
+       public Image 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 null;
+               }
+               imageIds.remove(image.getId());
+               imageIds.add(oldIndex - 1, image.getId());
+               return images.get(imageIds.get(oldIndex));
+       }
+
+       /**
+        * Moves 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
+        * @return The image that the given image swapped the place with, or
+        *         <code>null</code> if the image did not change its place
+        */
+       public Image 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 null;
+               }
+               imageIds.remove(image.getId());
+               imageIds.add(oldIndex + 1, image.getId());
+               return images.get(imageIds.get(oldIndex));
        }
 
        /**
@@ -180,10 +305,22 @@ 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);
+               return Default.forNull(images.get(albumImage), images.values().iterator().next());
+       }
+
+       /**
+        * 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;
        }
 
        /**
@@ -224,7 +361,6 @@ public class Album implements Fingerprintable {
         * @return This album
         */
        protected Album removeParent() {
-               Validation.begin().isNotNull("Album Parent", parent).check();
                this.parent = null;
                return this;
        }
@@ -287,6 +423,9 @@ public class Album implements Fingerprintable {
                fingerprint.append("ID(").append(id).append(')');
                fingerprint.append("Title(").append(title).append(')');
                fingerprint.append("Description(").append(description).append(')');
+               if (albumImage != null) {
+                       fingerprint.append("AlbumImage(").append(albumImage).append(')');
+               }
 
                /* add nested albums. */
                fingerprint.append("Albums(");
@@ -297,7 +436,7 @@ public class Album implements Fingerprintable {
 
                /* add images. */
                fingerprint.append("Images(");
-               for (Image image : images) {
+               for (Image image : getImages()) {
                        if (image.isInserted()) {
                                fingerprint.append(image.getFingerprint());
                        }