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;
25 import java.util.ArrayList;
26 import java.util.HashMap;
27 import java.util.List;
29 import java.util.UUID;
31 import net.pterodactylus.sone.data.Album;
32 import net.pterodactylus.sone.data.IdBuilder;
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 private final IdBuilder idBuilder = new IdBuilder();
52 /** The ID of this album. */
53 private final String id;
55 /** The Sone this album belongs to. */
56 private final Sone sone;
59 private final List<Album> albums = new ArrayList<Album>();
61 /** The image IDs in order. */
62 private final List<String> imageIds = new ArrayList<String>();
64 /** The images in this album. */
65 private final Map<String, Image> images = new HashMap<String, Image>();
67 /** The parent album. */
70 /** The title of this album. */
73 /** The description of this album. */
74 private String description;
76 /** The ID of the album picture. */
77 private String albumImage;
79 /** Creates a new album with a random ID. */
80 public AlbumImpl(Sone sone) {
81 this(sone, UUID.randomUUID().toString());
85 * Creates a new album with the given ID.
90 public AlbumImpl(Sone sone, String id) {
91 this.sone = checkNotNull(sone, "Sone must not be null");
92 this.id = checkNotNull(id, "id must not be null");
100 public String getId() {
101 return idBuilder.buildId(sone.getId(), id);
105 public String getInternalId() {
110 public Sone getSone() {
115 public List<Album> getAlbums() {
116 return new ArrayList<Album>(albums);
120 public void addAlbum(Album album) {
121 checkNotNull(album, "album must not be null");
122 checkArgument(album.getSone().equals(sone), "album must belong to the same Sone as this album");
123 album.setParent(this);
124 if (!albums.contains(album)) {
130 public void removeAlbum(Album album) {
131 checkNotNull(album, "album must not be null");
132 checkArgument(album.getSone().equals(sone), "album must belong this album’s Sone");
133 checkArgument(equals(album.getParent()), "album must belong to this album");
134 albums.remove(album);
135 album.removeParent();
139 public Album moveAlbumUp(Album album) {
140 checkNotNull(album, "album must not be null");
141 checkArgument(album.getSone().equals(sone), "album must belong to the same Sone as this album");
142 checkArgument(equals(album.getParent()), "album must belong to this album");
143 int oldIndex = albums.indexOf(album);
147 albums.remove(oldIndex);
148 albums.add(oldIndex - 1, album);
149 return albums.get(oldIndex);
153 public Album moveAlbumDown(Album album) {
154 checkNotNull(album, "album must not be null");
155 checkArgument(album.getSone().equals(sone), "album must belong to the same Sone as this album");
156 checkArgument(equals(album.getParent()), "album must belong to this album");
157 int oldIndex = albums.indexOf(album);
158 if ((oldIndex < 0) || (oldIndex >= (albums.size() - 1))) {
161 albums.remove(oldIndex);
162 albums.add(oldIndex + 1, album);
163 return albums.get(oldIndex);
167 public List<Image> getImages() {
168 return new ArrayList<Image>(Collections2.filter(Collections2.transform(imageIds, new Function<String, Image>() {
171 @SuppressWarnings("synthetic-access")
172 public Image apply(String imageId) {
173 return images.get(imageId);
175 }), Predicates.notNull()));
179 public void addImage(Image image) {
180 checkNotNull(image, "image must not be null");
181 checkNotNull(image.getSone(), "image must have an owner");
182 checkArgument(image.getSone().equals(sone), "image must belong to the same Sone as this album");
183 if (image.getAlbum() != null) {
184 image.getAlbum().removeImage(image);
186 image.setAlbum(this);
187 if (imageIds.isEmpty() && (albumImage == null)) {
188 albumImage = image.getId();
190 if (!imageIds.contains(image.getId())) {
191 imageIds.add(image.getId());
192 images.put(image.getId(), image);
197 public void removeImage(Image image) {
198 checkNotNull(image, "image must not be null");
199 checkNotNull(image.getSone(), "image must have an owner");
200 checkArgument(image.getSone().equals(sone), "image must belong to the same Sone as this album");
201 imageIds.remove(image.getId());
202 images.remove(image.getId());
203 if (image.getId().equals(albumImage)) {
204 if (images.isEmpty()) {
207 albumImage = images.values().iterator().next().getId();
213 public Image moveImageUp(Image image) {
214 checkNotNull(image, "image must not be null");
215 checkNotNull(image.getSone(), "image must have an owner");
216 checkArgument(image.getSone().equals(sone), "image must belong to the same Sone as this album");
217 checkArgument(image.getAlbum().equals(this), "image must belong to this album");
218 int oldIndex = imageIds.indexOf(image.getId());
222 imageIds.remove(image.getId());
223 imageIds.add(oldIndex - 1, image.getId());
224 return images.get(imageIds.get(oldIndex));
228 public Image moveImageDown(Image image) {
229 checkNotNull(image, "image must not be null");
230 checkNotNull(image.getSone(), "image must have an owner");
231 checkArgument(image.getSone().equals(sone), "image must belong to the same Sone as this album");
232 checkArgument(image.getAlbum().equals(this), "image must belong to this album");
233 int oldIndex = imageIds.indexOf(image.getId());
234 if ((oldIndex == -1) || (oldIndex >= (imageIds.size() - 1))) {
237 imageIds.remove(image.getId());
238 imageIds.add(oldIndex + 1, image.getId());
239 return images.get(imageIds.get(oldIndex));
243 public Image getAlbumImage() {
244 if (albumImage == null) {
247 return Optional.fromNullable(images.get(albumImage)).or(images.values().iterator().next());
251 public boolean isEmpty() {
252 return albums.isEmpty() && images.isEmpty();
256 public boolean isRoot() {
257 return parent == null;
261 public Album getParent() {
266 public Album setParent(Album parent) {
267 this.parent = checkNotNull(parent, "parent must not be null");
272 public Album removeParent() {
278 public String getTitle() {
283 public String getDescription() {
288 public Modifier modify() throws IllegalStateException {
289 // TODO: reenable check for local Sones
290 return new Modifier() {
291 private Optional<String> title = absent();
293 private Optional<String> description = absent();
295 private Optional<String> albumImage = absent();
298 public Modifier setTitle(String title) {
299 this.title = fromNullable(title);
304 public Modifier setDescription(String description) {
305 this.description = fromNullable(description);
310 public Modifier setAlbumImage(String imageId) {
311 this.albumImage = fromNullable(imageId);
316 public Album update() throws IllegalStateException {
317 if (title.isPresent() && title.get().trim().isEmpty()) {
318 throw new AlbumTitleMustNotBeEmpty();
320 if (title.isPresent()) {
321 AlbumImpl.this.title = title.get();
323 if (description.isPresent()) {
324 AlbumImpl.this.description = description.get();
326 if (albumImage.isPresent()) {
327 AlbumImpl.this.albumImage = albumImage.get();
329 return AlbumImpl.this;
335 // FINGERPRINTABLE METHODS
339 public String getFingerprint() {
340 Hasher hash = Hashing.sha256().newHasher();
341 hash.putString("Album(");
342 hash.putString("ID(").putString(id).putString(")");
343 hash.putString("Title(").putString(title).putString(")");
344 hash.putString("Description(").putString(description).putString(")");
345 if (albumImage != null) {
346 hash.putString("AlbumImage(").putString(albumImage).putString(")");
349 /* add nested albums. */
350 hash.putString("Albums(");
351 for (Album album : albums) {
352 hash.putString(album.getFingerprint());
357 hash.putString("Images(");
358 for (Image image : getImages()) {
359 if (image.isInserted()) {
360 hash.putString(image.getFingerprint());
366 return hash.hash().toString();
374 public int hashCode() {
375 return id.hashCode();
379 public boolean equals(Object object) {
380 if (!(object instanceof AlbumImpl)) {
383 AlbumImpl album = (AlbumImpl) object;
384 return id.equals(album.id);