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. */
50 private final Sone sone;
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. */
74 public AlbumImpl(Sone sone) {
75 this(sone, UUID.randomUUID().toString());
79 * Creates a new album with the given ID.
84 public AlbumImpl(Sone sone, String id) {
85 this.sone = checkNotNull(sone, "Sone must not be null");
86 this.id = checkNotNull(id, "id must not be null");
94 public String getId() {
99 public Sone getSone() {
104 public List<Album> getAlbums() {
105 return new ArrayList<Album>(albums);
109 public void addAlbum(Album album) {
110 checkNotNull(album, "album must not be null");
111 checkArgument(album.getSone().equals(sone), "album must belong to the same Sone as this album");
112 album.setParent(this);
113 if (!albums.contains(album)) {
119 public void removeAlbum(Album album) {
120 checkNotNull(album, "album must not be null");
121 checkArgument(album.getSone().equals(sone), "album must belong this album’s Sone");
122 checkArgument(equals(album.getParent()), "album must belong to this album");
123 albums.remove(album);
124 album.removeParent();
128 public Album moveAlbumUp(Album album) {
129 checkNotNull(album, "album must not be null");
130 checkArgument(album.getSone().equals(sone), "album must belong to the same Sone as this album");
131 checkArgument(equals(album.getParent()), "album must belong to this album");
132 int oldIndex = albums.indexOf(album);
136 albums.remove(oldIndex);
137 albums.add(oldIndex - 1, album);
138 return albums.get(oldIndex);
142 public Album moveAlbumDown(Album album) {
143 checkNotNull(album, "album must not be null");
144 checkArgument(album.getSone().equals(sone), "album must belong to the same Sone as this album");
145 checkArgument(equals(album.getParent()), "album must belong to this album");
146 int oldIndex = albums.indexOf(album);
147 if ((oldIndex < 0) || (oldIndex >= (albums.size() - 1))) {
150 albums.remove(oldIndex);
151 albums.add(oldIndex + 1, album);
152 return albums.get(oldIndex);
156 public List<Image> getImages() {
157 return new ArrayList<Image>(Collections2.filter(Collections2.transform(imageIds, new Function<String, Image>() {
160 @SuppressWarnings("synthetic-access")
161 public Image apply(String imageId) {
162 return images.get(imageId);
164 }), Predicates.notNull()));
168 public void addImage(Image image) {
169 checkNotNull(image, "image must not be null");
170 checkNotNull(image.getSone(), "image must have an owner");
171 checkArgument(image.getSone().equals(sone), "image must belong to the same Sone as this album");
172 if (image.getAlbum() != null) {
173 image.getAlbum().removeImage(image);
175 image.setAlbum(this);
176 if (imageIds.isEmpty() && (albumImage == null)) {
177 albumImage = image.getId();
179 if (!imageIds.contains(image.getId())) {
180 imageIds.add(image.getId());
181 images.put(image.getId(), image);
186 public void removeImage(Image image) {
187 checkNotNull(image, "image must not be null");
188 checkNotNull(image.getSone(), "image must have an owner");
189 checkArgument(image.getSone().equals(sone), "image must belong to the same Sone as this album");
190 imageIds.remove(image.getId());
191 images.remove(image.getId());
192 if (image.getId().equals(albumImage)) {
193 if (images.isEmpty()) {
196 albumImage = images.values().iterator().next().getId();
202 public Image moveImageUp(Image image) {
203 checkNotNull(image, "image must not be null");
204 checkNotNull(image.getSone(), "image must have an owner");
205 checkArgument(image.getSone().equals(sone), "image must belong to the same Sone as this album");
206 checkArgument(image.getAlbum().equals(this), "image must belong to this album");
207 int oldIndex = imageIds.indexOf(image.getId());
211 imageIds.remove(image.getId());
212 imageIds.add(oldIndex - 1, image.getId());
213 return images.get(imageIds.get(oldIndex));
217 public Image moveImageDown(Image image) {
218 checkNotNull(image, "image must not be null");
219 checkNotNull(image.getSone(), "image must have an owner");
220 checkArgument(image.getSone().equals(sone), "image must belong to the same Sone as this album");
221 checkArgument(image.getAlbum().equals(this), "image must belong to this album");
222 int oldIndex = imageIds.indexOf(image.getId());
223 if ((oldIndex == -1) || (oldIndex >= (imageIds.size() - 1))) {
226 imageIds.remove(image.getId());
227 imageIds.add(oldIndex + 1, image.getId());
228 return images.get(imageIds.get(oldIndex));
232 public Image getAlbumImage() {
233 if (albumImage == null) {
236 return Optional.fromNullable(images.get(albumImage)).or(images.values().iterator().next());
240 public boolean isEmpty() {
241 return albums.isEmpty() && images.isEmpty();
245 public boolean isRoot() {
246 return parent == null;
250 public Album getParent() {
255 public Album setParent(Album parent) {
256 this.parent = checkNotNull(parent, "parent must not be null");
261 public Album removeParent() {
267 public String getTitle() {
272 public String getDescription() {
277 public Modifier modify() throws IllegalStateException {
278 // TODO: reenable check for local Sones
279 return new Modifier() {
280 private Optional<String> title = absent();
282 private Optional<String> description = absent();
284 private Optional<String> albumImage = absent();
287 public Modifier setTitle(String title) {
288 this.title = fromNullable(title);
293 public Modifier setDescription(String description) {
294 this.description = fromNullable(description);
299 public Modifier setAlbumImage(String imageId) {
300 this.albumImage = fromNullable(imageId);
305 public Album update() throws IllegalStateException {
306 if (title.isPresent() && title.get().trim().isEmpty()) {
307 throw new AlbumTitleMustNotBeEmpty();
309 if (title.isPresent()) {
310 AlbumImpl.this.title = title.get();
312 if (description.isPresent()) {
313 AlbumImpl.this.description = description.get();
315 if (albumImage.isPresent()) {
316 AlbumImpl.this.albumImage = albumImage.get();
318 return AlbumImpl.this;
323 public static class AlbumTitleMustNotBeEmpty extends IllegalStateException { }
326 // FINGERPRINTABLE METHODS
330 public String getFingerprint() {
331 Hasher hash = Hashing.sha256().newHasher();
332 hash.putString("Album(");
333 hash.putString("ID(").putString(id).putString(")");
334 hash.putString("Title(").putString(title).putString(")");
335 hash.putString("Description(").putString(description).putString(")");
336 if (albumImage != null) {
337 hash.putString("AlbumImage(").putString(albumImage).putString(")");
340 /* add nested albums. */
341 hash.putString("Albums(");
342 for (Album album : albums) {
343 hash.putString(album.getFingerprint());
348 hash.putString("Images(");
349 for (Image image : getImages()) {
350 if (image.isInserted()) {
351 hash.putString(image.getFingerprint());
357 return hash.hash().toString();
365 public int hashCode() {
366 return id.hashCode();
370 public boolean equals(Object object) {
371 if (!(object instanceof AlbumImpl)) {
374 AlbumImpl album = (AlbumImpl) object;
375 return id.equals(album.id);