Return an Optional for the album image.
[Sone.git] / src / main / java / net / pterodactylus / sone / data / impl / DefaultAlbum.java
index cbef5da..7102bfe 100644 (file)
@@ -17,8 +17,8 @@
 
 package net.pterodactylus.sone.data.impl;
 
-import static com.google.common.base.Preconditions.checkArgument;
-import static com.google.common.base.Preconditions.checkNotNull;
+import static com.google.common.base.Optional.absent;
+import static com.google.common.base.Optional.fromNullable;
 import static com.google.common.base.Preconditions.checkState;
 
 import java.util.ArrayList;
@@ -92,34 +92,6 @@ public class DefaultAlbum extends AbstractAlbum {
        }
 
        @Override
-       public Album moveAlbumUp(Album album) {
-               checkNotNull(album, "album must not be null");
-               checkArgument(album.getSone().equals(sone), "album must belong to the same Sone as this album");
-               checkArgument(equals(album.getParent()), "album must belong to this album");
-               int oldIndex = albums.indexOf(album);
-               if (oldIndex <= 0) {
-                       return null;
-               }
-               albums.remove(oldIndex);
-               albums.add(oldIndex - 1, album);
-               return albums.get(oldIndex);
-       }
-
-       @Override
-       public Album moveAlbumDown(Album album) {
-               checkNotNull(album, "album must not be null");
-               checkArgument(album.getSone().equals(sone), "album must belong to the same Sone as this album");
-               checkArgument(equals(album.getParent()), "album must belong to this album");
-               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);
-       }
-
-       @Override
        public List<Image> getImages() {
                return new ArrayList<Image>(Collections2.filter(Collections2.transform(imageIds, new Function<String, Image>() {
 
@@ -132,41 +104,11 @@ public class DefaultAlbum extends AbstractAlbum {
        }
 
        @Override
-       public Image moveImageUp(Image image) {
-               checkNotNull(image, "image must not be null");
-               checkNotNull(image.getSone(), "image must have an owner");
-               checkArgument(image.getSone().equals(sone), "image must belong to the same Sone as this album");
-               checkArgument(image.getAlbum().equals(this), "image must belong to this album");
-               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));
-       }
-
-       @Override
-       public Image moveImageDown(Image image) {
-               checkNotNull(image, "image must not be null");
-               checkNotNull(image.getSone(), "image must have an owner");
-               checkArgument(image.getSone().equals(sone), "image must belong to the same Sone as this album");
-               checkArgument(image.getAlbum().equals(this), "image must belong to this album");
-               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));
-       }
-
-       @Override
-       public Image getAlbumImage() {
+       public Optional<Image> getAlbumImage() {
                if (albumImage == null) {
-                       return null;
+                       return absent();
                }
-               return Optional.fromNullable(images.get(albumImage)).or(images.values().iterator().next());
+               return fromNullable(fromNullable(images.get(albumImage)).or(images.values().iterator().next()));
        }
 
        @Override
@@ -203,6 +145,20 @@ public class DefaultAlbum extends AbstractAlbum {
        }
 
        @Override
+       public void moveUp() {
+               int oldIndex = parent.albums.indexOf(this);
+               parent.albums.remove(this);
+               parent.albums.add(Math.max(0, oldIndex - 1), this);
+       }
+
+       @Override
+       public void moveDown() {
+               int oldIndex = parent.albums.indexOf(this);
+               parent.albums.remove(this);
+               parent.albums.add(Math.min(parent.albums.size(), oldIndex + 1), this);
+       }
+
+       @Override
        public void remove() throws IllegalStateException {
                checkState(!isRoot(), "can not remove root album");
                removeAllAlbums();