From eb3a62a5014e129da19f70b90c2b089301e08050 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:31:35 +0200 Subject: [PATCH] Add lazy group implementation and creator. --- .../demoscenemusic/data/DataManager.java | 42 ++++++++++++++++++++++ 1 file changed, 42 insertions(+) diff --git a/src/main/java/net/pterodactylus/demoscenemusic/data/DataManager.java b/src/main/java/net/pterodactylus/demoscenemusic/data/DataManager.java index b56634f..374bb6c 100644 --- a/src/main/java/net/pterodactylus/demoscenemusic/data/DataManager.java +++ b/src/main/java/net/pterodactylus/demoscenemusic/data/DataManager.java @@ -49,6 +49,7 @@ import net.pterodactylus.util.database.ValueFieldWhereClause; public class DataManager { private final ObjectCreator artistCreator = new ArtistCreator(); + private final ObjectCreator groupCreator = new GroupCreator(); private final Database database; @@ -250,4 +251,45 @@ public class DataManager { } + private class LazyGroup extends DefaultGroup { + + private final Memoizer artistsMemoizer = new Memoizer(new Callable() { + + @Override + public Void call() throws Exception { + if (!hasValue("artists")) { + value("artists", Collection.class).set(getArtistsByGroup(id())); + } + return null; + } + + }); + + public LazyGroup(String id) { + super(id); + } + + /** + * {@inheritDoc} + */ + @Override + public Collection artists() { + artistsMemoizer.get(); + return super.artists(); + } + + } + + private class GroupCreator implements ObjectCreator { + + /** + * {@inheritDoc} + */ + @Override + public Group createObject(ResultSet resultSet) throws SQLException { + return new LazyGroup(resultSet.getString("GROUPS.ID")).name(resultSet.getString("GROUPS.NAME")).url(resultSet.getString("GROUPS.URL")); + } + + } + } -- 2.7.4