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;
22 import static com.google.common.base.Preconditions.checkState;
24 import java.util.ArrayList;
25 import java.util.HashMap;
26 import java.util.List;
28 import java.util.UUID;
30 import net.pterodactylus.sone.data.Album;
31 import net.pterodactylus.sone.data.Image;
32 import net.pterodactylus.sone.data.Sone;
33 import net.pterodactylus.sone.database.AlbumBuilder;
34 import net.pterodactylus.sone.database.ImageBuilder;
36 import com.google.common.base.Function;
37 import com.google.common.base.Optional;
38 import com.google.common.base.Predicates;
39 import com.google.common.collect.Collections2;
42 * Dumb, store-everything-in-memory implementation of an {@link Album}.
44 * @author <a href="mailto:bombe@pterodactylus.net">David ‘Bombe’ Roden</a>
46 public class DefaultAlbum extends AbstractAlbum {
48 /** The Sone this album belongs to. */
51 /** The parent album. */
52 private final DefaultAlbum parent;
55 private final List<Album> albums = new ArrayList<Album>();
57 /** The image IDs in order. */
58 final List<String> imageIds = new ArrayList<String>();
60 /** The images in this album. */
61 final Map<String, Image> images = new HashMap<String, Image>();
63 /** Creates a new album with a random ID. */
64 public DefaultAlbum(Sone sone, DefaultAlbum parent) {
65 this(UUID.randomUUID().toString(), sone, parent);
69 * Creates a new album with the given ID.
74 public DefaultAlbum(String id, Sone sone, DefaultAlbum parent) {
85 public Sone getSone() {
90 public List<Album> getAlbums() {
91 return new ArrayList<Album>(albums);
95 public List<Image> getImages() {
96 return new ArrayList<Image>(Collections2.filter(Collections2.transform(imageIds, new Function<String, Image>() {
99 @SuppressWarnings("synthetic-access")
100 public Image apply(String imageId) {
101 return images.get(imageId);
103 }), Predicates.notNull()));
107 public Image moveImageUp(Image image) {
108 checkNotNull(image, "image must not be null");
109 checkNotNull(image.getSone(), "image must have an owner");
110 checkArgument(image.getSone().equals(sone), "image must belong to the same Sone as this album");
111 checkArgument(image.getAlbum().equals(this), "image must belong to this album");
112 int oldIndex = imageIds.indexOf(image.getId());
116 imageIds.remove(image.getId());
117 imageIds.add(oldIndex - 1, image.getId());
118 return images.get(imageIds.get(oldIndex));
122 public Image moveImageDown(Image image) {
123 checkNotNull(image, "image must not be null");
124 checkNotNull(image.getSone(), "image must have an owner");
125 checkArgument(image.getSone().equals(sone), "image must belong to the same Sone as this album");
126 checkArgument(image.getAlbum().equals(this), "image must belong to this album");
127 int oldIndex = imageIds.indexOf(image.getId());
128 if ((oldIndex == -1) || (oldIndex >= (imageIds.size() - 1))) {
131 imageIds.remove(image.getId());
132 imageIds.add(oldIndex + 1, image.getId());
133 return images.get(imageIds.get(oldIndex));
137 public Image getAlbumImage() {
138 if (albumImage == null) {
141 return Optional.fromNullable(images.get(albumImage)).or(images.values().iterator().next());
145 public Album getParent() {
150 public AlbumBuilder newAlbumBuilder() {
151 return new DefaultAlbumBuilder(sone, this) {
153 public Album build() throws IllegalStateException {
154 Album album = super.build();
162 public ImageBuilder newImageBuilder() throws IllegalStateException {
163 return new DefaultImageBuilder(sone, this) {
165 public Image build() throws IllegalStateException {
166 Image image = super.build();
167 if (images.isEmpty() && (albumImage == null)) {
168 albumImage = image.getId();
170 images.put(image.getId(), image);
171 imageIds.add(image.getId());
178 public void moveUp() {
179 int oldIndex = parent.albums.indexOf(this);
180 parent.albums.remove(this);
181 parent.albums.add(Math.max(0, oldIndex - 1), this);
185 public void moveDown() {
186 int oldIndex = parent.albums.indexOf(this);
187 parent.albums.remove(this);
188 parent.albums.add(Math.min(parent.albums.size(), oldIndex + 1), this);
192 public void remove() throws IllegalStateException {
193 checkState(!isRoot(), "can not remove root album");
196 parent.albums.remove(this);
199 private void removeAllImages() {
200 for (Image image : images.values()) {
205 private void removeAllAlbums() {
206 for (Album album: albums) {