From: David ‘Bombe’ Roden Date: Sun, 13 Oct 2013 00:18:29 +0000 (+0200) Subject: Move methods to move an image up and down to Image. X-Git-Url: https://git.pterodactylus.net/?a=commitdiff_plain;h=31a732a2e18008ea65a31a4e8180c337458b2df9;p=Sone.git Move methods to move an image up and down to Image. --- diff --git a/src/main/java/net/pterodactylus/sone/data/Album.java b/src/main/java/net/pterodactylus/sone/data/Album.java index df514d4..e8efb59 100644 --- a/src/main/java/net/pterodactylus/sone/data/Album.java +++ b/src/main/java/net/pterodactylus/sone/data/Album.java @@ -128,28 +128,6 @@ public interface Album extends Identified, Fingerprintable { List getImages(); /** - * 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 - * null if the image did not change its place - */ - Image moveImageUp(Image image); - - /** - * 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 - * null if the image did not change its place - */ - Image moveImageDown(Image image); - - /** * Returns the album image of this album, or {@code null} if no album image has * been set. * diff --git a/src/main/java/net/pterodactylus/sone/data/Image.java b/src/main/java/net/pterodactylus/sone/data/Image.java index 3fbcc02..571eb4f 100644 --- a/src/main/java/net/pterodactylus/sone/data/Image.java +++ b/src/main/java/net/pterodactylus/sone/data/Image.java @@ -106,6 +106,10 @@ public interface Image extends Identified, Fingerprintable { Modifier modify() throws IllegalStateException; + void moveUp() throws IllegalStateException; + + void moveDown() throws IllegalStateException; + void remove() throws IllegalStateException; interface Modifier { diff --git a/src/main/java/net/pterodactylus/sone/data/impl/DefaultAlbum.java b/src/main/java/net/pterodactylus/sone/data/impl/DefaultAlbum.java index 161cdd9..32052d5 100644 --- a/src/main/java/net/pterodactylus/sone/data/impl/DefaultAlbum.java +++ b/src/main/java/net/pterodactylus/sone/data/impl/DefaultAlbum.java @@ -17,8 +17,6 @@ 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.Preconditions.checkState; import java.util.ArrayList; @@ -104,36 +102,6 @@ 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() { if (albumImage == null) { return null; diff --git a/src/main/java/net/pterodactylus/sone/data/impl/DefaultImage.java b/src/main/java/net/pterodactylus/sone/data/impl/DefaultImage.java index 156eb81..26f6355 100644 --- a/src/main/java/net/pterodactylus/sone/data/impl/DefaultImage.java +++ b/src/main/java/net/pterodactylus/sone/data/impl/DefaultImage.java @@ -50,6 +50,20 @@ public class DefaultImage extends AbstractImage { } @Override + public void moveUp() throws IllegalStateException { + int oldIndex = album.imageIds.indexOf(getId()); + album.imageIds.remove(getId()); + album.imageIds.add(Math.max(0, oldIndex - 1), getId()); + } + + @Override + public void moveDown() throws IllegalStateException { + int oldIndex = album.imageIds.indexOf(getId()); + album.imageIds.remove(getId()); + album.imageIds.add(Math.min(album.imageIds.size(), oldIndex + 1), getId()); + } + + @Override public void remove() throws IllegalStateException { synchronized (album) { album.images.remove(getId()); diff --git a/src/main/java/net/pterodactylus/sone/database/memory/MemoryDatabase.java b/src/main/java/net/pterodactylus/sone/database/memory/MemoryDatabase.java index c778de2..6cb0027 100644 --- a/src/main/java/net/pterodactylus/sone/database/memory/MemoryDatabase.java +++ b/src/main/java/net/pterodactylus/sone/database/memory/MemoryDatabase.java @@ -581,6 +581,66 @@ public class MemoryDatabase extends AbstractService implements Database { } } + void moveUp(Album album) { + lock.writeLock().lock(); + try { + List albums = albumChildren.get(album.getParent().getId()); + int currentIndex = albums.indexOf(album.getId()); + if (currentIndex == 0) { + return; + } + albums.remove(album.getId()); + albums.add(currentIndex - 1, album.getId()); + } finally { + lock.writeLock().unlock(); + } + } + + void moveDown(Album album) { + lock.writeLock().lock(); + try { + List albums = albumChildren.get(album.getParent().getId()); + int currentIndex = albums.indexOf(album.getId()); + if (currentIndex == (albums.size() - 1)) { + return; + } + albums.remove(album.getId()); + albums.add(currentIndex + 1, album.getId()); + } finally { + lock.writeLock().unlock(); + } + } + + void moveUp(Image image) { + lock.writeLock().lock(); + try { + List images = albumImages.get(image.getAlbum().getId()); + int currentIndex = images.indexOf(image.getId()); + if (currentIndex == 0) { + return; + } + images.remove(image.getId()); + images.add(currentIndex - 1, image.getId()); + } finally { + lock.writeLock().unlock(); + } + } + + void moveDown(Image image) { + lock.writeLock().lock(); + try { + List images = albumChildren.get(image.getAlbum().getId()); + int currentIndex = images.indexOf(image.getId()); + if (currentIndex == (images.size() - 1)) { + return; + } + images.remove(image.getId()); + images.add(currentIndex + 1, image.getId()); + } finally { + lock.writeLock().unlock(); + } + } + // // PRIVATE METHODS // diff --git a/src/main/java/net/pterodactylus/sone/database/memory/MemoryImage.java b/src/main/java/net/pterodactylus/sone/database/memory/MemoryImage.java index 338da0a..d2c743e 100644 --- a/src/main/java/net/pterodactylus/sone/database/memory/MemoryImage.java +++ b/src/main/java/net/pterodactylus/sone/database/memory/MemoryImage.java @@ -51,6 +51,16 @@ public class MemoryImage extends AbstractImage { } @Override + public void moveUp() throws IllegalStateException { + memoryDatabase.moveUp(this); + } + + @Override + public void moveDown() throws IllegalStateException { + memoryDatabase.moveDown(this); + } + + @Override public void remove() throws IllegalStateException { memoryDatabase.removeImage(this); } diff --git a/src/main/java/net/pterodactylus/sone/web/EditImagePage.java b/src/main/java/net/pterodactylus/sone/web/EditImagePage.java index cec6990..c9bd705 100644 --- a/src/main/java/net/pterodactylus/sone/web/EditImagePage.java +++ b/src/main/java/net/pterodactylus/sone/web/EditImagePage.java @@ -66,9 +66,9 @@ public class EditImagePage extends SoneTemplatePage { throw new RedirectException("noPermission.html"); } if ("true".equals(request.getHttpRequest().getPartAsStringFailsafe("moveLeft", 4))) { - image.get().getAlbum().moveImageUp(image.get()); + image.get().moveUp(); } else if ("true".equals(request.getHttpRequest().getPartAsStringFailsafe("moveRight", 4))) { - image.get().getAlbum().moveImageDown(image.get()); + image.get().moveDown(); } else { String title = request.getHttpRequest().getPartAsStringFailsafe("title", 100).trim(); String description = request.getHttpRequest().getPartAsStringFailsafe("description", 1024).trim(); diff --git a/src/main/java/net/pterodactylus/sone/web/ajax/EditImageAjaxPage.java b/src/main/java/net/pterodactylus/sone/web/ajax/EditImageAjaxPage.java index ace775d..18dccf4 100644 --- a/src/main/java/net/pterodactylus/sone/web/ajax/EditImageAjaxPage.java +++ b/src/main/java/net/pterodactylus/sone/web/ajax/EditImageAjaxPage.java @@ -68,14 +68,14 @@ public class EditImageAjaxPage extends JsonPage { return createErrorJsonObject("not-authorized"); } if ("true".equals(request.getHttpRequest().getParam("moveLeft"))) { - Image swappedImage = image.get().getAlbum().moveImageUp(image.get()); + image.get().moveUp(); webInterface.getCore().touchConfiguration(); - return createSuccessJsonObject().put("sourceImageId", image.get().getId()).put("destinationImageId", swappedImage.getId()); + return createSuccessJsonObject(); // TODO - fix javascript } if ("true".equals(request.getHttpRequest().getParam("moveRight"))) { - Image swappedImage = image.get().getAlbum().moveImageDown(image.get()); + image.get().moveDown(); webInterface.getCore().touchConfiguration(); - return createSuccessJsonObject().put("sourceImageId", image.get().getId()).put("destinationImageId", swappedImage.getId()); + return createSuccessJsonObject(); // TODO - fix javascript } String title = request.getHttpRequest().getParam("title").trim(); String description = request.getHttpRequest().getParam("description").trim();