Add isRoot() method to Album, only dump album ID in sone.xml if the parent is not...
[Sone.git] / src / main / java / net / pterodactylus / sone / data / Album.java
index 5375d98..8238b3f 100644 (file)
@@ -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,6 +31,7 @@ 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;
@@ -42,7 +44,7 @@ import com.google.common.hash.Hashing;
  *
  * @author <a href="mailto:bombe@pterodactylus.net">David ‘Bombe’ Roden</a>
  */
-public class Album implements Fingerprintable {
+public class Album implements Identified, Fingerprintable {
 
        /** Compares two {@link Album}s by {@link #getTitle()}. */
        public static final Comparator<Album> TITLE_COMPARATOR = new Comparator<Album>() {
@@ -67,6 +69,41 @@ public class Album implements Fingerprintable {
                }
        };
 
+       /** Function that transforms an album into the images it contains. */
+       public static final Function<Album, List<Image>> IMAGES = new Function<Album, List<Image>>() {
+
+               @Override
+               public List<Image> 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<Album> NOT_EMPTY = new Predicate<Album>() {
+
+               @Override
+               public boolean apply(Album album) {
+                       /* so, we flatten all albums below the given one and check whether at least one album… */
+                       return FluentIterable.from(asList(album)).transformAndConcat(FLATTENER).anyMatch(new Predicate<Album>() {
+
+                               @Override
+                               public boolean apply(Album album) {
+                                       /* …contains any inserted images. */
+                                       return !album.getImages().isEmpty() && FluentIterable.from(album.getImages()).allMatch(new Predicate<Image>() {
+
+                                               @Override
+                                               public boolean apply(Image input) {
+                                                       return input.isInserted();
+                                               }
+                                       });
+                               }
+                       });
+               }
+       };
+
        /** The ID of this album. */
        private final String id;
 
@@ -166,7 +203,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);
@@ -372,6 +408,16 @@ public class Album implements Fingerprintable {
        }
 
        /**
+        * Returns whether this album is an identitiy’s root album.
+        *
+        * @return {@code true} if this album is an identity’s root album, {@code
+        *         false} otherwise
+        */
+       public boolean isRoot() {
+               return parent == null;
+       }
+
+       /**
         * Returns the parent album of this album.
         *
         * @return The parent album of this album, or {@code null} if this album