Move album flattening algorithm to Sone.
authorDavid ‘Bombe’ Roden <bombe@pterodactylus.net>
Wed, 13 Apr 2011 05:34:33 +0000 (07:34 +0200)
committerDavid ‘Bombe’ Roden <bombe@pterodactylus.net>
Wed, 13 Apr 2011 05:34:33 +0000 (07:34 +0200)
src/main/java/net/pterodactylus/sone/core/Core.java
src/main/java/net/pterodactylus/sone/data/Sone.java

index a67177b..89de6fa 100644 (file)
@@ -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<Album> albums = new ArrayList<Album>();
-                       albums.addAll(sone.getAlbums());
-                       int lastAlbumIndex = 0;
-                       while (lastAlbumIndex < albums.size()) {
-                               int previousAlbumCount = albums.size();
-                               for (Album album : new ArrayList<Album>(albums.subList(lastAlbumIndex, albums.size()))) {
-                                       albums.addAll(album.getAlbums());
-                               }
-                               lastAlbumIndex = previousAlbumCount;
-                       }
+                       List<Album> albums = Sone.flattenAlbums(sone.getAlbums());
 
                        int albumCounter = 0;
                        for (Album album : albums) {
index aa4a155..4467eba 100644 (file)
@@ -706,6 +706,33 @@ public class Sone implements Fingerprintable, Comparable<Sone> {
        }
 
        //
+       // 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<Album> flattenAlbums(Collection<? extends Album> albums) {
+               List<Album> flatAlbums = new ArrayList<Album>();
+               flatAlbums.addAll(albums);
+               int lastAlbumIndex = 0;
+               while (lastAlbumIndex < flatAlbums.size()) {
+                       int previousAlbumCount = flatAlbums.size();
+                       for (Album album : new ArrayList<Album>(flatAlbums.subList(lastAlbumIndex, flatAlbums.size()))) {
+                               flatAlbums.addAll(album.getAlbums());
+                       }
+                       lastAlbumIndex = previousAlbumCount;
+               }
+               return flatAlbums;
+       }
+
+       //
        // INTERFACE Comparable<Sone>
        //