From eb4aded6a09159eadb8999ff652466950ea38615 Mon Sep 17 00:00:00 2001 From: =?utf8?q?David=20=E2=80=98Bombe=E2=80=99=20Roden?= Date: Wed, 13 Apr 2011 07:34:33 +0200 Subject: [PATCH] Move album flattening algorithm to Sone. --- .../java/net/pterodactylus/sone/core/Core.java | 11 +-------- .../java/net/pterodactylus/sone/data/Sone.java | 27 ++++++++++++++++++++++ 2 files changed, 28 insertions(+), 10 deletions(-) diff --git a/src/main/java/net/pterodactylus/sone/core/Core.java b/src/main/java/net/pterodactylus/sone/core/Core.java index a67177b..89de6fa 100644 --- a/src/main/java/net/pterodactylus/sone/core/Core.java +++ b/src/main/java/net/pterodactylus/sone/core/Core.java @@ -1548,16 +1548,7 @@ public class Core implements IdentityListener, UpdateListener, ImageInsertListen configuration.getStringValue(sonePrefix + "/Friends/" + friendCounter + "/ID").setValue(null); /* save albums. first, collect in a flat structure, top-level first. */ - List albums = new ArrayList(); - albums.addAll(sone.getAlbums()); - int lastAlbumIndex = 0; - while (lastAlbumIndex < albums.size()) { - int previousAlbumCount = albums.size(); - for (Album album : new ArrayList(albums.subList(lastAlbumIndex, albums.size()))) { - albums.addAll(album.getAlbums()); - } - lastAlbumIndex = previousAlbumCount; - } + List albums = Sone.flattenAlbums(sone.getAlbums()); int albumCounter = 0; for (Album album : albums) { diff --git a/src/main/java/net/pterodactylus/sone/data/Sone.java b/src/main/java/net/pterodactylus/sone/data/Sone.java index aa4a155..4467eba 100644 --- a/src/main/java/net/pterodactylus/sone/data/Sone.java +++ b/src/main/java/net/pterodactylus/sone/data/Sone.java @@ -706,6 +706,33 @@ public class Sone implements Fingerprintable, Comparable { } // + // STATIC METHODS + // + + /** + * Flattens the given top-level albums so that the resulting list contains + * parent albums before child albums and the resulting list can be parsed in + * a single pass. + * + * @param albums + * The albums to flatten + * @return The flattened albums + */ + public static List flattenAlbums(Collection albums) { + List flatAlbums = new ArrayList(); + flatAlbums.addAll(albums); + int lastAlbumIndex = 0; + while (lastAlbumIndex < flatAlbums.size()) { + int previousAlbumCount = flatAlbums.size(); + for (Album album : new ArrayList(flatAlbums.subList(lastAlbumIndex, flatAlbums.size()))) { + flatAlbums.addAll(album.getAlbums()); + } + lastAlbumIndex = previousAlbumCount; + } + return flatAlbums; + } + + // // INTERFACE Comparable // -- 2.7.4