2 * Sone - Album.java - Copyright © 2011–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.checkArgument;
21 import static com.google.common.base.Preconditions.checkNotNull;
23 import java.util.ArrayList;
24 import java.util.HashMap;
25 import java.util.List;
27 import java.util.UUID;
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;
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;
41 * Dumb, store-everything-in-memory implementation of an {@link Album}.
43 * @author <a href="mailto:bombe@pterodactylus.net">David ‘Bombe’ Roden</a>
45 public class DefaultAlbum extends AbstractAlbum {
47 /** The Sone this album belongs to. */
51 private final List<Album> albums = new ArrayList<Album>();
53 /** The image IDs in order. */
54 private final List<String> imageIds = new ArrayList<String>();
56 /** The images in this album. */
57 private final Map<String, Image> images = new HashMap<String, Image>();
59 /** The parent album. */
62 /** Creates a new album with a random ID. */
63 public DefaultAlbum(Sone sone) {
64 this(UUID.randomUUID().toString(), sone);
68 * Creates a new album with the given ID.
73 public DefaultAlbum(String id, Sone sone) {
83 public Sone getSone() {
88 public List<Album> getAlbums() {
89 return new ArrayList<Album>(albums);
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");
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);
110 albums.remove(oldIndex);
111 albums.add(oldIndex - 1, album);
112 return albums.get(oldIndex);
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))) {
124 albums.remove(oldIndex);
125 albums.add(oldIndex + 1, album);
126 return albums.get(oldIndex);
130 public List<Image> getImages() {
131 return new ArrayList<Image>(Collections2.filter(Collections2.transform(imageIds, new Function<String, Image>() {
134 @SuppressWarnings("synthetic-access")
135 public Image apply(String imageId) {
136 return images.get(imageId);
138 }), Predicates.notNull()));
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()) {
152 albumImage = images.values().iterator().next().getId();
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());
167 imageIds.remove(image.getId());
168 imageIds.add(oldIndex - 1, image.getId());
169 return images.get(imageIds.get(oldIndex));
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))) {
182 imageIds.remove(image.getId());
183 imageIds.add(oldIndex + 1, image.getId());
184 return images.get(imageIds.get(oldIndex));
188 public Image getAlbumImage() {
189 if (albumImage == null) {
192 return Optional.fromNullable(images.get(albumImage)).or(images.values().iterator().next());
196 public Album getParent() {
201 public Album setParent(Album parent) {
202 this.parent = checkNotNull(parent, "parent must not be null");
207 public Album removeParent() {
213 public AlbumBuilder newAlbumBuilder() {
214 return new DefaultAlbumBuilder(sone) {
216 public Album build() throws IllegalStateException {
217 Album album = super.build();
225 public ImageBuilder newImageBuilder() throws IllegalStateException {
226 return new DefaultImageBuilder(sone, this) {
228 public Image build() throws IllegalStateException {
229 Image image = super.build();
230 if (images.isEmpty() && (albumImage == null)) {
231 albumImage = image.getId();
233 images.put(image.getId(), image);
234 imageIds.add(image.getId());