From: David ‘Bombe’ Roden Date: Sat, 12 Oct 2013 19:57:30 +0000 (+0200) Subject: Simplify album removal. X-Git-Url: https://git.pterodactylus.net/?p=Sone.git;a=commitdiff_plain;h=f0c0d175ae5058661d3af7cc99d0c188616a4d8f Simplify album removal. --- diff --git a/src/main/java/net/pterodactylus/sone/core/Core.java b/src/main/java/net/pterodactylus/sone/core/Core.java index 4e7f18d..e61a58f 100644 --- a/src/main/java/net/pterodactylus/sone/core/Core.java +++ b/src/main/java/net/pterodactylus/sone/core/Core.java @@ -1224,7 +1224,7 @@ public class Core extends AbstractService implements SoneProvider, PostProvider, followSone(sone, friendId); } for (Album album : sone.getRootAlbum().getAlbums()) { - sone.getRootAlbum().removeAlbum(album); + album.remove(); } soneInserters.get(sone).setLastInsertFingerprint(lastInsertFingerprint); } @@ -1483,24 +1483,6 @@ public class Core extends AbstractService implements SoneProvider, PostProvider, } /** - * Deletes the given album. The owner of the album has to be a local Sone, and - * the album has to be {@link Album#isEmpty() empty} to be deleted. - * - * @param album - * The album to remove - */ - public void deleteAlbum(Album album) { - checkNotNull(album, "album must not be null"); - checkArgument(album.getSone().isLocal(), "album’s Sone must be a local Sone"); - if (!album.isEmpty()) { - return; - } - album.getParent().removeAlbum(album); - database.removeAlbum(album); - touchConfiguration(); - } - - /** * Creates a new image. * * @param sone diff --git a/src/main/java/net/pterodactylus/sone/data/Album.java b/src/main/java/net/pterodactylus/sone/data/Album.java index 5b9d368..26da9b7 100644 --- a/src/main/java/net/pterodactylus/sone/data/Album.java +++ b/src/main/java/net/pterodactylus/sone/data/Album.java @@ -121,14 +121,6 @@ public interface Album extends Identified, Fingerprintable { List getAlbums(); /** - * Removes an album from this album. - * - * @param album - * The album to remove - */ - void removeAlbum(Album album); - - /** * Moves the given album up in this album’s albums. If the album is already the * first album, nothing happens. * @@ -211,22 +203,6 @@ public interface Album extends Identified, Fingerprintable { Album getParent(); /** - * Sets the parent album of this album. - * - * @param parent - * The new parent album of this album - * @return This album - */ - Album setParent(Album parent); - - /** - * Removes the parent album of this album. - * - * @return This album - */ - Album removeParent(); - - /** * Returns the title of this album. * * @return The title of this album @@ -253,6 +229,8 @@ public interface Album extends Identified, Fingerprintable { */ Modifier modify() throws IllegalStateException; + void remove() throws IllegalStateException; + /** * Allows modifying an album. Modifications are only performed once {@link * #update()} has succesfully returned a new album with the modifications diff --git a/src/main/java/net/pterodactylus/sone/data/SoneImpl.java b/src/main/java/net/pterodactylus/sone/data/SoneImpl.java index e929025..1f7ee1a 100644 --- a/src/main/java/net/pterodactylus/sone/data/SoneImpl.java +++ b/src/main/java/net/pterodactylus/sone/data/SoneImpl.java @@ -100,7 +100,7 @@ public class SoneImpl implements Sone { private final Set likedReplyIds = new CopyOnWriteArraySet(); /** The root album containing all albums. */ - private final Album rootAlbum = new DefaultAlbum(this); + private final Album rootAlbum = new DefaultAlbum(this, null); /** Sone-specific options. */ private Options options = new Options(); 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 74b66e7..cbef5da 100644 --- a/src/main/java/net/pterodactylus/sone/data/impl/DefaultAlbum.java +++ b/src/main/java/net/pterodactylus/sone/data/impl/DefaultAlbum.java @@ -19,6 +19,7 @@ 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; import java.util.HashMap; @@ -47,6 +48,9 @@ public class DefaultAlbum extends AbstractAlbum { /** The Sone this album belongs to. */ private Sone sone; + /** The parent album. */ + private final DefaultAlbum parent; + /** Nested albums. */ private final List albums = new ArrayList(); @@ -56,12 +60,9 @@ public class DefaultAlbum extends AbstractAlbum { /** The images in this album. */ final Map images = new HashMap(); - /** The parent album. */ - private Album parent; - /** Creates a new album with a random ID. */ - public DefaultAlbum(Sone sone) { - this(UUID.randomUUID().toString(), sone); + public DefaultAlbum(Sone sone, DefaultAlbum parent) { + this(UUID.randomUUID().toString(), sone, parent); } /** @@ -70,9 +71,10 @@ public class DefaultAlbum extends AbstractAlbum { * @param id * The ID of the album */ - public DefaultAlbum(String id, Sone sone) { + public DefaultAlbum(String id, Sone sone, DefaultAlbum parent) { super(id); this.sone = sone; + this.parent = parent; } // @@ -90,15 +92,6 @@ public class DefaultAlbum extends AbstractAlbum { } @Override - public void removeAlbum(Album album) { - checkNotNull(album, "album must not be null"); - checkArgument(album.getSone().equals(sone), "album must belong this album’s Sone"); - checkArgument(equals(album.getParent()), "album must belong to this album"); - albums.remove(album); - album.removeParent(); - } - - @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"); @@ -182,20 +175,8 @@ public class DefaultAlbum extends AbstractAlbum { } @Override - public Album setParent(Album parent) { - this.parent = checkNotNull(parent, "parent must not be null"); - return this; - } - - @Override - public Album removeParent() { - this.parent = null; - return this; - } - - @Override public AlbumBuilder newAlbumBuilder() { - return new DefaultAlbumBuilder(sone) { + return new DefaultAlbumBuilder(sone, this) { @Override public Album build() throws IllegalStateException { Album album = super.build(); @@ -221,4 +202,24 @@ public class DefaultAlbum extends AbstractAlbum { }; } + @Override + public void remove() throws IllegalStateException { + checkState(!isRoot(), "can not remove root album"); + removeAllAlbums(); + removeAllImages(); + parent.albums.remove(this); + } + + private void removeAllImages() { + for (Image image : images.values()) { + image.remove(); + } + } + + private void removeAllAlbums() { + for (Album album: albums) { + album.remove(); + } + } + } diff --git a/src/main/java/net/pterodactylus/sone/data/impl/DefaultAlbumBuilder.java b/src/main/java/net/pterodactylus/sone/data/impl/DefaultAlbumBuilder.java index cd6475a..5148471 100644 --- a/src/main/java/net/pterodactylus/sone/data/impl/DefaultAlbumBuilder.java +++ b/src/main/java/net/pterodactylus/sone/data/impl/DefaultAlbumBuilder.java @@ -29,15 +29,17 @@ import net.pterodactylus.sone.database.AlbumBuilder; public class DefaultAlbumBuilder extends AbstractAlbumBuilder { private final Sone sone; + private final DefaultAlbum parent; - public DefaultAlbumBuilder(Sone sone) { + public DefaultAlbumBuilder(Sone sone, DefaultAlbum parent) { this.sone = sone; + this.parent = parent; } @Override public Album build() throws IllegalStateException { validate(); - return new DefaultAlbum(getId(), sone); + return new DefaultAlbum(getId(), sone, parent); } } diff --git a/src/main/java/net/pterodactylus/sone/web/DeleteAlbumPage.java b/src/main/java/net/pterodactylus/sone/web/DeleteAlbumPage.java index dfb384b..0250971 100644 --- a/src/main/java/net/pterodactylus/sone/web/DeleteAlbumPage.java +++ b/src/main/java/net/pterodactylus/sone/web/DeleteAlbumPage.java @@ -63,7 +63,7 @@ public class DeleteAlbumPage extends SoneTemplatePage { throw new RedirectException("imageBrowser.html?album=" + album.get().getId()); } Album parentAlbum = album.get().getParent(); - webInterface.getCore().deleteAlbum(album.get()); + album.get().remove(); if (parentAlbum.equals(album.get().getSone().getRootAlbum())) { throw new RedirectException("imageBrowser.html?sone=" + album.get().getSone().getId()); }