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. */
42 private final String parentId;
44 protected DefaultAlbum(Database database, String id, Sone sone, String parentId) {
46 this.database = database;
48 this.parentId = parentId;
52 public Sone getSone() {
57 public List<Album> getAlbums() {
58 return database.getAlbums(this);
62 public List<Image> getImages() {
63 return database.getImages(this);
67 public Optional<Image> getAlbumImage() {
68 return database.getImage(albumImage);
72 public Album getParent() {
73 return database.getAlbum(parentId).get();
77 public AlbumBuilder newAlbumBuilder() throws IllegalStateException {
78 return new AbstractAlbumBuilder() {
80 public Album build() throws IllegalStateException {
82 DefaultAlbum memoryAlbum = new DefaultAlbum(database, getId(), sone, DefaultAlbum.this.id);
83 database.storeAlbum(memoryAlbum);
90 public ImageBuilder newImageBuilder() throws IllegalStateException {
91 return new AbstractImageBuilder() {
93 public Image build(Optional<ImageCreated> imageCreated) throws IllegalStateException {
95 DefaultImage image = new DefaultImage(database, getId(), sone, DefaultAlbum.this.id, key, getCreationTime(), width, height);
96 database.storeImage(image);
97 if (imageCreated.isPresent()) {
98 imageCreated.get().imageCreated(image);
106 public void moveUp() {
107 database.moveUp(this);
111 public void moveDown() {
112 database.moveDown(this);
116 public void remove() throws IllegalStateException {
117 checkState(!isRoot(), "can not remove root album");
118 for (Album album : getAlbums()) {
121 for (Image image : getImages()) {
124 database.removeAlbum(this);