X-Git-Url: https://git.pterodactylus.net/?p=Sone.git;a=blobdiff_plain;f=src%2Fmain%2Fjava%2Fnet%2Fpterodactylus%2Fsone%2Fdata%2FAlbum.java;h=94eec8f142ae5f2df8daf0b41a9a7898c270b708;hp=d0d078b4422b72a18c4bd7caaad4dc4ff10161cd;hb=49e0bc7a5120b0f07ad19a486b6c0c18def8ca66;hpb=17da32be452acc7e110801fb04bf155bf0587224 diff --git a/src/main/java/net/pterodactylus/sone/data/Album.java b/src/main/java/net/pterodactylus/sone/data/Album.java index d0d078b..94eec8f 100644 --- a/src/main/java/net/pterodactylus/sone/data/Album.java +++ b/src/main/java/net/pterodactylus/sone/data/Album.java @@ -23,6 +23,8 @@ 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; /** @@ -41,6 +43,9 @@ 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 Map images = new LinkedHashMap(); @@ -151,7 +156,15 @@ public class Album implements Fingerprintable { * @return The images in this album */ public List getImages() { - return new ArrayList(images.values()); + return Mappers.mappedList(imageIds, new Mapper() { + + @Override + @SuppressWarnings("synthetic-access") + public Image map(String imageId) { + return images.get(imageId); + } + + }); } /** @@ -166,7 +179,11 @@ public class Album implements Fingerprintable { image.getAlbum().removeImage(image); } image.setAlbum(this); - if (!images.containsKey(image.getId())) { + if (imageIds.isEmpty()) { + albumImage = image.getId(); + } + if (!imageIds.contains(image.getId())) { + imageIds.add(image.getId()); images.put(image.getId(), image); } } @@ -179,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(); + } + } } /** @@ -196,6 +221,18 @@ public class Album implements Fingerprintable { } /** + * 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