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.database.memory;
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.data.impl.AbstractAlbum;
28 import net.pterodactylus.sone.data.impl.AbstractAlbumBuilder;
29 import net.pterodactylus.sone.data.impl.AbstractImageBuilder;
30 import net.pterodactylus.sone.database.AlbumBuilder;
31 import net.pterodactylus.sone.database.ImageBuilder;
33 import com.google.common.base.Optional;
38 * @author <a href="mailto:bombe@pterodactylus.net">David ‘Bombe’ Roden</a>
40 public class MemoryAlbum extends AbstractAlbum {
42 private final MemoryDatabase memoryDatabase;
43 private final Sone sone; /* TODO - only store sone ID. */
44 private final String parentId;
46 protected MemoryAlbum(MemoryDatabase memoryDatabase, String id, Sone sone, String parentId) {
48 this.memoryDatabase = memoryDatabase;
50 this.parentId = parentId;
54 public Sone getSone() {
59 public List<Album> getAlbums() {
60 return memoryDatabase.getAlbums(this);
64 public List<Image> getImages() {
65 return memoryDatabase.getImages(this);
69 public Optional<Image> getAlbumImage() {
70 return memoryDatabase.getImage(albumImage);
74 public Album getParent() {
75 return memoryDatabase.getAlbum(parentId).get();
79 public AlbumBuilder newAlbumBuilder() throws IllegalStateException {
80 return new AbstractAlbumBuilder() {
82 public Album build() throws IllegalStateException {
84 MemoryAlbum memoryAlbum = new MemoryAlbum(memoryDatabase, getId(), sone, MemoryAlbum.this.id);
85 memoryDatabase.storeAlbum(memoryAlbum);
92 public ImageBuilder newImageBuilder() throws IllegalStateException {
93 return new AbstractImageBuilder() {
95 public Image build() throws IllegalStateException {
97 MemoryImage memoryImage = new MemoryImage(memoryDatabase, getId(), sone, MemoryAlbum.this.id, key, getCreationTime(), width, height);
98 memoryDatabase.storeImage(memoryImage);
105 public void moveUp() {
106 memoryDatabase.moveUp(this);
110 public void moveDown() {
111 memoryDatabase.moveDown(this);
115 public void remove() throws IllegalStateException {
116 checkState(!isRoot(), "can not remove root album");
117 for (Album album : getAlbums()) {
120 for (Image image : getImages()) {
123 memoryDatabase.removeAlbum(this);