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 final List<String> imageIds = new ArrayList<String>();
56 /** The images in this album. */
57 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 Image moveImageUp(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 checkArgument(image.getAlbum().equals(this), "image must belong to this album");
147 int oldIndex = imageIds.indexOf(image.getId());
151 imageIds.remove(image.getId());
152 imageIds.add(oldIndex - 1, image.getId());
153 return images.get(imageIds.get(oldIndex));
157 public Image moveImageDown(Image image) {
158 checkNotNull(image, "image must not be null");
159 checkNotNull(image.getSone(), "image must have an owner");
160 checkArgument(image.getSone().equals(sone), "image must belong to the same Sone as this album");
161 checkArgument(image.getAlbum().equals(this), "image must belong to this album");
162 int oldIndex = imageIds.indexOf(image.getId());
163 if ((oldIndex == -1) || (oldIndex >= (imageIds.size() - 1))) {
166 imageIds.remove(image.getId());
167 imageIds.add(oldIndex + 1, image.getId());
168 return images.get(imageIds.get(oldIndex));
172 public Image getAlbumImage() {
173 if (albumImage == null) {
176 return Optional.fromNullable(images.get(albumImage)).or(images.values().iterator().next());
180 public Album getParent() {
185 public Album setParent(Album parent) {
186 this.parent = checkNotNull(parent, "parent must not be null");
191 public Album removeParent() {
197 public AlbumBuilder newAlbumBuilder() {
198 return new DefaultAlbumBuilder(sone) {
200 public Album build() throws IllegalStateException {
201 Album album = super.build();
209 public ImageBuilder newImageBuilder() throws IllegalStateException {
210 return new DefaultImageBuilder(sone, this) {
212 public Image build() throws IllegalStateException {
213 Image image = super.build();
214 if (images.isEmpty() && (albumImage == null)) {
215 albumImage = image.getId();
217 images.put(image.getId(), image);
218 imageIds.add(image.getId());