Turn album list flattening into a proper Sone method.
authorDavid ‘Bombe’ Roden <bombe@pterodactylus.net>
Tue, 27 Sep 2011 17:52:21 +0000 (19:52 +0200)
committerDavid ‘Bombe’ Roden <bombe@pterodactylus.net>
Tue, 27 Sep 2011 17:52:21 +0000 (19:52 +0200)
src/main/java/net/pterodactylus/sone/core/Core.java
src/main/java/net/pterodactylus/sone/core/SoneInserter.java
src/main/java/net/pterodactylus/sone/data/Sone.java

index 8a2b3cb..1bea477 100644 (file)
@@ -2097,7 +2097,7 @@ public class Core extends AbstractService implements IdentityListener, UpdateLis
                        configuration.getStringValue(sonePrefix + "/Friends/" + friendCounter + "/ID").setValue(null);
 
                        /* save albums. first, collect in a flat structure, top-level first. */
-                       List<Album> albums = Sone.flattenAlbums(sone.getAlbums());
+                       List<Album> albums = sone.getAllAlbums();
 
                        int albumCounter = 0;
                        for (Album album : albums) {
index 44f8bbc..a66069f 100644 (file)
@@ -302,7 +302,7 @@ public class SoneInserter extends AbstractService {
                        soneProperties.put("replies", new ListBuilder<Reply>(new ArrayList<Reply>(sone.getReplies())).sort(new ReverseComparator<Reply>(Reply.TIME_COMPARATOR)).get());
                        soneProperties.put("likedPostIds", new HashSet<String>(sone.getLikedPostIds()));
                        soneProperties.put("likedReplyIds", new HashSet<String>(sone.getLikedReplyIds()));
-                       soneProperties.put("albums", Sone.flattenAlbums(sone.getAlbums()));
+                       soneProperties.put("albums", sone.getAllAlbums());
                }
 
                //
index e124387..aad50e5 100644 (file)
@@ -645,6 +645,27 @@ public class Sone implements Fingerprintable, Comparable<Sone> {
        }
 
        /**
+        * Returns a flattened list of all albums of this Sone. The resulting list
+        * contains parent albums before child albums so that the resulting list can
+        * be parsed in a single pass.
+        *
+        * @return The flattened albums
+        */
+       public List<Album> getAllAlbums() {
+               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;
+       }
+
+       /**
         * Adds an album to this Sone.
         *
         * @param album
@@ -781,33 +802,6 @@ 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>
        //