From: David ‘Bombe’ Roden Date: Sat, 16 Feb 2013 01:27:01 +0000 (+0100) Subject: Add function to flatten an album and all albums beneath it. X-Git-Tag: 0.8.5^2~3^2~7 X-Git-Url: https://git.pterodactylus.net/?p=Sone.git;a=commitdiff_plain;h=0382e0afaa2bf1c193f4092a6468c99ff942df0c;ds=sidebyside Add function to flatten an album and all albums beneath it. --- diff --git a/src/main/java/net/pterodactylus/sone/data/Album.java b/src/main/java/net/pterodactylus/sone/data/Album.java index 7103871..5375d98 100644 --- a/src/main/java/net/pterodactylus/sone/data/Album.java +++ b/src/main/java/net/pterodactylus/sone/data/Album.java @@ -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> FLATTENER = new Function>() { + + @Override + public List apply(Album album) { + List albums = new ArrayList(); + 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;