Add in-memory Album implementation.
[Sone.git] / src / main / java / net / pterodactylus / sone / database / memory / MemoryAlbum.java
1 /*
2  * Sone - MemoryAlbum.java - Copyright © 2013 David Roden
3  *
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.
8  *
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.
13  *
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/>.
16  */
17
18 package net.pterodactylus.sone.database.memory;
19
20 import static com.google.common.base.Preconditions.checkState;
21
22 import java.util.List;
23
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;
32
33 import com.google.common.base.Optional;
34
35 /**
36  * TODO
37  *
38  * @author <a href="mailto:bombe@pterodactylus.net">David ‘Bombe’ Roden</a>
39  */
40 public class MemoryAlbum extends AbstractAlbum {
41
42         private final MemoryDatabase memoryDatabase;
43         private final Sone sone; /* TODO - only store sone ID. */
44         private final String parentId;
45
46         protected MemoryAlbum(MemoryDatabase memoryDatabase, String id, Sone sone, String parentId) {
47                 super(id);
48                 this.memoryDatabase = memoryDatabase;
49                 this.sone = sone;
50                 this.parentId = parentId;
51         }
52
53         @Override
54         public Sone getSone() {
55                 return sone;
56         }
57
58         @Override
59         public List<Album> getAlbums() {
60                 return memoryDatabase.getAlbums(this);
61         }
62
63         @Override
64         public List<Image> getImages() {
65                 return memoryDatabase.getImages(this);
66         }
67
68         @Override
69         public Optional<Image> getAlbumImage() {
70                 return memoryDatabase.getImage(albumImage);
71         }
72
73         @Override
74         public Album getParent() {
75                 return memoryDatabase.getAlbum(parentId).get();
76         }
77
78         @Override
79         public AlbumBuilder newAlbumBuilder() throws IllegalStateException {
80                 return new AbstractAlbumBuilder() {
81                         @Override
82                         public Album build() throws IllegalStateException {
83                                 validate();
84                                 MemoryAlbum memoryAlbum = new MemoryAlbum(memoryDatabase, getId(), sone, MemoryAlbum.this.id);
85                                 memoryDatabase.storeAlbum(memoryAlbum);
86                                 return memoryAlbum;
87                         }
88                 };
89         }
90
91         @Override
92         public ImageBuilder newImageBuilder() throws IllegalStateException {
93                 return new AbstractImageBuilder() {
94                         @Override
95                         public Image build() throws IllegalStateException {
96                                 validate();
97                                 MemoryImage memoryImage = new MemoryImage(memoryDatabase, getId(), sone, MemoryAlbum.this.id, key, getCreationTime(), width, height);
98                                 memoryDatabase.storeImage(memoryImage);
99                                 return memoryImage;
100                         }
101                 };
102         }
103
104         @Override
105         public void moveUp() {
106                 memoryDatabase.moveUp(this);
107         }
108
109         @Override
110         public void moveDown() {
111                 memoryDatabase.moveDown(this);
112         }
113
114         @Override
115         public void remove() throws IllegalStateException {
116                 checkState(!isRoot(), "can not remove root album");
117                 for (Album album : getAlbums()) {
118                         album.remove();
119                 }
120                 for (Image image : getImages()) {
121                         image.remove();
122                 }
123                 memoryDatabase.removeAlbum(this);
124         }
125
126 }