Add lazy track implementation and creator.
authorDavid ‘Bombe’ Roden <bombe@pterodactylus.net>
Thu, 19 Apr 2012 14:55:01 +0000 (16:55 +0200)
committerDavid ‘Bombe’ Roden <bombe@pterodactylus.net>
Thu, 19 Apr 2012 14:55:01 +0000 (16:55 +0200)
src/main/java/net/pterodactylus/demoscenemusic/data/DataManager.java

index e595121..5523bbe 100644 (file)
@@ -50,6 +50,7 @@ public class DataManager {
 
        private final ObjectCreator<Artist> artistCreator = new ArtistCreator();
        private final ObjectCreator<Group> groupCreator = new GroupCreator();
+       private final ObjectCreator<Track> trackCreator = new TrackCreator();
 
        private final Database database;
 
@@ -285,4 +286,57 @@ public class DataManager {
 
        }
 
+       private class LazyTrack extends DefaultTrack {
+
+               private final Memoizer<Void> artistsMemoizer = new Memoizer<Void>(new Callable<Void>() {
+
+                       @Override
+                       public Void call() throws Exception {
+                               if (!hasValue("artists")) {
+                                       value("artists", List.class).set(getArtistsByTrack(id()));
+                               }
+                               return null;
+                       }
+
+               });
+
+               /**
+                * @param id
+                */
+               public LazyTrack(String id) {
+                       super(id);
+               }
+
+               /**
+                * {@inheritDoc}
+                */
+               @Override
+               public List<Artist> artists() {
+                       artistsMemoizer.get();
+                       return super.artists();
+               }
+
+               /**
+                * {@inheritDoc}
+                */
+               @Override
+               public Collection<Style> styles() {
+                       // TODO Auto-generated method stub
+                       return super.styles();
+               }
+
+       }
+
+       private class TrackCreator implements ObjectCreator<Track> {
+
+               /**
+                * {@inheritDoc}
+                */
+               @Override
+               public Track createObject(ResultSet resultSet) throws SQLException {
+                       return new LazyTrack(resultSet.getString("TRACKS.ID")).name(resultSet.getString("TRACKS.NAME"));
+               }
+
+       }
+
 }