diff --git a/src/main/java/sarsoo/fmframework/cache/Cacheable.java b/src/main/java/sarsoo/fmframework/cache/Cacheable.java new file mode 100644 index 0000000..5cf2ebb --- /dev/null +++ b/src/main/java/sarsoo/fmframework/cache/Cacheable.java @@ -0,0 +1,7 @@ +package sarsoo.fmframework.cache; + +public interface Cacheable { + + public boolean matches(Object o); + +} diff --git a/src/main/java/sarsoo/fmframework/cache/StaticCache.java b/src/main/java/sarsoo/fmframework/cache/StaticCache.java index 5857efb..21b0928 100644 --- a/src/main/java/sarsoo/fmframework/cache/StaticCache.java +++ b/src/main/java/sarsoo/fmframework/cache/StaticCache.java @@ -9,7 +9,7 @@ import sarsoo.fmframework.log.Logger; import sarsoo.fmframework.log.entry.ErrorEntry; import sarsoo.fmframework.log.entry.LogEntry; -public class StaticCache implements IStaticCache { +public class StaticCache implements IStaticCache { private ArrayList> pool; private Puller puller; @@ -26,7 +26,7 @@ public class StaticCache implements IStaticCache { @Override public T get(S input) { - Optional> item = pool.stream().filter(i -> i.getSubject().equals(input)).findFirst(); + Optional> item = pool.stream().filter(i -> i.getSubject().matches(input)).findFirst(); if (item.isPresent()) { Logger.getLog().log(new LogEntry("getCachedItem").addArg("found").addArg(input.toString())); @@ -50,7 +50,7 @@ public class StaticCache implements IStaticCache { @Override public T getNew(S input) { - Optional> item = pool.stream().filter(i -> i.getSubject().equals(input)).findFirst(); + Optional> item = pool.stream().filter(i -> i.getSubject().matches(input)).findFirst(); if (item.isPresent()) { Logger.getLog().log(new LogEntry("getNewCachedItem").addArg("removed").addArg(input.toString())); diff --git a/src/main/java/sarsoo/fmframework/cache/TagPool.java b/src/main/java/sarsoo/fmframework/cache/TagPool.java deleted file mode 100644 index 6a0bd43..0000000 --- a/src/main/java/sarsoo/fmframework/cache/TagPool.java +++ /dev/null @@ -1,69 +0,0 @@ -package sarsoo.fmframework.cache; - -import java.util.ArrayList; - -import sarsoo.fmframework.config.Config; -import sarsoo.fmframework.fm.FmUserNetwork; -import sarsoo.fmframework.fx.FmFramework; -import sarsoo.fmframework.util.FMObjList; - -public class TagPool { - - private static TagPool instance; - - private ArrayList tagList; - - private TagPool() { - tagList = new ArrayList(); - } - -// public static synchronized TagPool getInstance() { -// if (instance == null) { -// instance = new TagPool(); -// } -// return instance; -// } - - public static TagPool getPool() { - if (instance == null) { - synchronized (TagPool.class) { - if (instance == null) { - instance = new TagPool(); - } - } - } - return instance; - } - - public FMObjList getTag(String name) { - System.out.println("gettag " + name); - int i; - boolean containedInPool = false; - for (i = 0; i < tagList.size(); i++) { - if (tagList.get(i).getGroupName().equals(name)) { - containedInPool = true; - System.out.println("found in pool"); - - break; - } - } - - if (containedInPool) { - System.out.println("returned from pool"); - return tagList.get(i); - } else { - Config config = FmFramework.getSessionConfig(); - - FmUserNetwork net = new FmUserNetwork(config.getValue("api_key"), config.getValue("username")); - FMObjList tag = net.getTag(name); - tagList.add(tag); - System.out.println("pulling tag"); - return tag; - } - } - - public void flush() { - tagList.clear(); - } - -} diff --git a/src/main/java/sarsoo/fmframework/cache/puller/TagPuller.java b/src/main/java/sarsoo/fmframework/cache/puller/TagPuller.java new file mode 100644 index 0000000..a721064 --- /dev/null +++ b/src/main/java/sarsoo/fmframework/cache/puller/TagPuller.java @@ -0,0 +1,18 @@ +package sarsoo.fmframework.cache.puller; + +import sarsoo.fmframework.fm.FmUserNetwork; +import sarsoo.fmframework.util.FMObjList; + +public class TagPuller implements Puller { + + private FmUserNetwork net; + + public TagPuller(FmUserNetwork net) { + this.net = net; + } + + public FMObjList pull(String name) { + return net.getTag(name); + } + +} diff --git a/src/main/java/sarsoo/fmframework/fx/FmFramework.java b/src/main/java/sarsoo/fmframework/fx/FmFramework.java index 41e2ea1..3000ef5 100644 --- a/src/main/java/sarsoo/fmframework/fx/FmFramework.java +++ b/src/main/java/sarsoo/fmframework/fx/FmFramework.java @@ -7,12 +7,16 @@ import javafx.fxml.FXMLLoader; import javafx.scene.Parent; import javafx.scene.Scene; import javafx.stage.Stage; +import sarsoo.fmframework.cache.StaticCache; +import sarsoo.fmframework.cache.puller.TagPuller; import sarsoo.fmframework.config.Config; import sarsoo.fmframework.config.ConfigPersister; +import sarsoo.fmframework.fm.FmUserNetwork; import sarsoo.fmframework.fx.controller.RootController; import sarsoo.fmframework.fx.service.SaveConfigService; import sarsoo.fmframework.log.Logger; import sarsoo.fmframework.log.entry.LogEntry; +import sarsoo.fmframework.util.FMObjList; public class FmFramework extends Application { @@ -21,6 +25,8 @@ public class FmFramework extends Application { private static Config config; + private static StaticCache tagPool = null; + private static RootController control; @Override @@ -28,7 +34,8 @@ public class FmFramework extends Application { FmFramework.stage = stage; initConfig(); - + initCaches(); + FXMLLoader loader = new FXMLLoader(getClass().getResource("ui/RootPane.fxml")); // Parent root = FXMLLoader.load(getClass().getResource("ui/main.fxml")); @@ -48,7 +55,7 @@ public class FmFramework extends Application { stage.show(); } - + private void initConfig() { ConfigPersister persist = new ConfigPersister(); @@ -62,18 +69,28 @@ public class FmFramework extends Application { SaveConfigService saveConfig = new SaveConfigService(".fm/", config); saveConfig.start(); - }else { + } else { Logger.getLog().log(new LogEntry("load config").addArg("null config returned")); } } + private void initCaches() { + + tagPool = new StaticCache<>( + new TagPuller(new FmUserNetwork(config.getValue("api_key"), config.getValue("username")))); + + } + + public static StaticCache getTagPool(){ + return tagPool; + } + public static Config getSessionConfig() { return config; } public static void main(String[] args) { - Runtime.getRuntime().addShutdownHook( - new Thread(() -> (new ConfigPersister()).saveConfig(".fm/", config))); + Runtime.getRuntime().addShutdownHook(new Thread(() -> (new ConfigPersister()).saveConfig(".fm/", config))); launch(args); } diff --git a/src/main/java/sarsoo/fmframework/fx/chart/GenrePieChart.java b/src/main/java/sarsoo/fmframework/fx/chart/GenrePieChart.java index 7b1ba71..3a62b2a 100644 --- a/src/main/java/sarsoo/fmframework/fx/chart/GenrePieChart.java +++ b/src/main/java/sarsoo/fmframework/fx/chart/GenrePieChart.java @@ -7,7 +7,8 @@ import java.util.Comparator; import javafx.collections.FXCollections; import javafx.collections.ObservableList; import javafx.scene.chart.PieChart; -import sarsoo.fmframework.cache.TagPool; +import sarsoo.fmframework.cache.StaticCache; +import sarsoo.fmframework.fx.FmFramework; import sarsoo.fmframework.util.FMObjList; public class GenrePieChart extends PieChart{ @@ -25,13 +26,13 @@ public class GenrePieChart extends PieChart{ pieChartData = FXCollections.observableArrayList(); - TagPool tagPool = TagPool.getPool(); + StaticCache tagPool = FmFramework.getTagPool(); ArrayList tagObjs = new ArrayList(); int i; for(i = 0; i < tagNames.size(); i++){ - tagObjs.add(tagPool.getTag(tagNames.get(i))); + tagObjs.add(tagPool.get(tagNames.get(i))); } for(i = 0; i < tagObjs.size(); i++) { diff --git a/src/main/java/sarsoo/fmframework/fx/service/GetTagMenuItemsService.java b/src/main/java/sarsoo/fmframework/fx/service/GetTagMenuItemsService.java index dad89e9..7f93c90 100644 --- a/src/main/java/sarsoo/fmframework/fx/service/GetTagMenuItemsService.java +++ b/src/main/java/sarsoo/fmframework/fx/service/GetTagMenuItemsService.java @@ -8,7 +8,6 @@ import javafx.concurrent.Task; import javafx.event.ActionEvent; import javafx.event.EventHandler; import javafx.scene.control.MenuItem; -import sarsoo.fmframework.cache.TagPool; import sarsoo.fmframework.fx.FmFramework; import sarsoo.fmframework.fx.tab.FMObjListTab; import sarsoo.fmframework.log.Logger; @@ -54,7 +53,7 @@ public class GetTagMenuItemsService extends Service> { @Override protected Void call() throws Exception { - FMObjListTab tab = new FMObjListTab(TagPool.getPool().getTag(name)); + FMObjListTab tab = new FMObjListTab(FmFramework.getTagPool().get(name)); Platform.runLater(new Runnable() { @Override diff --git a/src/main/java/sarsoo/fmframework/music/Album.java b/src/main/java/sarsoo/fmframework/music/Album.java index 15bd309..eb44313 100644 --- a/src/main/java/sarsoo/fmframework/music/Album.java +++ b/src/main/java/sarsoo/fmframework/music/Album.java @@ -3,7 +3,9 @@ package sarsoo.fmframework.music; import java.io.Serializable; import java.util.ArrayList; -public class Album extends FMObj implements Serializable{ +import sarsoo.fmframework.cache.Cacheable; + +public class Album extends FMObj implements Serializable, Cacheable{ private static final long serialVersionUID = 1L; protected Artist artist; @@ -150,4 +152,9 @@ public class Album extends FMObj implements Serializable{ } return null; } + + @Override + public boolean matches(Object o) { + return equals(o); + } } diff --git a/src/main/java/sarsoo/fmframework/music/Artist.java b/src/main/java/sarsoo/fmframework/music/Artist.java index 8ade396..7314487 100644 --- a/src/main/java/sarsoo/fmframework/music/Artist.java +++ b/src/main/java/sarsoo/fmframework/music/Artist.java @@ -3,7 +3,9 @@ package sarsoo.fmframework.music; import java.io.Serializable; import java.util.ArrayList; -public class Artist extends FMObj implements Serializable { +import sarsoo.fmframework.cache.Cacheable; + +public class Artist extends FMObj implements Serializable, Cacheable { /** * @@ -66,6 +68,11 @@ public class Artist extends FMObj implements Serializable { return false; } + + @Override + public boolean matches(Object o) { + return equals(o); + } public String toString() { return "Artist: " + name; diff --git a/src/main/java/sarsoo/fmframework/music/Track.java b/src/main/java/sarsoo/fmframework/music/Track.java index f3c0b8b..7ec39e8 100644 --- a/src/main/java/sarsoo/fmframework/music/Track.java +++ b/src/main/java/sarsoo/fmframework/music/Track.java @@ -3,7 +3,9 @@ package sarsoo.fmframework.music; import java.io.Serializable; import java.util.ArrayList; -public class Track extends FMObj implements Serializable { +import sarsoo.fmframework.cache.Cacheable; + +public class Track extends FMObj implements Serializable, Cacheable { /** * @@ -92,7 +94,11 @@ public class Track extends FMObj implements Serializable { return false; } - + + @Override + public boolean matches(Object o) { + return equals(o); + } public String toString() { if (album != null) diff --git a/src/main/java/sarsoo/fmframework/util/FMObjList.java b/src/main/java/sarsoo/fmframework/util/FMObjList.java index fa04435..faa79dd 100644 --- a/src/main/java/sarsoo/fmframework/util/FMObjList.java +++ b/src/main/java/sarsoo/fmframework/util/FMObjList.java @@ -3,6 +3,7 @@ package sarsoo.fmframework.util; import java.io.Serializable; import java.util.ArrayList; +import sarsoo.fmframework.cache.Cacheable; import sarsoo.fmframework.config.Config; import sarsoo.fmframework.fm.FmUserNetwork; import sarsoo.fmframework.fx.FmFramework; @@ -11,7 +12,7 @@ import sarsoo.fmframework.music.Artist; import sarsoo.fmframework.music.FMObj; import sarsoo.fmframework.music.Track; -public class FMObjList extends ArrayList implements Comparable, Serializable { +public class FMObjList extends ArrayList implements Comparable, Serializable, Cacheable { private static final long serialVersionUID = 1L; @@ -86,6 +87,24 @@ public class FMObjList extends ArrayList implements Comparable return getTotalUserScrobbles() - list.getTotalUserScrobbles(); } + @Override + public boolean matches(Object obj) { + + if (obj instanceof String) { + String stringed = (String) obj; + if (getGroupName().equalsIgnoreCase(stringed)) + return true; + } + + if (obj instanceof FMObjList) { + FMObjList list = (FMObjList) obj; + if (getGroupName().equalsIgnoreCase(list.getGroupName())) + return true; + } + + return false; + } + public void refresh() { Config config = FmFramework.getSessionConfig();