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.Image;
33 import net.pterodactylus.sone.data.Sone;
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;
39 import com.google.common.hash.Hasher;
40 import com.google.common.hash.Hashing;
43 * Container for images that can also contain nested {@link AlbumImpl}s.
45 * @author <a href="mailto:bombe@pterodactylus.net">David ‘Bombe’ Roden</a>
47 public class AlbumImpl implements Album {
49 /** The ID of this album. */
50 private final String id;
52 /** The Sone this album belongs to. */
53 private final Sone sone;
56 private final List<Album> albums = new ArrayList<Album>();
58 /** The image IDs in order. */
59 private final List<String> imageIds = new ArrayList<String>();
61 /** The images in this album. */
62 private final Map<String, Image> images = new HashMap<String, Image>();
64 /** The parent album. */
67 /** The title of this album. */
70 /** The description of this album. */
71 private String description;
73 /** The ID of the album picture. */
74 private String albumImage;
76 /** Creates a new album with a random ID. */
77 public AlbumImpl(Sone sone) {
78 this(sone, UUID.randomUUID().toString());
82 * Creates a new album with the given ID.
87 public AlbumImpl(Sone sone, String id) {
88 this.sone = checkNotNull(sone, "Sone must not be null");
89 this.id = checkNotNull(id, "id must not be null");
97 public String getId() {
102 public Sone getSone() {
107 public List<Album> getAlbums() {
108 return new ArrayList<Album>(albums);
112 public void addAlbum(Album album) {
113 checkNotNull(album, "album must not be null");
114 checkArgument(album.getSone().equals(sone), "album must belong to the same Sone as this album");
115 album.setParent(this);
116 if (!albums.contains(album)) {
122 public void removeAlbum(Album album) {
123 checkNotNull(album, "album must not be null");
124 checkArgument(album.getSone().equals(sone), "album must belong this album’s Sone");
125 checkArgument(equals(album.getParent()), "album must belong to this album");
126 albums.remove(album);
127 album.removeParent();
131 public Album moveAlbumUp(Album album) {
132 checkNotNull(album, "album must not be null");
133 checkArgument(album.getSone().equals(sone), "album must belong to the same Sone as this album");
134 checkArgument(equals(album.getParent()), "album must belong to this album");
135 int oldIndex = albums.indexOf(album);
139 albums.remove(oldIndex);
140 albums.add(oldIndex - 1, album);
141 return albums.get(oldIndex);
145 public Album moveAlbumDown(Album album) {
146 checkNotNull(album, "album must not be null");
147 checkArgument(album.getSone().equals(sone), "album must belong to the same Sone as this album");
148 checkArgument(equals(album.getParent()), "album must belong to this album");
149 int oldIndex = albums.indexOf(album);
150 if ((oldIndex < 0) || (oldIndex >= (albums.size() - 1))) {
153 albums.remove(oldIndex);
154 albums.add(oldIndex + 1, album);
155 return albums.get(oldIndex);
159 public List<Image> getImages() {
160 return new ArrayList<Image>(Collections2.filter(Collections2.transform(imageIds, new Function<String, Image>() {
163 @SuppressWarnings("synthetic-access")
164 public Image apply(String imageId) {
165 return images.get(imageId);
167 }), Predicates.notNull()));
171 public void addImage(Image image) {
172 checkNotNull(image, "image must not be null");
173 checkNotNull(image.getSone(), "image must have an owner");
174 checkArgument(image.getSone().equals(sone), "image must belong to the same Sone as this album");
175 if (image.getAlbum() != null) {
176 image.getAlbum().removeImage(image);
178 image.setAlbum(this);
179 if (imageIds.isEmpty() && (albumImage == null)) {
180 albumImage = image.getId();
182 if (!imageIds.contains(image.getId())) {
183 imageIds.add(image.getId());
184 images.put(image.getId(), image);
189 public void removeImage(Image image) {
190 checkNotNull(image, "image must not be null");
191 checkNotNull(image.getSone(), "image must have an owner");
192 checkArgument(image.getSone().equals(sone), "image must belong to the same Sone as this album");
193 imageIds.remove(image.getId());
194 images.remove(image.getId());
195 if (image.getId().equals(albumImage)) {
196 if (images.isEmpty()) {
199 albumImage = images.values().iterator().next().getId();
205 public Image moveImageUp(Image image) {
206 checkNotNull(image, "image must not be null");
207 checkNotNull(image.getSone(), "image must have an owner");
208 checkArgument(image.getSone().equals(sone), "image must belong to the same Sone as this album");
209 checkArgument(image.getAlbum().equals(this), "image must belong to this album");
210 int oldIndex = imageIds.indexOf(image.getId());
214 imageIds.remove(image.getId());
215 imageIds.add(oldIndex - 1, image.getId());
216 return images.get(imageIds.get(oldIndex));
220 public Image moveImageDown(Image image) {
221 checkNotNull(image, "image must not be null");
222 checkNotNull(image.getSone(), "image must have an owner");
223 checkArgument(image.getSone().equals(sone), "image must belong to the same Sone as this album");
224 checkArgument(image.getAlbum().equals(this), "image must belong to this album");
225 int oldIndex = imageIds.indexOf(image.getId());
226 if ((oldIndex == -1) || (oldIndex >= (imageIds.size() - 1))) {
229 imageIds.remove(image.getId());
230 imageIds.add(oldIndex + 1, image.getId());
231 return images.get(imageIds.get(oldIndex));
235 public Image getAlbumImage() {
236 if (albumImage == null) {
239 return Optional.fromNullable(images.get(albumImage)).or(images.values().iterator().next());
243 public boolean isEmpty() {
244 return albums.isEmpty() && images.isEmpty();
248 public boolean isRoot() {
249 return parent == null;
253 public Album getParent() {
258 public Album setParent(Album parent) {
259 this.parent = checkNotNull(parent, "parent must not be null");
264 public Album removeParent() {
270 public String getTitle() {
275 public String getDescription() {
280 public Modifier modify() throws IllegalStateException {
281 // TODO: reenable check for local Sones
282 return new Modifier() {
283 private Optional<String> title = absent();
285 private Optional<String> description = absent();
287 private Optional<String> albumImage = absent();
290 public Modifier setTitle(String title) {
291 this.title = fromNullable(title);
296 public Modifier setDescription(String description) {
297 this.description = fromNullable(description);
302 public Modifier setAlbumImage(String imageId) {
303 this.albumImage = fromNullable(imageId);
308 public Album update() throws IllegalStateException {
309 if (title.isPresent() && title.get().trim().isEmpty()) {
310 throw new AlbumTitleMustNotBeEmpty();
312 if (title.isPresent()) {
313 AlbumImpl.this.title = title.get();
315 if (description.isPresent()) {
316 AlbumImpl.this.description = description.get();
318 if (albumImage.isPresent()) {
319 AlbumImpl.this.albumImage = albumImage.get();
321 return AlbumImpl.this;
327 // FINGERPRINTABLE METHODS
331 public String getFingerprint() {
332 Hasher hash = Hashing.sha256().newHasher();
333 hash.putString("Album(");
334 hash.putString("ID(").putString(id).putString(")");
335 hash.putString("Title(").putString(title).putString(")");
336 hash.putString("Description(").putString(description).putString(")");
337 if (albumImage != null) {
338 hash.putString("AlbumImage(").putString(albumImage).putString(")");
341 /* add nested albums. */
342 hash.putString("Albums(");
343 for (Album album : albums) {
344 hash.putString(album.getFingerprint());
349 hash.putString("Images(");
350 for (Image image : getImages()) {
351 if (image.isInserted()) {
352 hash.putString(image.getFingerprint());
358 return hash.hash().toString();
366 public int hashCode() {
367 return id.hashCode();
371 public boolean equals(Object object) {
372 if (!(object instanceof AlbumImpl)) {
375 AlbumImpl album = (AlbumImpl) object;
376 return id.equals(album.id);