X-Git-Url: https://git.pterodactylus.net/?a=blobdiff_plain;f=src%2Fmain%2Fjava%2Fnet%2Fpterodactylus%2Fsone%2Fdata%2Fimpl%2FDefaultAlbum.java;h=cbef5da55c9b246c56ade3bb782967a75ee2cbc5;hb=f0c0d175ae5058661d3af7cc99d0c188616a4d8f;hp=6992a35552973f25d70282bf2c4150a275ad3b05;hpb=ff08aebea0013faaa8fb26ecc5b494eb4d1cdffc;p=Sone.git 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 6992a35..cbef5da 100644 --- a/src/main/java/net/pterodactylus/sone/data/impl/DefaultAlbum.java +++ b/src/main/java/net/pterodactylus/sone/data/impl/DefaultAlbum.java @@ -30,6 +30,7 @@ import java.util.UUID; import net.pterodactylus.sone.data.Album; import net.pterodactylus.sone.data.Image; import net.pterodactylus.sone.data.Sone; +import net.pterodactylus.sone.database.AlbumBuilder; import net.pterodactylus.sone.database.ImageBuilder; import com.google.common.base.Function; @@ -47,21 +48,21 @@ 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(); /** The image IDs in order. */ - private final List imageIds = new ArrayList(); + final List imageIds = new ArrayList(); /** The images in this album. */ - private final Map images = new HashMap(); - - /** The parent album. */ - private Album parent; + final Map images = new HashMap(); /** Creates a new album with a random ID. */ - public DefaultAlbum() { - this(UUID.randomUUID().toString()); + public DefaultAlbum(Sone sone, DefaultAlbum parent) { + this(UUID.randomUUID().toString(), sone, parent); } /** @@ -70,8 +71,10 @@ public class DefaultAlbum extends AbstractAlbum { * @param id * The ID of the album */ - public DefaultAlbum(String id) { + public DefaultAlbum(String id, Sone sone, DefaultAlbum parent) { super(id); + this.sone = sone; + this.parent = parent; } // @@ -84,38 +87,11 @@ public class DefaultAlbum extends AbstractAlbum { } @Override - public Album setSone(Sone sone) { - checkNotNull(sone, "sone must not be null"); - checkState((this.sone == null) || (this.sone.equals(sone)), "album owner must not already be set to some other Sone"); - this.sone = sone; - return this; - } - - @Override public List getAlbums() { return new ArrayList(albums); } @Override - public void addAlbum(Album album) { - checkNotNull(album, "album must not be null"); - checkArgument(album.getSone().equals(sone), "album must belong to the same Sone as this album"); - album.setParent(this); - if (!albums.contains(album)) { - albums.add(album); - } - } - - @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"); @@ -156,22 +132,6 @@ public class DefaultAlbum extends AbstractAlbum { } @Override - public void removeImage(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"); - 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(); - } - } - } - - @Override public Image moveImageUp(Image image) { checkNotNull(image, "image must not be null"); checkNotNull(image.getSone(), "image must have an owner"); @@ -215,15 +175,15 @@ 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; + public AlbumBuilder newAlbumBuilder() { + return new DefaultAlbumBuilder(sone, this) { + @Override + public Album build() throws IllegalStateException { + Album album = super.build(); + albums.add(album); + return album; + } + }; } @Override @@ -242,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(); + } + } + }