2 * Sone - MemoryAlbum.java - Copyright © 2013 David Roden
4 * This program is free software: you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation, either version 3 of the License, or
7 * (at your option) any later version.
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
14 * You should have received a copy of the GNU General Public License
15 * along with this program. If not, see <http://www.gnu.org/licenses/>.
18 package net.pterodactylus.sone.data.impl;
20 import static com.google.common.base.Preconditions.checkState;
22 import java.util.List;
24 import net.pterodactylus.sone.data.Album;
25 import net.pterodactylus.sone.data.Image;
26 import net.pterodactylus.sone.data.Sone;
27 import net.pterodactylus.sone.database.AlbumBuilder;
28 import net.pterodactylus.sone.database.Database;
29 import net.pterodactylus.sone.database.ImageBuilder;
31 import com.google.common.base.Optional;
36 * @author <a href="mailto:bombe@pterodactylus.net">David ‘Bombe’ Roden</a>
38 public class DefaultAlbum extends AbstractAlbum {
40 private final Database database;
41 private final Sone sone; /* TODO - only store sone ID. */
43 protected DefaultAlbum(Database database, String id, Sone sone, String parentId) {
45 this.database = database;
50 public Sone getSone() {
55 public List<Album> getAlbums() {
56 return database.getAlbums(this);
60 public List<Image> getImages() {
61 return database.getImages(this);
65 public Optional<Image> getAlbumImage() {
66 return database.getImage(albumImage);
70 public Album getParent() {
71 return database.getAlbum(parentId).get();
75 public AlbumBuilder newAlbumBuilder() throws IllegalStateException {
76 return new AbstractAlbumBuilder() {
78 public Album build() throws IllegalStateException {
80 DefaultAlbum memoryAlbum = new DefaultAlbum(database, getId(), sone, DefaultAlbum.this.id);
81 database.storeAlbum(memoryAlbum);
88 public ImageBuilder newImageBuilder() throws IllegalStateException {
89 return new AbstractImageBuilder() {
91 public Image build(Optional<ImageCreated> imageCreated) throws IllegalStateException {
93 DefaultImage image = new DefaultImage(database, getId(), sone, DefaultAlbum.this.id, key, getCreationTime(), width, height);
94 database.storeImage(image);
95 if (imageCreated.isPresent()) {
96 imageCreated.get().imageCreated(image);
104 public void moveUp() {
105 database.moveUp(this);
109 public void moveDown() {
110 database.moveDown(this);
114 public void remove() throws IllegalStateException {
115 checkState(!isRoot(), "can not remove root album");
116 for (Album album : getAlbums()) {
119 for (Image image : getImages()) {
122 database.removeAlbum(this);