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;
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 com.google.common.base.Function;
33 import com.google.common.base.Optional;
34 import com.google.common.base.Predicates;
35 import com.google.common.collect.Collections2;
36 import com.google.common.hash.Hasher;
37 import com.google.common.hash.Hashing;
40 * Container for images that can also contain nested {@link AlbumImpl}s.
42 * @author <a href="mailto:bombe@pterodactylus.net">David ‘Bombe’ Roden</a>
44 public class AlbumImpl implements Album {
46 /** The ID of this album. */
47 private final String id;
49 /** The Sone this album belongs to. */
53 private final List<Album> albums = new ArrayList<Album>();
55 /** The image IDs in order. */
56 private final List<String> imageIds = new ArrayList<String>();
58 /** The images in this album. */
59 private final Map<String, Image> images = new HashMap<String, Image>();
61 /** The parent album. */
64 /** The title of this album. */
67 /** The description of this album. */
68 private String description;
70 /** The ID of the album picture. */
71 private String albumImage;
73 /** Creates a new album with a random ID. */
75 this(UUID.randomUUID().toString());
79 * Creates a new album with the given ID.
84 public AlbumImpl(String id) {
85 this.id = checkNotNull(id, "id must not be null");
93 public String getId() {
98 public Sone getSone() {
103 public Album setSone(Sone sone) {
104 checkNotNull(sone, "sone must not be null");
105 checkState((this.sone == null) || (this.sone.equals(sone)), "album owner must not already be set to some other Sone");
111 public List<Album> getAlbums() {
112 return new ArrayList<Album>(albums);
116 public void addAlbum(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 album.setParent(this);
120 if (!albums.contains(album)) {
126 public void removeAlbum(Album album) {
127 checkNotNull(album, "album must not be null");
128 checkArgument(album.getSone().equals(sone), "album must belong this album’s Sone");
129 checkArgument(equals(album.getParent()), "album must belong to this album");
130 albums.remove(album);
131 album.removeParent();
135 public Album moveAlbumUp(Album album) {
136 checkNotNull(album, "album must not be null");
137 checkArgument(album.getSone().equals(sone), "album must belong to the same Sone as this album");
138 checkArgument(equals(album.getParent()), "album must belong to this album");
139 int oldIndex = albums.indexOf(album);
143 albums.remove(oldIndex);
144 albums.add(oldIndex - 1, album);
145 return albums.get(oldIndex);
149 public Album moveAlbumDown(Album album) {
150 checkNotNull(album, "album must not be null");
151 checkArgument(album.getSone().equals(sone), "album must belong to the same Sone as this album");
152 checkArgument(equals(album.getParent()), "album must belong to this album");
153 int oldIndex = albums.indexOf(album);
154 if ((oldIndex < 0) || (oldIndex >= (albums.size() - 1))) {
157 albums.remove(oldIndex);
158 albums.add(oldIndex + 1, album);
159 return albums.get(oldIndex);
163 public List<Image> getImages() {
164 return new ArrayList<Image>(Collections2.filter(Collections2.transform(imageIds, new Function<String, Image>() {
167 @SuppressWarnings("synthetic-access")
168 public Image apply(String imageId) {
169 return images.get(imageId);
171 }), Predicates.notNull()));
175 public void addImage(Image image) {
176 checkNotNull(image, "image must not be null");
177 checkNotNull(image.getSone(), "image must have an owner");
178 checkArgument(image.getSone().equals(sone), "image must belong to the same Sone as this album");
179 if (image.getAlbum() != null) {
180 image.getAlbum().removeImage(image);
182 image.setAlbum(this);
183 if (imageIds.isEmpty() && (albumImage == null)) {
184 albumImage = image.getId();
186 if (!imageIds.contains(image.getId())) {
187 imageIds.add(image.getId());
188 images.put(image.getId(), image);
193 public void removeImage(Image image) {
194 checkNotNull(image, "image must not be null");
195 checkNotNull(image.getSone(), "image must have an owner");
196 checkArgument(image.getSone().equals(sone), "image must belong to the same Sone as this album");
197 imageIds.remove(image.getId());
198 images.remove(image.getId());
199 if (image.getId().equals(albumImage)) {
200 if (images.isEmpty()) {
203 albumImage = images.values().iterator().next().getId();
209 public Image moveImageUp(Image image) {
210 checkNotNull(image, "image must not be null");
211 checkNotNull(image.getSone(), "image must have an owner");
212 checkArgument(image.getSone().equals(sone), "image must belong to the same Sone as this album");
213 checkArgument(image.getAlbum().equals(this), "image must belong to this album");
214 int oldIndex = imageIds.indexOf(image.getId());
218 imageIds.remove(image.getId());
219 imageIds.add(oldIndex - 1, image.getId());
220 return images.get(imageIds.get(oldIndex));
224 public Image moveImageDown(Image image) {
225 checkNotNull(image, "image must not be null");
226 checkNotNull(image.getSone(), "image must have an owner");
227 checkArgument(image.getSone().equals(sone), "image must belong to the same Sone as this album");
228 checkArgument(image.getAlbum().equals(this), "image must belong to this album");
229 int oldIndex = imageIds.indexOf(image.getId());
230 if ((oldIndex == -1) || (oldIndex >= (imageIds.size() - 1))) {
233 imageIds.remove(image.getId());
234 imageIds.add(oldIndex + 1, image.getId());
235 return images.get(imageIds.get(oldIndex));
239 public Image getAlbumImage() {
240 if (albumImage == null) {
243 return Optional.fromNullable(images.get(albumImage)).or(images.values().iterator().next());
247 public boolean isEmpty() {
248 return albums.isEmpty() && images.isEmpty();
252 public boolean isRoot() {
253 return parent == null;
257 public Album getParent() {
262 public Album setParent(Album parent) {
263 this.parent = checkNotNull(parent, "parent must not be null");
268 public Album removeParent() {
274 public String getTitle() {
279 public String getDescription() {
284 public Modifier modify() throws IllegalStateException {
285 // TODO: reenable check for local Sones
286 return new Modifier() {
287 private Optional<String> title = absent();
289 private Optional<String> description = absent();
291 private Optional<String> albumImage = absent();
294 public Modifier setTitle(String title) {
295 this.title = fromNullable(title);
300 public Modifier setDescription(String description) {
301 this.description = fromNullable(description);
306 public Modifier setAlbumImage(String imageId) {
307 this.albumImage = fromNullable(imageId);
312 public Album update() throws IllegalStateException {
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;
328 // FINGERPRINTABLE METHODS
332 public String getFingerprint() {
333 Hasher hash = Hashing.sha256().newHasher();
334 hash.putString("Album(");
335 hash.putString("ID(").putString(id).putString(")");
336 hash.putString("Title(").putString(title).putString(")");
337 hash.putString("Description(").putString(description).putString(")");
338 if (albumImage != null) {
339 hash.putString("AlbumImage(").putString(albumImage).putString(")");
342 /* add nested albums. */
343 hash.putString("Albums(");
344 for (Album album : albums) {
345 hash.putString(album.getFingerprint());
350 hash.putString("Images(");
351 for (Image image : getImages()) {
352 if (image.isInserted()) {
353 hash.putString(image.getFingerprint());
359 return hash.hash().toString();
367 public int hashCode() {
368 return id.hashCode();
372 public boolean equals(Object object) {
373 if (!(object instanceof AlbumImpl)) {
376 AlbumImpl album = (AlbumImpl) object;
377 return id.equals(album.id);