X-Git-Url: https://git.pterodactylus.net/?p=Sone.git;a=blobdiff_plain;f=src%2Fmain%2Fjava%2Fnet%2Fpterodactylus%2Fsone%2Fdata%2FAlbum.java;h=8238b3fc3391acc55ae1ff62fa8fdc633957d881;hp=76015c82bab446a0b72e9e2593517278bf407470;hb=afdb3c077f9f762f85be2656015609179eff8510;hpb=f38f21ad0a7703d84f826b01c4e107e4c7ba5fe6 diff --git a/src/main/java/net/pterodactylus/sone/data/Album.java b/src/main/java/net/pterodactylus/sone/data/Album.java index 76015c8..8238b3f 100644 --- a/src/main/java/net/pterodactylus/sone/data/Album.java +++ b/src/main/java/net/pterodactylus/sone/data/Album.java @@ -1,5 +1,5 @@ /* - * Sone - Album.java - Copyright © 2011 David Roden + * Sone - Album.java - Copyright © 2011–2013 David Roden * * This program is free software: you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by @@ -17,48 +17,125 @@ package net.pterodactylus.sone.data; +import static com.google.common.base.Preconditions.checkArgument; +import static com.google.common.base.Preconditions.checkNotNull; +import static com.google.common.base.Preconditions.checkState; +import static java.util.Arrays.asList; + import java.util.ArrayList; +import java.util.Comparator; +import java.util.HashMap; import java.util.List; +import java.util.Map; import java.util.UUID; -import net.pterodactylus.util.validation.Validation; +import com.google.common.base.Function; +import com.google.common.base.Optional; +import com.google.common.base.Predicate; +import com.google.common.base.Predicates; +import com.google.common.collect.Collections2; +import com.google.common.collect.FluentIterable; +import com.google.common.collect.ImmutableList; +import com.google.common.hash.Hasher; +import com.google.common.hash.Hashing; /** * Container for images that can also contain nested {@link Album}s. * * @author David ‘Bombe’ Roden */ -public class Album implements Fingerprintable { +public class Album implements Identified, Fingerprintable { + + /** Compares two {@link Album}s by {@link #getTitle()}. */ + public static final Comparator TITLE_COMPARATOR = new Comparator() { + + @Override + public int compare(Album leftAlbum, Album rightAlbum) { + return leftAlbum.getTitle().compareToIgnoreCase(rightAlbum.getTitle()); + } + }; + + /** Function that flattens the given album and all albums beneath it. */ + public static final Function> FLATTENER = new Function>() { + + @Override + public List apply(Album album) { + List albums = new ArrayList(); + albums.add(album); + for (Album subAlbum : album.getAlbums()) { + albums.addAll(FluentIterable.from(ImmutableList.of(subAlbum)).transformAndConcat(FLATTENER).toList()); + } + return albums; + } + }; + + /** Function that transforms an album into the images it contains. */ + public static final Function> IMAGES = new Function>() { + + @Override + public List apply(Album album) { + return album.getImages(); + } + }; + + /** + * Filter that removes all albums that do not have any images in any album + * below it. + */ + public static final Predicate NOT_EMPTY = new Predicate() { + + @Override + public boolean apply(Album album) { + /* so, we flatten all albums below the given one and check whether at least one album… */ + return FluentIterable.from(asList(album)).transformAndConcat(FLATTENER).anyMatch(new Predicate() { + + @Override + public boolean apply(Album album) { + /* …contains any inserted images. */ + return !album.getImages().isEmpty() && FluentIterable.from(album.getImages()).allMatch(new Predicate() { + + @Override + public boolean apply(Image input) { + return input.isInserted(); + } + }); + } + }); + } + }; /** The ID of this album. */ private final String id; /** The Sone this album belongs to. */ - private final Sone sone; + private Sone sone; /** Nested albums. */ private final List albums = new ArrayList(); + /** The image IDs in order. */ + private final List imageIds = new ArrayList(); + /** The images in this album. */ - private final List images = new ArrayList(); + private final Map images = new HashMap(); /** The parent album. */ private Album parent; - /** The name of this album. */ - private String name; + /** The title of this album. */ + private String title; /** The description of this album. */ private String description; + /** The ID of the album picture. */ + private String albumImage; + /** * Creates a new album with a random ID. - * - * @param sone - * The Sone this album belongs to */ - public Album(Sone sone) { - this(UUID.randomUUID().toString(), sone); + public Album() { + this(UUID.randomUUID().toString()); } /** @@ -66,13 +143,9 @@ public class Album implements Fingerprintable { * * @param id * The ID of the album - * @param sone - * The Sone this album belongs to */ - public Album(String id, Sone sone) { - Validation.begin().isNotNull("Album ID", id).isNotNull("Album Owner", sone).check(); - this.id = id; - this.sone = sone; + public Album(String id) { + this.id = checkNotNull(id, "id must not be null"); } // @@ -98,11 +171,26 @@ public class Album implements Fingerprintable { } /** + * Sets the owner of the album. The owner can only be set as long as the + * current owner is {@code null}. + * + * @param sone + * The album owner + * @return This album + */ + public Album setSone(Sone sone) { + checkNotNull(sone, "sone must not be null"); + checkState((this.sone == null) || (this.sone.equals(sone)), "album owner must not already be set to some other Sone"); + this.sone = sone; + return this; + } + + /** * Returns the nested albums. * * @return The nested albums */ - public List getNestedAlbums() { + public List getAlbums() { return new ArrayList(albums); } @@ -113,9 +201,12 @@ public class Album implements Fingerprintable { * The album to add */ public void addAlbum(Album album) { - Validation.begin().isNotNull("Album", album).check().isEqual("Album Owner", album.sone, sone).isNull("Album Parent", album.parent).check(); - albums.add(album); + checkNotNull(album, "album must not be null"); + checkArgument(album.getSone().equals(sone), "album must belong to the same Sone as this album"); album.setParent(this); + if (!albums.contains(album)) { + albums.add(album); + } } /** @@ -125,18 +216,71 @@ public class Album implements Fingerprintable { * The album to remove */ public void removeAlbum(Album album) { - Validation.begin().isNotNull("Album", album).check().isEqual("Album Owner", album.sone, sone).isEqual("Album Parent", album.parent, this).check(); + checkNotNull(album, "album must not be null"); + checkArgument(album.sone.equals(sone), "album must belong this album’s Sone"); + checkArgument(equals(album.parent), "album must belong to this album"); albums.remove(album); album.removeParent(); } /** + * Moves the given album up in this album’s albums. If the album is already + * the first album, nothing happens. + * + * @param album + * The album to move up + * @return The album that the given album swapped the place with, or + * null if the album did not change its place + */ + public Album moveAlbumUp(Album album) { + checkNotNull(album, "album must not be null"); + checkArgument(album.sone.equals(sone), "album must belong to the same Sone as this album"); + checkArgument(equals(album.parent), "album must belong to this album"); + int oldIndex = albums.indexOf(album); + if (oldIndex <= 0) { + return null; + } + albums.remove(oldIndex); + albums.add(oldIndex - 1, album); + return albums.get(oldIndex); + } + + /** + * Moves the given album down in this album’s albums. If the album is + * already the last album, nothing happens. + * + * @param album + * The album to move down + * @return The album that the given album swapped the place with, or + * null if the album did not change its place + */ + public Album moveAlbumDown(Album album) { + checkNotNull(album, "album must not be null"); + checkArgument(album.sone.equals(sone), "album must belong to the same Sone as this album"); + checkArgument(equals(album.parent), "album must belong to this album"); + int oldIndex = albums.indexOf(album); + if ((oldIndex < 0) || (oldIndex >= (albums.size() - 1))) { + return null; + } + albums.remove(oldIndex); + albums.add(oldIndex + 1, album); + return albums.get(oldIndex); + } + + /** * Returns the images in this album. * * @return The images in this album */ public List getImages() { - return new ArrayList(images); + return new ArrayList(Collections2.filter(Collections2.transform(imageIds, new Function() { + + @Override + @SuppressWarnings("synthetic-access") + public Image apply(String imageId) { + return images.get(imageId); + } + }), Predicates.notNull())); } /** @@ -146,8 +290,20 @@ public class Album implements Fingerprintable { * The image to add */ public void addImage(Image image) { - Validation.begin().isNotNull("Image", image).check().isEqual("Image Owner", image.getSone(), sone).check(); - images.add(image); + checkNotNull(image, "image must not be null"); + checkNotNull(image.getSone(), "image must have an owner"); + checkArgument(image.getSone().equals(sone), "image must belong to the same Sone as this album"); + if (image.getAlbum() != null) { + image.getAlbum().removeImage(image); + } + image.setAlbum(this); + if (imageIds.isEmpty() && (albumImage == null)) { + albumImage = image.getId(); + } + if (!imageIds.contains(image.getId())) { + imageIds.add(image.getId()); + images.put(image.getId(), image); + } } /** @@ -157,8 +313,108 @@ public class Album implements Fingerprintable { * The image to remove */ public void removeImage(Image image) { - Validation.begin().isNotNull("Image", image).check().isEqual("Image Owner", image.getSone(), sone).check(); - images.remove(image); + checkNotNull(image, "image must not be null"); + checkNotNull(image.getSone(), "image must have an owner"); + checkArgument(image.getSone().equals(sone), "image must belong to the same Sone as this album"); + imageIds.remove(image.getId()); + images.remove(image.getId()); + if (image.getId().equals(albumImage)) { + if (images.isEmpty()) { + albumImage = null; + } else { + albumImage = images.values().iterator().next().getId(); + } + } + } + + /** + * Moves the given image up in this album’s images. If the image is already + * the first image, nothing happens. + * + * @param image + * The image to move up + * @return The image that the given image swapped the place with, or + * null if the image did not change its place + */ + public Image moveImageUp(Image image) { + checkNotNull(image, "image must not be null"); + checkNotNull(image.getSone(), "image must have an owner"); + checkArgument(image.getSone().equals(sone), "image must belong to the same Sone as this album"); + checkArgument(image.getAlbum().equals(this), "image must belong to this album"); + int oldIndex = imageIds.indexOf(image.getId()); + if (oldIndex <= 0) { + return null; + } + imageIds.remove(image.getId()); + imageIds.add(oldIndex - 1, image.getId()); + return images.get(imageIds.get(oldIndex)); + } + + /** + * Moves the given image down in this album’s images. If the image is + * already the last image, nothing happens. + * + * @param image + * The image to move down + * @return The image that the given image swapped the place with, or + * null if the image did not change its place + */ + public Image moveImageDown(Image image) { + checkNotNull(image, "image must not be null"); + checkNotNull(image.getSone(), "image must have an owner"); + checkArgument(image.getSone().equals(sone), "image must belong to the same Sone as this album"); + checkArgument(image.getAlbum().equals(this), "image must belong to this album"); + int oldIndex = imageIds.indexOf(image.getId()); + if ((oldIndex == -1) || (oldIndex >= (imageIds.size() - 1))) { + return null; + } + imageIds.remove(image.getId()); + imageIds.add(oldIndex + 1, image.getId()); + return images.get(imageIds.get(oldIndex)); + } + + /** + * Returns the album image of this album, or {@code null} if no album image + * has been set. + * + * @return The image to show when this album is listed + */ + public Image getAlbumImage() { + if (albumImage == null) { + return null; + } + return Optional.fromNullable(images.get(albumImage)).or(images.values().iterator().next()); + } + + /** + * Sets the ID of the album image. + * + * @param id + * The ID of the album image + * @return This album + */ + public Album setAlbumImage(String id) { + this.albumImage = id; + return this; + } + + /** + * Returns whether this album contains any other albums or images. + * + * @return {@code true} if this album is empty, {@code false} otherwise + */ + public boolean isEmpty() { + return albums.isEmpty() && images.isEmpty(); + } + + /** + * Returns whether this album is an identitiy’s root album. + * + * @return {@code true} if this album is an identity’s root album, {@code + * false} otherwise + */ + public boolean isRoot() { + return parent == null; } /** @@ -179,8 +435,7 @@ public class Album implements Fingerprintable { * @return This album */ protected Album setParent(Album parent) { - Validation.begin().isNotNull("Album Parent", parent).check(); - this.parent = parent; + this.parent = checkNotNull(parent, "parent must not be null"); return this; } @@ -190,30 +445,28 @@ public class Album implements Fingerprintable { * @return This album */ protected Album removeParent() { - Validation.begin().isNotNull("Album Parent", parent).check(); this.parent = null; return this; } /** - * Returns the name of this album. + * Returns the title of this album. * - * @return The name of this album + * @return The title of this album */ - public String getName() { - return name; + public String getTitle() { + return title; } /** - * Sets the name of this album. + * Sets the title of this album. * - * @param name - * The name of this album + * @param title + * The title of this album * @return This album */ - public Album setName(String name) { - Validation.begin().isNotNull("Album Name", name).check(); - this.name = name; + public Album setTitle(String title) { + this.title = checkNotNull(title, "title must not be null"); return this; } @@ -234,8 +487,7 @@ public class Album implements Fingerprintable { * @return This album */ public Album setDescription(String description) { - Validation.begin().isNotNull("Album Description", description).check(); - this.description = description; + this.description = checkNotNull(description, "description must not be null"); return this; } @@ -248,28 +500,33 @@ public class Album implements Fingerprintable { */ @Override public String getFingerprint() { - StringBuilder fingerprint = new StringBuilder(); - fingerprint.append("Album("); - fingerprint.append("ID(").append(id).append(')'); - fingerprint.append("Name(").append(name).append(')'); - fingerprint.append("Description(").append(description).append(')'); + Hasher hash = Hashing.sha256().newHasher(); + hash.putString("Album("); + hash.putString("ID(").putString(id).putString(")"); + hash.putString("Title(").putString(title).putString(")"); + hash.putString("Description(").putString(description).putString(")"); + if (albumImage != null) { + hash.putString("AlbumImage(").putString(albumImage).putString(")"); + } /* add nested albums. */ - fingerprint.append("Albums("); + hash.putString("Albums("); for (Album album : albums) { - fingerprint.append(album.getFingerprint()); + hash.putString(album.getFingerprint()); } - fingerprint.append(')'); + hash.putString(")"); /* add images. */ - fingerprint.append("Images("); - for (Image image : images) { - fingerprint.append(image.getFingerprint()); + hash.putString("Images("); + for (Image image : getImages()) { + if (image.isInserted()) { + hash.putString(image.getFingerprint()); + } } - fingerprint.append(')'); + hash.putString(")"); - fingerprint.append(')'); - return fingerprint.toString(); + hash.putString(")"); + return hash.hash().toString(); } // @@ -280,12 +537,20 @@ public class Album implements Fingerprintable { * {@inheritDoc} */ @Override + public int hashCode() { + return id.hashCode(); + } + + /** + * {@inheritDoc} + */ + @Override public boolean equals(Object object) { if (!(object instanceof Album)) { return false; } Album album = (Album) object; - return sone.equals(album.sone) && id.equals(album.id); + return id.equals(album.id); } }