From 49e0bc7a5120b0f07ad19a486b6c0c18def8ca66 Mon Sep 17 00:00:00 2001 From: =?utf8?q?David=20=E2=80=98Bombe=E2=80=99=20Roden?= Date: Fri, 23 Sep 2011 11:00:58 +0200 Subject: [PATCH] Store order of images separately. --- .../java/net/pterodactylus/sone/data/Album.java | 23 ++++++++++++++++++---- 1 file changed, 19 insertions(+), 4 deletions(-) diff --git a/src/main/java/net/pterodactylus/sone/data/Album.java b/src/main/java/net/pterodactylus/sone/data/Album.java index 89823a8..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,10 +179,11 @@ public class Album implements Fingerprintable { image.getAlbum().removeImage(image); } image.setAlbum(this); - if (images.isEmpty()) { + if (imageIds.isEmpty()) { albumImage = image.getId(); } - if (!images.containsKey(image.getId())) { + if (!imageIds.contains(image.getId())) { + imageIds.add(image.getId()); images.put(image.getId(), image); } } @@ -182,7 +196,8 @@ 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; -- 2.7.4