followSone(sone, friendId);
}
for (Album album : sone.getRootAlbum().getAlbums()) {
- sone.getRootAlbum().removeAlbum(album);
+ album.remove();
}
soneInserters.get(sone).setLastInsertFingerprint(lastInsertFingerprint);
}
}
/**
- * 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
List<Album> 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.
*
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
*/
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
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;
/** The Sone this album belongs to. */
private Sone sone;
+ /** The parent album. */
+ private final DefaultAlbum parent;
+
/** Nested albums. */
private final List<Album> albums = new ArrayList<Album>();
/** The images in this album. */
final Map<String, Image> images = new HashMap<String, Image>();
- /** 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);
}
/**
* @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;
}
//
}
@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");
}
@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();
};
}
+ @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();
+ }
+ }
+
}