From: David ‘Bombe’ Roden Date: Wed, 9 Oct 2013 05:17:20 +0000 (+0200) Subject: Extract album interface. X-Git-Tag: 0.8.8^2~26 X-Git-Url: https://git.pterodactylus.net/?a=commitdiff_plain;h=f313a48bfc36f9968d18b76436739c093b562b7a;p=Sone.git Extract album interface. --- diff --git a/src/main/java/net/pterodactylus/sone/core/Core.java b/src/main/java/net/pterodactylus/sone/core/Core.java index 5103b7d..a6a3316 100644 --- a/src/main/java/net/pterodactylus/sone/core/Core.java +++ b/src/main/java/net/pterodactylus/sone/core/Core.java @@ -52,6 +52,7 @@ import net.pterodactylus.sone.core.event.SoneLockedEvent; import net.pterodactylus.sone.core.event.SoneRemovedEvent; import net.pterodactylus.sone.core.event.SoneUnlockedEvent; import net.pterodactylus.sone.data.Album; +import net.pterodactylus.sone.data.AlbumImpl; import net.pterodactylus.sone.data.Client; import net.pterodactylus.sone.data.Image; import net.pterodactylus.sone.data.Post; @@ -639,7 +640,7 @@ public class Core extends AbstractService implements SoneProvider, PostProvider, synchronized (albums) { Album album = albums.get(albumId); if (create && (album == null)) { - album = new Album(albumId); + album = new AlbumImpl(albumId); albums.put(albumId, album); } return album; @@ -1603,7 +1604,7 @@ public class Core extends AbstractService implements SoneProvider, PostProvider, * @return The new album */ public Album createAlbum(Sone sone, Album parent) { - Album album = new Album(); + AlbumImpl album = new AlbumImpl(); synchronized (albums) { albums.put(album.getId(), album); } diff --git a/src/main/java/net/pterodactylus/sone/data/Album.java b/src/main/java/net/pterodactylus/sone/data/Album.java index 70478f3..1e79f89 100644 --- a/src/main/java/net/pterodactylus/sone/data/Album.java +++ b/src/main/java/net/pterodactylus/sone/data/Album.java @@ -17,40 +17,29 @@ 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 static java.util.Collections.emptyList; import java.util.ArrayList; import java.util.Collections; import java.util.Comparator; -import java.util.HashMap; import java.util.List; -import java.util.Map; -import java.util.UUID; import javax.annotation.Nonnull; 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 Identified, Fingerprintable { +public interface Album extends Identified, Fingerprintable { /** Compares two {@link Album}s by {@link #getTitle()}. */ - public static final Comparator TITLE_COMPARATOR = new Comparator() { + Comparator TITLE_COMPARATOR = new Comparator() { @Override public int compare(Album leftAlbum, Album rightAlbum) { @@ -59,7 +48,7 @@ public class Album implements Identified, Fingerprintable { }; /** Function that flattens the given album and all albums beneath it. */ - public static final Function> FLATTENER = new Function>() { + Function> FLATTENER = new Function>() { @Override @Nonnull @@ -77,7 +66,7 @@ public class Album implements Identified, Fingerprintable { }; /** Function that transforms an album into the images it contains. */ - public static final Function> IMAGES = new Function>() { + Function> IMAGES = new Function>() { @Override @Nonnull @@ -90,7 +79,7 @@ public class Album implements Identified, Fingerprintable { * Filter that removes all albums that do not have any images in any album * below it. */ - public static final Predicate NOT_EMPTY = new Predicate() { + Predicate NOT_EMPTY = new Predicate() { @Override public boolean apply(Album album) { @@ -112,308 +101,143 @@ public class Album implements Identified, Fingerprintable { } }; - /** The ID of this album. */ - private final String id; - - /** The Sone this album belongs to. */ - 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 Map images = new HashMap(); - - /** The parent album. */ - private Album parent; - - /** 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. - */ - public Album() { - this(UUID.randomUUID().toString()); - } - - /** - * Creates a new album with the given ID. - * - * @param id - * The ID of the album - */ - public Album(String id) { - this.id = checkNotNull(id, "id must not be null"); - } - - // - // ACCESSORS - // - /** * Returns the ID of this album. * * @return The ID of this album */ - public String getId() { - return id; - } + String getId(); /** * Returns the Sone this album belongs to. * * @return The Sone this album belongs to */ - public Sone getSone() { - return sone; - } + Sone getSone(); /** * 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 + * 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; - } + Album setSone(Sone sone); /** * Returns the nested albums. * * @return The nested albums */ - public List getAlbums() { - return new ArrayList(albums); - } + List getAlbums(); /** * Adds an album to this album. * * @param album - * The album to add + * The album to add */ - public void addAlbum(Album 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); - } - } + void addAlbum(Album album); /** * Removes an album from this album. * * @param album - * The album to remove + * The album to remove */ - public void removeAlbum(Album album) { - 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(); - } + void removeAlbum(Album album); /** - * Moves the given album up in this album’s albums. If the album is already - * the first album, nothing happens. + * 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 + * 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); - } + Album moveAlbumUp(Album album); /** - * Moves the given album down in this album’s albums. If the album is - * already the last album, nothing happens. + * 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 + * 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); - } + Album moveAlbumDown(Album album); /** * Returns the images in this album. * * @return The images in this album */ - public List getImages() { - 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())); - } + List getImages(); /** * Adds the given image to this album. * * @param image - * The image to add + * The image to add */ - public void addImage(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"); - 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); - } - } + void addImage(Image image); /** * Removes the given image from this album. * * @param image - * The image to remove + * The image to remove */ - public void removeImage(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"); - 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(); - } - } - } + void removeImage(Image image); /** - * Moves the given image up in this album’s images. If the image is already - * the first image, nothing happens. + * 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 + * 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)); - } + Image moveImageUp(Image image); /** - * Moves the given image down in this album’s images. If the image is - * already the last image, nothing happens. + * 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 + * 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)); - } + Image moveImageDown(Image image); /** - * Returns the album image of this album, or {@code null} if no album image - * has been set. + * 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()); - } + Image getAlbumImage(); /** * Sets the ID of the album image. * * @param id - * The ID of the album image + * The ID of the album image * @return This album */ - public Album setAlbumImage(String id) { - this.albumImage = id; - return this; - } + Album setAlbumImage(String id); /** * 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(); - } + boolean isEmpty(); /** * Returns whether this album is an identitiy’s root album. @@ -421,144 +245,62 @@ public class Album implements Identified, Fingerprintable { * @return {@code true} if this album is an identity’s root album, {@code * false} otherwise */ - public boolean isRoot() { - return parent == null; - } + boolean isRoot(); /** * Returns the parent album of this album. * - * @return The parent album of this album, or {@code null} if this album - * does not have a parent + * @return The parent album of this album, or {@code null} if this album does + * not have a parent */ - public Album getParent() { - return parent; - } + Album getParent(); /** * Sets the parent album of this album. * * @param parent - * The new parent album of this album + * The new parent album of this album * @return This album */ - protected Album setParent(Album parent) { - this.parent = checkNotNull(parent, "parent must not be null"); - return this; - } + Album setParent(Album parent); /** * Removes the parent album of this album. * * @return This album */ - protected Album removeParent() { - this.parent = null; - return this; - } + Album removeParent(); /** * Returns the title of this album. * * @return The title of this album */ - public String getTitle() { - return title; - } + String getTitle(); /** * Sets the title of this album. * * @param title - * The title of this album + * The title of this album * @return This album */ - public Album setTitle(String title) { - this.title = checkNotNull(title, "title must not be null"); - return this; - } + Album setTitle(String title); /** * Returns the description of this album. * * @return The description of this album */ - public String getDescription() { - return description; - } + String getDescription(); /** * Sets the description of this album. * * @param description - * The description of this album + * The description of this album * @return This album */ - public Album setDescription(String description) { - this.description = checkNotNull(description, "description must not be null"); - return this; - } - - // - // FINGERPRINTABLE METHODS - // - - /** - * {@inheritDoc} - */ - @Override - public String getFingerprint() { - 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. */ - hash.putString("Albums("); - for (Album album : albums) { - hash.putString(album.getFingerprint()); - } - hash.putString(")"); - - /* add images. */ - hash.putString("Images("); - for (Image image : getImages()) { - if (image.isInserted()) { - hash.putString(image.getFingerprint()); - } - } - hash.putString(")"); - - hash.putString(")"); - return hash.hash().toString(); - } - - // - // OBJECT METHODS - // - - /** - * {@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 id.equals(album.id); - } + Album setDescription(String description); } diff --git a/src/main/java/net/pterodactylus/sone/data/AlbumImpl.java b/src/main/java/net/pterodactylus/sone/data/AlbumImpl.java new file mode 100644 index 0000000..56c44aa --- /dev/null +++ b/src/main/java/net/pterodactylus/sone/data/AlbumImpl.java @@ -0,0 +1,352 @@ +/* + * 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 + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ + +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 java.util.ArrayList; +import java.util.HashMap; +import java.util.List; +import java.util.Map; +import java.util.UUID; + +import com.google.common.base.Function; +import com.google.common.base.Optional; +import com.google.common.base.Predicates; +import com.google.common.collect.Collections2; +import com.google.common.hash.Hasher; +import com.google.common.hash.Hashing; + +/** + * Container for images that can also contain nested {@link AlbumImpl}s. + * + * @author David ‘Bombe’ Roden + */ +public class AlbumImpl implements Album { + + /** The ID of this album. */ + private final String id; + + /** The Sone this album belongs to. */ + 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 Map images = new HashMap(); + + /** The parent album. */ + private Album parent; + + /** 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. */ + public AlbumImpl() { + this(UUID.randomUUID().toString()); + } + + /** + * Creates a new album with the given ID. + * + * @param id + * The ID of the album + */ + public AlbumImpl(String id) { + this.id = checkNotNull(id, "id must not be null"); + } + + // + // ACCESSORS + // + + @Override + public String getId() { + return id; + } + + @Override + public Sone getSone() { + return sone; + } + + @Override + 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; + } + + @Override + public List getAlbums() { + return new ArrayList(albums); + } + + @Override + public void addAlbum(Album 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); + } + } + + @Override + public void removeAlbum(Album album) { + checkNotNull(album, "album must not be null"); + checkArgument(album.getSone().equals(sone), "album must belong this album’s Sone"); + checkArgument(equals(album.getParent()), "album must belong to this album"); + albums.remove(album); + album.removeParent(); + } + + @Override + public Album moveAlbumUp(Album album) { + checkNotNull(album, "album must not be null"); + checkArgument(album.getSone().equals(sone), "album must belong to the same Sone as this album"); + checkArgument(equals(album.getParent()), "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); + } + + @Override + public Album moveAlbumDown(Album album) { + checkNotNull(album, "album must not be null"); + checkArgument(album.getSone().equals(sone), "album must belong to the same Sone as this album"); + checkArgument(equals(album.getParent()), "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); + } + + @Override + public List getImages() { + 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())); + } + + @Override + public void addImage(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"); + 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); + } + } + + @Override + public void removeImage(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"); + 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(); + } + } + } + + @Override + 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)); + } + + @Override + 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)); + } + + @Override + public Image getAlbumImage() { + if (albumImage == null) { + return null; + } + return Optional.fromNullable(images.get(albumImage)).or(images.values().iterator().next()); + } + + @Override + public AlbumImpl setAlbumImage(String id) { + this.albumImage = id; + return this; + } + + @Override + public boolean isEmpty() { + return albums.isEmpty() && images.isEmpty(); + } + + @Override + public boolean isRoot() { + return parent == null; + } + + @Override + public Album getParent() { + return parent; + } + + @Override + public Album setParent(Album parent) { + this.parent = checkNotNull(parent, "parent must not be null"); + return this; + } + + @Override + public Album removeParent() { + this.parent = null; + return this; + } + + @Override + public String getTitle() { + return title; + } + + @Override + public Album setTitle(String title) { + this.title = checkNotNull(title, "title must not be null"); + return this; + } + + @Override + public String getDescription() { + return description; + } + + @Override + public AlbumImpl setDescription(String description) { + this.description = checkNotNull(description, "description must not be null"); + return this; + } + + // + // FINGERPRINTABLE METHODS + // + + @Override + public String getFingerprint() { + 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. */ + hash.putString("Albums("); + for (Album album : albums) { + hash.putString(album.getFingerprint()); + } + hash.putString(")"); + + /* add images. */ + hash.putString("Images("); + for (Image image : getImages()) { + if (image.isInserted()) { + hash.putString(image.getFingerprint()); + } + } + hash.putString(")"); + + hash.putString(")"); + return hash.hash().toString(); + } + + // + // OBJECT METHODS + // + + @Override + public int hashCode() { + return id.hashCode(); + } + + @Override + public boolean equals(Object object) { + if (!(object instanceof AlbumImpl)) { + return false; + } + AlbumImpl album = (AlbumImpl) object; + return id.equals(album.id); + } + +} diff --git a/src/main/java/net/pterodactylus/sone/data/Sone.java b/src/main/java/net/pterodactylus/sone/data/Sone.java index 31153ff..1279f75 100644 --- a/src/main/java/net/pterodactylus/sone/data/Sone.java +++ b/src/main/java/net/pterodactylus/sone/data/Sone.java @@ -231,7 +231,7 @@ public class Sone implements Identified, Fingerprintable, Comparable { private final Set likedReplyIds = new CopyOnWriteArraySet(); /** The root album containing all albums. */ - private final Album rootAlbum = new Album().setSone(this); + private final Album rootAlbum = new AlbumImpl().setSone(this); /** Sone-specific options. */ private Options options = new Options();