Set Sone of an album in the album builder, use album builder to add albums.
[Sone.git] / src / main / java / net / pterodactylus / sone / data / impl / DefaultAlbum.java
1 /*
2  * Sone - Album.java - Copyright © 2011–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.data.impl;
19
20 import static com.google.common.base.Preconditions.checkArgument;
21 import static com.google.common.base.Preconditions.checkNotNull;
22
23 import java.util.ArrayList;
24 import java.util.HashMap;
25 import java.util.List;
26 import java.util.Map;
27 import java.util.UUID;
28
29 import net.pterodactylus.sone.data.Album;
30 import net.pterodactylus.sone.data.Image;
31 import net.pterodactylus.sone.data.Sone;
32 import net.pterodactylus.sone.database.AlbumBuilder;
33 import net.pterodactylus.sone.database.ImageBuilder;
34
35 import com.google.common.base.Function;
36 import com.google.common.base.Optional;
37 import com.google.common.base.Predicates;
38 import com.google.common.collect.Collections2;
39
40 /**
41  * Dumb, store-everything-in-memory implementation of an {@link Album}.
42  *
43  * @author <a href="mailto:bombe@pterodactylus.net">David ‘Bombe’ Roden</a>
44  */
45 public class DefaultAlbum extends AbstractAlbum {
46
47         /** The Sone this album belongs to. */
48         private Sone sone;
49
50         /** Nested albums. */
51         private final List<Album> albums = new ArrayList<Album>();
52
53         /** The image IDs in order. */
54         private final List<String> imageIds = new ArrayList<String>();
55
56         /** The images in this album. */
57         private final Map<String, Image> images = new HashMap<String, Image>();
58
59         /** The parent album. */
60         private Album parent;
61
62         /** Creates a new album with a random ID. */
63         public DefaultAlbum(Sone sone) {
64                 this(UUID.randomUUID().toString(), sone);
65         }
66
67         /**
68          * Creates a new album with the given ID.
69          *
70          * @param id
71          *              The ID of the album
72          */
73         public DefaultAlbum(String id, Sone sone) {
74                 super(id);
75                 this.sone = sone;
76         }
77
78         //
79         // ACCESSORS
80         //
81
82         @Override
83         public Sone getSone() {
84                 return sone;
85         }
86
87         @Override
88         public List<Album> getAlbums() {
89                 return new ArrayList<Album>(albums);
90         }
91
92         @Override
93         public void removeAlbum(Album album) {
94                 checkNotNull(album, "album must not be null");
95                 checkArgument(album.getSone().equals(sone), "album must belong this album’s Sone");
96                 checkArgument(equals(album.getParent()), "album must belong to this album");
97                 albums.remove(album);
98                 album.removeParent();
99         }
100
101         @Override
102         public Album moveAlbumUp(Album album) {
103                 checkNotNull(album, "album must not be null");
104                 checkArgument(album.getSone().equals(sone), "album must belong to the same Sone as this album");
105                 checkArgument(equals(album.getParent()), "album must belong to this album");
106                 int oldIndex = albums.indexOf(album);
107                 if (oldIndex <= 0) {
108                         return null;
109                 }
110                 albums.remove(oldIndex);
111                 albums.add(oldIndex - 1, album);
112                 return albums.get(oldIndex);
113         }
114
115         @Override
116         public Album moveAlbumDown(Album album) {
117                 checkNotNull(album, "album must not be null");
118                 checkArgument(album.getSone().equals(sone), "album must belong to the same Sone as this album");
119                 checkArgument(equals(album.getParent()), "album must belong to this album");
120                 int oldIndex = albums.indexOf(album);
121                 if ((oldIndex < 0) || (oldIndex >= (albums.size() - 1))) {
122                         return null;
123                 }
124                 albums.remove(oldIndex);
125                 albums.add(oldIndex + 1, album);
126                 return albums.get(oldIndex);
127         }
128
129         @Override
130         public List<Image> getImages() {
131                 return new ArrayList<Image>(Collections2.filter(Collections2.transform(imageIds, new Function<String, Image>() {
132
133                         @Override
134                         @SuppressWarnings("synthetic-access")
135                         public Image apply(String imageId) {
136                                 return images.get(imageId);
137                         }
138                 }), Predicates.notNull()));
139         }
140
141         @Override
142         public void removeImage(Image image) {
143                 checkNotNull(image, "image must not be null");
144                 checkNotNull(image.getSone(), "image must have an owner");
145                 checkArgument(image.getSone().equals(sone), "image must belong to the same Sone as this album");
146                 imageIds.remove(image.getId());
147                 images.remove(image.getId());
148                 if (image.getId().equals(albumImage)) {
149                         if (images.isEmpty()) {
150                                 albumImage = null;
151                         } else {
152                                 albumImage = images.values().iterator().next().getId();
153                         }
154                 }
155         }
156
157         @Override
158         public Image moveImageUp(Image image) {
159                 checkNotNull(image, "image must not be null");
160                 checkNotNull(image.getSone(), "image must have an owner");
161                 checkArgument(image.getSone().equals(sone), "image must belong to the same Sone as this album");
162                 checkArgument(image.getAlbum().equals(this), "image must belong to this album");
163                 int oldIndex = imageIds.indexOf(image.getId());
164                 if (oldIndex <= 0) {
165                         return null;
166                 }
167                 imageIds.remove(image.getId());
168                 imageIds.add(oldIndex - 1, image.getId());
169                 return images.get(imageIds.get(oldIndex));
170         }
171
172         @Override
173         public Image moveImageDown(Image image) {
174                 checkNotNull(image, "image must not be null");
175                 checkNotNull(image.getSone(), "image must have an owner");
176                 checkArgument(image.getSone().equals(sone), "image must belong to the same Sone as this album");
177                 checkArgument(image.getAlbum().equals(this), "image must belong to this album");
178                 int oldIndex = imageIds.indexOf(image.getId());
179                 if ((oldIndex == -1) || (oldIndex >= (imageIds.size() - 1))) {
180                         return null;
181                 }
182                 imageIds.remove(image.getId());
183                 imageIds.add(oldIndex + 1, image.getId());
184                 return images.get(imageIds.get(oldIndex));
185         }
186
187         @Override
188         public Image getAlbumImage() {
189                 if (albumImage == null) {
190                         return null;
191                 }
192                 return Optional.fromNullable(images.get(albumImage)).or(images.values().iterator().next());
193         }
194
195         @Override
196         public Album getParent() {
197                 return parent;
198         }
199
200         @Override
201         public Album setParent(Album parent) {
202                 this.parent = checkNotNull(parent, "parent must not be null");
203                 return this;
204         }
205
206         @Override
207         public Album removeParent() {
208                 this.parent = null;
209                 return this;
210         }
211
212         @Override
213         public AlbumBuilder newAlbumBuilder() {
214                 return new DefaultAlbumBuilder(sone) {
215                         @Override
216                         public Album build() throws IllegalStateException {
217                                 Album album = super.build();
218                                 albums.add(album);
219                                 return album;
220                         }
221                 };
222         }
223
224         @Override
225         public ImageBuilder newImageBuilder() throws IllegalStateException {
226                 return new DefaultImageBuilder(sone, this) {
227                         @Override
228                         public Image build() throws IllegalStateException {
229                                 Image image = super.build();
230                                 if (images.isEmpty() && (albumImage == null)) {
231                                         albumImage = image.getId();
232                                 }
233                                 images.put(image.getId(), image);
234                                 imageIds.add(image.getId());
235                                 return image;
236                         }
237                 };
238         }
239
240 }