Remove getAllAlbums() method, use album flattener.
[Sone.git] / src / main / java / net / pterodactylus / sone / data / Sone.java
index a609982..8b08b17 100644 (file)
@@ -38,6 +38,9 @@ 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;
 
 import freenet.keys.FreenetURI;
 
@@ -768,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.
         *
@@ -796,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;
@@ -915,46 +897,46 @@ public class Sone implements Fingerprintable, Comparable<Sone> {
         */
        @Override
        public synchronized String getFingerprint() {
-               StringBuilder fingerprint = new StringBuilder();
-               fingerprint.append(profile.getFingerprint());
+               Hasher hash = Hashing.sha256().newHasher();
+               hash.putString(profile.getFingerprint());
 
-               fingerprint.append("Posts(");
+               hash.putString("Posts(");
                for (Post post : getPosts()) {
-                       fingerprint.append("Post(").append(post.getId()).append(')');
+                       hash.putString("Post(").putString(post.getId()).putString(")");
                }
-               fingerprint.append(")");
+               hash.putString(")");
 
                List<PostReply> replies = new ArrayList<PostReply>(getReplies());
                Collections.sort(replies, Reply.TIME_COMPARATOR);
-               fingerprint.append("Replies(");
+               hash.putString("Replies(");
                for (PostReply reply : replies) {
-                       fingerprint.append("Reply(").append(reply.getId()).append(')');
+                       hash.putString("Reply(").putString(reply.getId()).putString(")");
                }
-               fingerprint.append(')');
+               hash.putString(")");
 
                List<String> likedPostIds = new ArrayList<String>(getLikedPostIds());
                Collections.sort(likedPostIds);
-               fingerprint.append("LikedPosts(");
+               hash.putString("LikedPosts(");
                for (String likedPostId : likedPostIds) {
-                       fingerprint.append("Post(").append(likedPostId).append(')');
+                       hash.putString("Post(").putString(likedPostId).putString(")");
                }
-               fingerprint.append(')');
+               hash.putString(")");
 
                List<String> likedReplyIds = new ArrayList<String>(getLikedReplyIds());
                Collections.sort(likedReplyIds);
-               fingerprint.append("LikedReplies(");
+               hash.putString("LikedReplies(");
                for (String likedReplyId : likedReplyIds) {
-                       fingerprint.append("Reply(").append(likedReplyId).append(')');
+                       hash.putString("Reply(").putString(likedReplyId).putString(")");
                }
-               fingerprint.append(')');
+               hash.putString(")");
 
-               fingerprint.append("Albums(");
+               hash.putString("Albums(");
                for (Album album : albums) {
-                       fingerprint.append(album.getFingerprint());
+                       hash.putString(album.getFingerprint());
                }
-               fingerprint.append(')');
+               hash.putString(")");
 
-               return fingerprint.toString();
+               return hash.hash().toString();
        }
 
        //