X-Git-Url: https://git.pterodactylus.net/?p=Sone.git;a=blobdiff_plain;f=src%2Fmain%2Fjava%2Fnet%2Fpterodactylus%2Fsone%2Fdata%2FAlbum.java;h=7d10e72253c0af1331665c62ec3a8db34b0fe930;hp=20862a4647300e904d04995abbde7d244772ba16;hb=6abf2d1fb158459383a7b9e983c1b730323ff759;hpb=356a84e5ec2bb0936b7d1cdc635f7f051788da6a diff --git a/src/main/java/net/pterodactylus/sone/data/Album.java b/src/main/java/net/pterodactylus/sone/data/Album.java index 20862a4..7d10e72 100644 --- a/src/main/java/net/pterodactylus/sone/data/Album.java +++ b/src/main/java/net/pterodactylus/sone/data/Album.java @@ -18,9 +18,13 @@ package net.pterodactylus.sone.data; import java.util.ArrayList; +import java.util.HashMap; import java.util.List; +import java.util.Map; import java.util.UUID; +import net.pterodactylus.util.collection.Mapper; +import net.pterodactylus.util.collection.Mappers; import net.pterodactylus.util.validation.Validation; /** @@ -39,8 +43,11 @@ public class Album implements Fingerprintable { /** 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; @@ -51,8 +58,8 @@ public class Album implements Fingerprintable { /** The description of this album. */ private String description; - /** The index of the album picture. */ - private int albumImage = -1; + /** The ID of the album picture. */ + private String albumImage; /** * Creates a new album with a random ID. @@ -149,7 +156,15 @@ public class Album implements Fingerprintable { * @return The images in this album */ public List getImages() { - return new ArrayList(images); + return Mappers.mappedList(imageIds, new Mapper() { + + @Override + @SuppressWarnings("synthetic-access") + public Image map(String imageId) { + return images.get(imageId); + } + + }); } /** @@ -164,8 +179,12 @@ public class Album implements Fingerprintable { image.getAlbum().removeImage(image); } image.setAlbum(this); - if (!images.contains(image)) { - images.add(image); + if (imageIds.isEmpty()) { + albumImage = image.getId(); + } + if (!imageIds.contains(image.getId())) { + imageIds.add(image.getId()); + images.put(image.getId(), image); } } @@ -177,7 +196,15 @@ public class Album implements Fingerprintable { */ public void removeImage(Image image) { Validation.begin().isNotNull("Image", image).check().isEqual("Image Owner", image.getSone(), sone).check(); - images.remove(image); + 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(); + } + } } /** @@ -187,13 +214,25 @@ public class Album implements Fingerprintable { * @return The image to show when this album is listed */ public Image getAlbumImage() { - if (albumImage == -1) { + if (albumImage == null) { return null; } return images.get(albumImage); } /** + * 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 @@ -303,7 +342,7 @@ public class Album implements Fingerprintable { /* add images. */ fingerprint.append("Images("); - for (Image image : images) { + for (Image image : images.values()) { if (image.isInserted()) { fingerprint.append(image.getFingerprint()); }