Add function to flatten an album and all albums beneath it.
[Sone.git] / src / main / java / net / pterodactylus / sone / data / Album.java
index 7103871..5375d98 100644 (file)
@@ -32,6 +32,8 @@ import com.google.common.base.Function;
 import com.google.common.base.Optional;
 import com.google.common.base.Predicates;
 import com.google.common.collect.Collections2;
+import com.google.common.collect.FluentIterable;
+import com.google.common.collect.ImmutableList;
 import com.google.common.hash.Hasher;
 import com.google.common.hash.Hashing;
 
@@ -51,6 +53,20 @@ public class Album implements Fingerprintable {
                }
        };
 
+       /** Function that flattens the given album and all albums beneath it. */
+       public static final Function<Album, List<Album>> FLATTENER = new Function<Album, List<Album>>() {
+
+               @Override
+               public List<Album> apply(Album album) {
+                       List<Album> albums = new ArrayList<Album>();
+                       albums.add(album);
+                       for (Album subAlbum : album.getAlbums()) {
+                               albums.addAll(FluentIterable.from(ImmutableList.of(subAlbum)).transformAndConcat(FLATTENER).toList());
+                       }
+                       return albums;
+               }
+       };
+
        /** The ID of this album. */
        private final String id;