Remove getAllAlbums() method, use album flattener.
authorDavid ‘Bombe’ Roden <bombe@pterodactylus.net>
Sat, 16 Feb 2013 01:33:11 +0000 (02:33 +0100)
committerDavid ‘Bombe’ Roden <bombe@pterodactylus.net>
Wed, 13 Mar 2013 18:42:49 +0000 (19:42 +0100)
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
src/main/java/net/pterodactylus/sone/web/ImageBrowserPage.java

index 2d24690..d66e032 100644 (file)
@@ -94,6 +94,7 @@ import com.google.common.base.Optional;
 import com.google.common.base.Predicate;
 import com.google.common.base.Predicates;
 import com.google.common.collect.Collections2;
+import com.google.common.collect.FluentIterable;
 import com.google.common.eventbus.EventBus;
 import com.google.common.eventbus.Subscribe;
 import com.google.inject.Inject;
@@ -1867,7 +1868,7 @@ public class Core extends AbstractService implements SoneProvider, PostProvider,
                        configuration.getStringValue(sonePrefix + "/Friends/" + friendCounter + "/ID").setValue(null);
 
                        /* save albums. first, collect in a flat structure, top-level first. */
-                       List<Album> albums = sone.getAllAlbums();
+                       List<Album> albums = FluentIterable.from(sone.getAlbums()).transformAndConcat(Album.FLATTENER).toList();
 
                        int albumCounter = 0;
                        for (Album album : albums) {
index 48241aa..8efcc73 100644 (file)
@@ -29,6 +29,7 @@ import java.util.logging.Logger;
 import net.pterodactylus.sone.core.event.SoneInsertAbortedEvent;
 import net.pterodactylus.sone.core.event.SoneInsertedEvent;
 import net.pterodactylus.sone.core.event.SoneInsertingEvent;
+import net.pterodactylus.sone.data.Album;
 import net.pterodactylus.sone.data.Post;
 import net.pterodactylus.sone.data.Reply;
 import net.pterodactylus.sone.data.Sone;
@@ -47,6 +48,7 @@ import net.pterodactylus.util.template.TemplateException;
 import net.pterodactylus.util.template.TemplateParser;
 import net.pterodactylus.util.template.XmlFilter;
 
+import com.google.common.collect.FluentIterable;
 import com.google.common.collect.Ordering;
 import com.google.common.eventbus.EventBus;
 
@@ -289,7 +291,7 @@ public class SoneInserter extends AbstractService {
                        soneProperties.put("replies", Ordering.from(Reply.TIME_COMPARATOR).reverse().sortedCopy(sone.getReplies()));
                        soneProperties.put("likedPostIds", new HashSet<String>(sone.getLikedPostIds()));
                        soneProperties.put("likedReplyIds", new HashSet<String>(sone.getLikedReplyIds()));
-                       soneProperties.put("albums", sone.getAllAlbums());
+                       soneProperties.put("albums", FluentIterable.from(sone.getAlbums()).transformAndConcat(Album.FLATTENER).toList());
                }
 
                //
index a3e872b..8b08b17 100644 (file)
@@ -38,6 +38,7 @@ import net.pterodactylus.sone.template.SoneAccessor;
 import net.pterodactylus.util.logging.Logging;
 
 import com.google.common.base.Predicate;
+import com.google.common.collect.FluentIterable;
 import com.google.common.hash.Hasher;
 import com.google.common.hash.Hashing;
 
@@ -770,27 +771,6 @@ 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;
-       }
-
-       /**
         * Returns all images of a Sone. Images of a album are inserted into this
         * list before images of all child albums.
         *
@@ -798,7 +778,7 @@ public class Sone implements Fingerprintable, Comparable<Sone> {
         */
        public List<Image> getAllImages() {
                List<Image> allImages = new ArrayList<Image>();
-               for (Album album : getAllAlbums()) {
+               for (Album album : FluentIterable.from(getAlbums()).transformAndConcat(Album.FLATTENER).toList()) {
                        allImages.addAll(album.getImages());
                }
                return allImages;
index 53c22f2..37268ec 100644 (file)
@@ -23,6 +23,7 @@ import java.util.Collections;
 import java.util.List;
 
 import com.google.common.base.Optional;
+import com.google.common.collect.FluentIterable;
 
 import net.pterodactylus.sone.data.Album;
 import net.pterodactylus.sone.data.Image;
@@ -89,7 +90,7 @@ public class ImageBrowserPage extends SoneTemplatePage {
                        templateContext.set("galleryRequested", true);
                        List<Album> albums = new ArrayList<Album>();
                        for (Sone sone : webInterface.getCore().getSones()) {
-                               albums.addAll(sone.getAllAlbums());
+                               albums.addAll(FluentIterable.from(sone.getAlbums()).transformAndConcat(Album.FLATTENER).toList());
                        }
                        Collections.sort(albums, Album.TITLE_COMPARATOR);
                        Pagination<Album> albumPagination = new Pagination<Album>(albums, 12).setPage(Numbers.safeParseInteger(request.getHttpRequest().getParam("page"), 0));