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.Optional.absent;
21 import static com.google.common.base.Optional.fromNullable;
22 import static com.google.common.base.Preconditions.checkArgument;
23 import static com.google.common.base.Preconditions.checkNotNull;
24 import static com.google.common.base.Preconditions.checkState;
26 import java.util.ArrayList;
27 import java.util.HashMap;
28 import java.util.List;
30 import java.util.UUID;
32 import net.pterodactylus.sone.data.Album;
33 import net.pterodactylus.sone.data.Image;
34 import net.pterodactylus.sone.data.Sone;
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;
40 import com.google.common.hash.Hasher;
41 import com.google.common.hash.Hashing;
44 * Container for images that can also contain nested {@link AlbumImpl}s.
46 * @author <a href="mailto:bombe@pterodactylus.net">David ‘Bombe’ Roden</a>
48 public class AlbumImpl implements Album {
50 /** The ID of this album. */
51 private final String id;
53 /** The Sone this album belongs to. */
54 private final Sone sone;
57 private final List<Album> albums = new ArrayList<Album>();
59 /** The image IDs in order. */
60 private final List<String> imageIds = new ArrayList<String>();
62 /** The images in this album. */
63 private final Map<String, Image> images = new HashMap<String, Image>();
65 /** The parent album. */
68 /** The title of this album. */
71 /** The description of this album. */
72 private String description;
74 /** The ID of the album picture. */
75 private String albumImage;
77 /** Creates a new album with a random ID. */
78 public AlbumImpl(Sone sone) {
79 this(sone, UUID.randomUUID().toString());
83 * Creates a new album with the given ID.
88 public AlbumImpl(Sone sone, String id) {
89 this.sone = checkNotNull(sone, "Sone must not be null");
90 this.id = checkNotNull(id, "id must not be null");
98 public String getId() {
103 public Sone getSone() {
108 public List<Album> getAlbums() {
109 return new ArrayList<Album>(albums);
113 public void addAlbum(Album album) {
114 checkNotNull(album, "album must not be null");
115 checkArgument(album.getSone().equals(sone), "album must belong to the same Sone as this album");
116 album.setParent(this);
117 if (!albums.contains(album)) {
123 public void removeAlbum(Album album) {
124 checkNotNull(album, "album must not be null");
125 checkArgument(album.getSone().equals(sone), "album must belong this album’s Sone");
126 checkArgument(equals(album.getParent()), "album must belong to this album");
127 albums.remove(album);
128 album.removeParent();
132 public Album moveAlbumUp(Album album) {
133 checkNotNull(album, "album must not be null");
134 checkArgument(album.getSone().equals(sone), "album must belong to the same Sone as this album");
135 checkArgument(equals(album.getParent()), "album must belong to this album");
136 int oldIndex = albums.indexOf(album);
140 albums.remove(oldIndex);
141 albums.add(oldIndex - 1, album);
142 return albums.get(oldIndex);
146 public Album moveAlbumDown(Album album) {
147 checkNotNull(album, "album must not be null");
148 checkArgument(album.getSone().equals(sone), "album must belong to the same Sone as this album");
149 checkArgument(equals(album.getParent()), "album must belong to this album");
150 int oldIndex = albums.indexOf(album);
151 if ((oldIndex < 0) || (oldIndex >= (albums.size() - 1))) {
154 albums.remove(oldIndex);
155 albums.add(oldIndex + 1, album);
156 return albums.get(oldIndex);
160 public List<Image> getImages() {
161 return new ArrayList<Image>(Collections2.filter(Collections2.transform(imageIds, new Function<String, Image>() {
164 @SuppressWarnings("synthetic-access")
165 public Image apply(String imageId) {
166 return images.get(imageId);
168 }), Predicates.notNull()));
172 public void addImage(Image image) {
173 checkNotNull(image, "image must not be null");
174 checkNotNull(image.getSone(), "image must have an owner");
175 checkArgument(image.getSone().equals(sone), "image must belong to the same Sone as this album");
176 if (image.getAlbum() != null) {
177 image.getAlbum().removeImage(image);
179 image.setAlbum(this);
180 if (imageIds.isEmpty() && (albumImage == null)) {
181 albumImage = image.getId();
183 if (!imageIds.contains(image.getId())) {
184 imageIds.add(image.getId());
185 images.put(image.getId(), image);
190 public void removeImage(Image image) {
191 checkNotNull(image, "image must not be null");
192 checkNotNull(image.getSone(), "image must have an owner");
193 checkArgument(image.getSone().equals(sone), "image must belong to the same Sone as this album");
194 imageIds.remove(image.getId());
195 images.remove(image.getId());
196 if (image.getId().equals(albumImage)) {
197 if (images.isEmpty()) {
200 albumImage = images.values().iterator().next().getId();
206 public Image moveImageUp(Image image) {
207 checkNotNull(image, "image must not be null");
208 checkNotNull(image.getSone(), "image must have an owner");
209 checkArgument(image.getSone().equals(sone), "image must belong to the same Sone as this album");
210 checkArgument(image.getAlbum().equals(this), "image must belong to this album");
211 int oldIndex = imageIds.indexOf(image.getId());
215 imageIds.remove(image.getId());
216 imageIds.add(oldIndex - 1, image.getId());
217 return images.get(imageIds.get(oldIndex));
221 public Image moveImageDown(Image image) {
222 checkNotNull(image, "image must not be null");
223 checkNotNull(image.getSone(), "image must have an owner");
224 checkArgument(image.getSone().equals(sone), "image must belong to the same Sone as this album");
225 checkArgument(image.getAlbum().equals(this), "image must belong to this album");
226 int oldIndex = imageIds.indexOf(image.getId());
227 if ((oldIndex == -1) || (oldIndex >= (imageIds.size() - 1))) {
230 imageIds.remove(image.getId());
231 imageIds.add(oldIndex + 1, image.getId());
232 return images.get(imageIds.get(oldIndex));
236 public Image getAlbumImage() {
237 if (albumImage == null) {
240 return Optional.fromNullable(images.get(albumImage)).or(images.values().iterator().next());
244 public boolean isEmpty() {
245 return albums.isEmpty() && images.isEmpty();
249 public boolean isRoot() {
250 return parent == null;
254 public Album getParent() {
259 public Album setParent(Album parent) {
260 this.parent = checkNotNull(parent, "parent must not be null");
265 public Album removeParent() {
271 public String getTitle() {
276 public String getDescription() {
281 public Modifier modify() throws IllegalStateException {
282 // TODO: reenable check for local Sones
283 return new Modifier() {
284 private Optional<String> title = absent();
286 private Optional<String> description = absent();
288 private Optional<String> albumImage = absent();
291 public Modifier setTitle(String title) {
292 this.title = fromNullable(title);
297 public Modifier setDescription(String description) {
298 this.description = fromNullable(description);
303 public Modifier setAlbumImage(String imageId) {
304 this.albumImage = fromNullable(imageId);
309 public Album update() throws IllegalStateException {
310 if (title.isPresent() && title.get().trim().isEmpty()) {
311 throw new AlbumTitleMustNotBeEmpty();
313 if (title.isPresent()) {
314 AlbumImpl.this.title = title.get();
316 if (description.isPresent()) {
317 AlbumImpl.this.description = description.get();
319 if (albumImage.isPresent()) {
320 AlbumImpl.this.albumImage = albumImage.get();
322 return AlbumImpl.this;
327 public static class AlbumTitleMustNotBeEmpty extends IllegalStateException { }
330 // FINGERPRINTABLE METHODS
334 public String getFingerprint() {
335 Hasher hash = Hashing.sha256().newHasher();
336 hash.putString("Album(");
337 hash.putString("ID(").putString(id).putString(")");
338 hash.putString("Title(").putString(title).putString(")");
339 hash.putString("Description(").putString(description).putString(")");
340 if (albumImage != null) {
341 hash.putString("AlbumImage(").putString(albumImage).putString(")");
344 /* add nested albums. */
345 hash.putString("Albums(");
346 for (Album album : albums) {
347 hash.putString(album.getFingerprint());
352 hash.putString("Images(");
353 for (Image image : getImages()) {
354 if (image.isInserted()) {
355 hash.putString(image.getFingerprint());
361 return hash.hash().toString();
369 public int hashCode() {
370 return id.hashCode();
374 public boolean equals(Object object) {
375 if (!(object instanceof AlbumImpl)) {
378 AlbumImpl album = (AlbumImpl) object;
379 return id.equals(album.id);