X-Git-Url: https://git.pterodactylus.net/?p=Sone.git;a=blobdiff_plain;f=src%2Fmain%2Fjava%2Fnet%2Fpterodactylus%2Fsone%2Fdata%2FAlbum.java;h=aa14edef2f2f128d9ae5067faaa675736281ac29;hp=710387196b904f9e916859de887a2f30844b4018;hb=c2e868714435ac7c75d77d1911d0dfb00393d051;hpb=e3a3dccdfcb0f7390f8bf30ec1af0f9d9fb69496 diff --git a/src/main/java/net/pterodactylus/sone/data/Album.java b/src/main/java/net/pterodactylus/sone/data/Album.java index 7103871..aa14ede 100644 --- a/src/main/java/net/pterodactylus/sone/data/Album.java +++ b/src/main/java/net/pterodactylus/sone/data/Album.java @@ -20,6 +20,7 @@ package net.pterodactylus.sone.data; import static com.google.common.base.Preconditions.checkArgument; import static com.google.common.base.Preconditions.checkNotNull; import static com.google.common.base.Preconditions.checkState; +import static java.util.Arrays.asList; import java.util.ArrayList; import java.util.Comparator; @@ -30,8 +31,11 @@ import java.util.UUID; import com.google.common.base.Function; 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.collect.ImmutableList; import com.google.common.hash.Hasher; import com.google.common.hash.Hashing; @@ -51,6 +55,47 @@ 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; + } + }; + + /** Function that transforms an album into the images it contains. */ + public static final Function> IMAGES = new Function>() { + + @Override + public List apply(Album album) { + return album.getImages(); + } + }; + + /** + * Filter that removes all albums that do not have any images in any album + * below it. + */ + public static final Predicate NOT_EMPTY = new Predicate() { + + @Override + public boolean apply(Album album) { + return FluentIterable.from(asList(album)).transformAndConcat(FLATTENER).anyMatch(new Predicate() { + + @Override + public boolean apply(Album album) { + return !album.getImages().isEmpty(); + } + }); + } + }; + /** The ID of this album. */ private final String id; @@ -150,7 +195,6 @@ public class Album implements Fingerprintable { public void addAlbum(Album album) { checkNotNull(album, "album must not be null"); checkArgument(album.getSone().equals(sone), "album must belong to the same Sone as this album"); - checkState((this.parent == null) || (this.parent.equals(album.parent)), "album must not already be set to some other Sone"); album.setParent(this); if (!albums.contains(album)) { albums.add(album);