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.Preconditions.checkArgument;
21 import static com.google.common.base.Preconditions.checkNotNull;
22 import static com.google.common.base.Preconditions.checkState;
24 import java.util.ArrayList;
25 import java.util.Comparator;
26 import java.util.HashMap;
27 import java.util.List;
29 import java.util.UUID;
31 import com.google.common.base.Function;
32 import com.google.common.base.Optional;
33 import com.google.common.base.Predicates;
34 import com.google.common.collect.Collections2;
35 import com.google.common.hash.Hasher;
36 import com.google.common.hash.Hashing;
39 * Container for images that can also contain nested {@link Album}s.
41 * @author <a href="mailto:bombe@pterodactylus.net">David ‘Bombe’ Roden</a>
43 public class Album implements Fingerprintable {
45 /** Compares two {@link Album}s by {@link #getTitle()}. */
46 public static final Comparator<Album> TITLE_COMPARATOR = new Comparator<Album>() {
49 public int compare(Album leftAlbum, Album rightAlbum) {
50 return leftAlbum.getTitle().compareToIgnoreCase(rightAlbum.getTitle());
54 /** The ID of this album. */
55 private final String id;
57 /** The Sone this album belongs to. */
61 private final List<Album> albums = new ArrayList<Album>();
63 /** The image IDs in order. */
64 private final List<String> imageIds = new ArrayList<String>();
66 /** The images in this album. */
67 private final Map<String, Image> images = new HashMap<String, Image>();
69 /** The parent album. */
72 /** The title of this album. */
75 /** The description of this album. */
76 private String description;
78 /** The ID of the album picture. */
79 private String albumImage;
82 * Creates a new album with a random ID.
85 this(UUID.randomUUID().toString());
89 * Creates a new album with the given ID.
94 public Album(String id) {
95 this.id = checkNotNull(id, "id must not be null");
103 * Returns the ID of this album.
105 * @return The ID of this album
107 public String getId() {
112 * Returns the Sone this album belongs to.
114 * @return The Sone this album belongs to
116 public Sone getSone() {
121 * Sets the owner of the album. The owner can only be set as long as the
122 * current owner is {@code null}.
128 public Album setSone(Sone sone) {
129 checkNotNull(sone, "sone must not be null");
130 checkState((this.sone == null) || (this.sone.equals(sone)), "album owner must not already be set to some other Sone");
136 * Returns the nested albums.
138 * @return The nested albums
140 public List<Album> getAlbums() {
141 return new ArrayList<Album>(albums);
145 * Adds an album to this album.
150 public void addAlbum(Album album) {
151 checkNotNull(album, "album must not be null");
152 checkArgument(album.getSone().equals(sone), "album must belong to the same Sone as this album");
153 checkState((this.parent == null) || (this.parent.equals(album.parent)), "album must not already be set to some other Sone");
154 album.setParent(this);
155 if (!albums.contains(album)) {
161 * Removes an album from this album.
164 * The album to remove
166 public void removeAlbum(Album album) {
167 checkNotNull(album, "album must not be null");
168 checkArgument(album.sone.equals(sone), "album must belong this album’s Sone");
169 checkArgument(equals(album.parent), "album must belong to this album");
170 albums.remove(album);
171 album.removeParent();
175 * Moves the given album up in this album’s albums. If the album is already
176 * the first album, nothing happens.
179 * The album to move up
180 * @return The album that the given album swapped the place with, or
181 * <code>null</code> if the album did not change its place
183 public Album moveAlbumUp(Album album) {
184 checkNotNull(album, "album must not be null");
185 checkArgument(album.sone.equals(sone), "album must belong to the same Sone as this album");
186 checkArgument(equals(album.parent), "album must belong to this album");
187 int oldIndex = albums.indexOf(album);
191 albums.remove(oldIndex);
192 albums.add(oldIndex - 1, album);
193 return albums.get(oldIndex);
197 * Moves the given album down in this album’s albums. If the album is
198 * already the last album, nothing happens.
201 * The album to move down
202 * @return The album that the given album swapped the place with, or
203 * <code>null</code> if the album did not change its place
205 public Album moveAlbumDown(Album album) {
206 checkNotNull(album, "album must not be null");
207 checkArgument(album.sone.equals(sone), "album must belong to the same Sone as this album");
208 checkArgument(equals(album.parent), "album must belong to this album");
209 int oldIndex = albums.indexOf(album);
210 if ((oldIndex < 0) || (oldIndex >= (albums.size() - 1))) {
213 albums.remove(oldIndex);
214 albums.add(oldIndex + 1, album);
215 return albums.get(oldIndex);
219 * Returns the images in this album.
221 * @return The images in this album
223 public List<Image> getImages() {
224 return new ArrayList<Image>(Collections2.filter(Collections2.transform(imageIds, new Function<String, Image>() {
227 @SuppressWarnings("synthetic-access")
228 public Image apply(String imageId) {
229 return images.get(imageId);
231 }), Predicates.notNull()));
235 * Adds the given image to this album.
240 public void addImage(Image image) {
241 checkNotNull(image, "image must not be null");
242 checkNotNull(image.getSone(), "image must have an owner");
243 checkArgument(image.getSone().equals(sone), "image must belong to the same Sone as this album");
244 if (image.getAlbum() != null) {
245 image.getAlbum().removeImage(image);
247 image.setAlbum(this);
248 if (imageIds.isEmpty() && (albumImage == null)) {
249 albumImage = image.getId();
251 if (!imageIds.contains(image.getId())) {
252 imageIds.add(image.getId());
253 images.put(image.getId(), image);
258 * Removes the given image from this album.
261 * The image to remove
263 public void removeImage(Image image) {
264 checkNotNull(image, "image must not be null");
265 checkNotNull(image.getSone(), "image must have an owner");
266 checkArgument(image.getSone().equals(sone), "image must belong to the same Sone as this album");
267 imageIds.remove(image.getId());
268 images.remove(image.getId());
269 if (image.getId().equals(albumImage)) {
270 if (images.isEmpty()) {
273 albumImage = images.values().iterator().next().getId();
279 * Moves the given image up in this album’s images. If the image is already
280 * the first image, nothing happens.
283 * The image to move up
284 * @return The image that the given image swapped the place with, or
285 * <code>null</code> if the image did not change its place
287 public Image moveImageUp(Image image) {
288 checkNotNull(image, "image must not be null");
289 checkNotNull(image.getSone(), "image must have an owner");
290 checkArgument(image.getSone().equals(sone), "image must belong to the same Sone as this album");
291 checkArgument(image.getAlbum().equals(this), "image must belong to this album");
292 int oldIndex = imageIds.indexOf(image.getId());
296 imageIds.remove(image.getId());
297 imageIds.add(oldIndex - 1, image.getId());
298 return images.get(imageIds.get(oldIndex));
302 * Moves the given image down in this album’s images. If the image is
303 * already the last image, nothing happens.
306 * The image to move down
307 * @return The image that the given image swapped the place with, or
308 * <code>null</code> if the image did not change its place
310 public Image moveImageDown(Image image) {
311 checkNotNull(image, "image must not be null");
312 checkNotNull(image.getSone(), "image must have an owner");
313 checkArgument(image.getSone().equals(sone), "image must belong to the same Sone as this album");
314 checkArgument(image.getAlbum().equals(this), "image must belong to this album");
315 int oldIndex = imageIds.indexOf(image.getId());
316 if ((oldIndex == -1) || (oldIndex >= (imageIds.size() - 1))) {
319 imageIds.remove(image.getId());
320 imageIds.add(oldIndex + 1, image.getId());
321 return images.get(imageIds.get(oldIndex));
325 * Returns the album image of this album, or {@code null} if no album image
328 * @return The image to show when this album is listed
330 public Image getAlbumImage() {
331 if (albumImage == null) {
334 return Optional.fromNullable(images.get(albumImage)).or(images.values().iterator().next());
338 * Sets the ID of the album image.
341 * The ID of the album image
344 public Album setAlbumImage(String id) {
345 this.albumImage = id;
350 * Returns whether this album contains any other albums or images.
352 * @return {@code true} if this album is empty, {@code false} otherwise
354 public boolean isEmpty() {
355 return albums.isEmpty() && images.isEmpty();
359 * Returns the parent album of this album.
361 * @return The parent album of this album, or {@code null} if this album
362 * does not have a parent
364 public Album getParent() {
369 * Sets the parent album of this album.
372 * The new parent album of this album
375 protected Album setParent(Album parent) {
376 this.parent = checkNotNull(parent, "parent must not be null");
381 * Removes the parent album of this album.
385 protected Album removeParent() {
391 * Returns the title of this album.
393 * @return The title of this album
395 public String getTitle() {
400 * Sets the title of this album.
403 * The title of this album
406 public Album setTitle(String title) {
407 this.title = checkNotNull(title, "title must not be null");
412 * Returns the description of this album.
414 * @return The description of this album
416 public String getDescription() {
421 * Sets the description of this album.
424 * The description of this album
427 public Album setDescription(String description) {
428 this.description = checkNotNull(description, "description must not be null");
433 // FINGERPRINTABLE METHODS
440 public String getFingerprint() {
441 Hasher hash = Hashing.sha256().newHasher();
442 hash.putString("Album(");
443 hash.putString("ID(").putString(id).putString(")");
444 hash.putString("Title(").putString(title).putString(")");
445 hash.putString("Description(").putString(description).putString(")");
446 if (albumImage != null) {
447 hash.putString("AlbumImage(").putString(albumImage).putString(")");
450 /* add nested albums. */
451 hash.putString("Albums(");
452 for (Album album : albums) {
453 hash.putString(album.getFingerprint());
458 hash.putString("Images(");
459 for (Image image : getImages()) {
460 if (image.isInserted()) {
461 hash.putString(image.getFingerprint());
467 return hash.hash().toString();
478 public int hashCode() {
479 return id.hashCode();
486 public boolean equals(Object object) {
487 if (!(object instanceof Album)) {
490 Album album = (Album) object;
491 return id.equals(album.id);