Add artist implementation that lazily loads referenced objects.
authorDavid ‘Bombe’ Roden <bombe@pterodactylus.net>
Thu, 19 Apr 2012 14:01:02 +0000 (16:01 +0200)
committerDavid ‘Bombe’ Roden <bombe@pterodactylus.net>
Thu, 19 Apr 2012 14:01:02 +0000 (16:01 +0200)
src/main/java/net/pterodactylus/demoscenemusic/data/DataManager.java

index e6d58bf..5183226 100644 (file)
@@ -23,7 +23,9 @@ import java.util.ArrayList;
 import java.util.Collection;
 import java.util.HashSet;
 import java.util.List;
+import java.util.concurrent.Callable;
 
+import net.pterodactylus.util.collection.Memoizer;
 import net.pterodactylus.util.database.Database;
 import net.pterodactylus.util.database.DatabaseException;
 import net.pterodactylus.util.database.Field;
@@ -181,4 +183,26 @@ public class DataManager {
 
        }
 
+       private class LazyArtist extends DefaultArtist {
+
+               private final Memoizer<Void> tracks = new Memoizer<Void>(new Callable<Void>() {
+                       public Void call() throws DatabaseException {
+                               if (!hasValue("tracks")) {
+                                       value("tracks", Collection.class).set(getTracksByArtist(id()));
+                               }
+                               return null;
+                       }
+               });
+
+               public LazyArtist(String id) {
+                       super(id);
+               }
+
+               public Collection<Track> tracks() {
+                       tracks.get();
+                       return value("tracks", Collection.class).get();
+               }
+
+       }
+
 }