From: David ‘Bombe’ Roden Date: Thu, 19 Apr 2012 14:01:02 +0000 (+0200) Subject: Add artist implementation that lazily loads referenced objects. X-Git-Url: https://git.pterodactylus.net/?a=commitdiff_plain;h=ccdb2ab4b84f960ec67561227692f6e694a7f260;p=demoscenemusic.git Add artist implementation that lazily loads referenced objects. --- diff --git a/src/main/java/net/pterodactylus/demoscenemusic/data/DataManager.java b/src/main/java/net/pterodactylus/demoscenemusic/data/DataManager.java index e6d58bf..5183226 100644 --- a/src/main/java/net/pterodactylus/demoscenemusic/data/DataManager.java +++ b/src/main/java/net/pterodactylus/demoscenemusic/data/DataManager.java @@ -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 tracks = new Memoizer(new Callable() { + 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 tracks() { + tracks.get(); + return value("tracks", Collection.class).get(); + } + + } + }