Add method to get child albums of an album to AlbumProvider.
authorDavid ‘Bombe’ Roden <bombe@pterodactylus.net>
Mon, 14 Oct 2013 04:23:30 +0000 (06:23 +0200)
committerDavid ‘Bombe’ Roden <bombe@pterodactylus.net>
Fri, 28 Feb 2014 21:25:25 +0000 (22:25 +0100)
src/main/java/net/pterodactylus/sone/database/AlbumProvider.java
src/main/java/net/pterodactylus/sone/database/memory/MemoryDatabase.java

index b83b179..c035623 100644 (file)
@@ -17,6 +17,8 @@
 
 package net.pterodactylus.sone.database;
 
+import java.util.List;
+
 import net.pterodactylus.sone.data.Album;
 
 import com.google.common.base.Optional;
@@ -38,4 +40,6 @@ public interface AlbumProvider {
         */
        Optional<Album> getAlbum(String albumId);
 
+       List<Album> getAlbums(Album parent);
+
 }
index 4437baf..451be3a 100644 (file)
@@ -21,6 +21,7 @@ import static com.google.common.base.Optional.fromNullable;
 import static com.google.common.base.Preconditions.checkNotNull;
 import static com.google.common.base.Predicates.not;
 import static com.google.common.collect.FluentIterable.from;
+import static java.util.Collections.emptyList;
 import static net.pterodactylus.sone.data.Sone.LOCAL_SONE_FILTER;
 
 import java.util.ArrayList;
@@ -52,6 +53,7 @@ import net.pterodactylus.sone.database.SoneBuilder;
 import net.pterodactylus.util.config.Configuration;
 import net.pterodactylus.util.config.ConfigurationException;
 
+import com.google.common.base.Function;
 import com.google.common.base.Optional;
 import com.google.common.collect.ArrayListMultimap;
 import com.google.common.collect.ListMultimap;
@@ -480,6 +482,16 @@ public class MemoryDatabase extends AbstractService implements Database {
                }
        }
 
+       @Override
+       public List<Album> getAlbums(Album parent) {
+               lock.readLock().lock();
+               try {
+                       return from(albumChildren.get(parent.getId())).transformAndConcat(getAlbum()).toList();
+               } finally {
+                       lock.readLock().unlock();
+               }
+       }
+
        //
        // ALBUMSTORE METHODS
        //
@@ -846,4 +858,13 @@ public class MemoryDatabase extends AbstractService implements Database {
                }
        }
 
+       private Function<String, Iterable<Album>> getAlbum() {
+               return new Function<String, Iterable<Album>>() {
+                       @Override
+                       public Iterable<Album> apply(String input) {
+                               return (input == null) ? Collections.<Album>emptyList() : getAlbum(input).asSet();
+                       }
+               };
+       }
+
 }