From ccdb2ab4b84f960ec67561227692f6e694a7f260 Mon Sep 17 00:00:00 2001 From: =?utf8?q?David=20=E2=80=98Bombe=E2=80=99=20Roden?= Date: Thu, 19 Apr 2012 16:01:02 +0200 Subject: [PATCH] Add artist implementation that lazily loads referenced objects. --- .../demoscenemusic/data/DataManager.java | 24 ++++++++++++++++++++++ 1 file changed, 24 insertions(+) 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(); + } + + } + } -- 2.7.4