added cacheable, removed tag pool

This commit is contained in:
aj 2019-05-17 07:32:51 +01:00
parent 6ef7488495
commit 12508ea57d
11 changed files with 99 additions and 87 deletions

View File

@ -0,0 +1,7 @@
package sarsoo.fmframework.cache;
public interface Cacheable {
public boolean matches(Object o);
}

View File

@ -9,7 +9,7 @@ import sarsoo.fmframework.log.Logger;
import sarsoo.fmframework.log.entry.ErrorEntry; import sarsoo.fmframework.log.entry.ErrorEntry;
import sarsoo.fmframework.log.entry.LogEntry; import sarsoo.fmframework.log.entry.LogEntry;
public class StaticCache<T, S> implements IStaticCache<T, S> { public class StaticCache<T extends Cacheable, S> implements IStaticCache<T, S> {
private ArrayList<CacheEntry<T>> pool; private ArrayList<CacheEntry<T>> pool;
private Puller<T, S> puller; private Puller<T, S> puller;
@ -26,7 +26,7 @@ public class StaticCache<T, S> implements IStaticCache<T, S> {
@Override @Override
public T get(S input) { public T get(S input) {
Optional<CacheEntry<T>> item = pool.stream().filter(i -> i.getSubject().equals(input)).findFirst(); Optional<CacheEntry<T>> item = pool.stream().filter(i -> i.getSubject().matches(input)).findFirst();
if (item.isPresent()) { if (item.isPresent()) {
Logger.getLog().log(new LogEntry("getCachedItem").addArg("found").addArg(input.toString())); Logger.getLog().log(new LogEntry("getCachedItem").addArg("found").addArg(input.toString()));
@ -50,7 +50,7 @@ public class StaticCache<T, S> implements IStaticCache<T, S> {
@Override @Override
public T getNew(S input) { public T getNew(S input) {
Optional<CacheEntry<T>> item = pool.stream().filter(i -> i.getSubject().equals(input)).findFirst(); Optional<CacheEntry<T>> item = pool.stream().filter(i -> i.getSubject().matches(input)).findFirst();
if (item.isPresent()) { if (item.isPresent()) {
Logger.getLog().log(new LogEntry("getNewCachedItem").addArg("removed").addArg(input.toString())); Logger.getLog().log(new LogEntry("getNewCachedItem").addArg("removed").addArg(input.toString()));

View File

@ -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<FMObjList> tagList;
private TagPool() {
tagList = new ArrayList<FMObjList>();
}
// 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();
}
}

View File

@ -0,0 +1,18 @@
package sarsoo.fmframework.cache.puller;
import sarsoo.fmframework.fm.FmUserNetwork;
import sarsoo.fmframework.util.FMObjList;
public class TagPuller implements Puller<FMObjList, String> {
private FmUserNetwork net;
public TagPuller(FmUserNetwork net) {
this.net = net;
}
public FMObjList pull(String name) {
return net.getTag(name);
}
}

View File

@ -7,12 +7,16 @@ import javafx.fxml.FXMLLoader;
import javafx.scene.Parent; import javafx.scene.Parent;
import javafx.scene.Scene; import javafx.scene.Scene;
import javafx.stage.Stage; import javafx.stage.Stage;
import sarsoo.fmframework.cache.StaticCache;
import sarsoo.fmframework.cache.puller.TagPuller;
import sarsoo.fmframework.config.Config; import sarsoo.fmframework.config.Config;
import sarsoo.fmframework.config.ConfigPersister; import sarsoo.fmframework.config.ConfigPersister;
import sarsoo.fmframework.fm.FmUserNetwork;
import sarsoo.fmframework.fx.controller.RootController; import sarsoo.fmframework.fx.controller.RootController;
import sarsoo.fmframework.fx.service.SaveConfigService; import sarsoo.fmframework.fx.service.SaveConfigService;
import sarsoo.fmframework.log.Logger; import sarsoo.fmframework.log.Logger;
import sarsoo.fmframework.log.entry.LogEntry; import sarsoo.fmframework.log.entry.LogEntry;
import sarsoo.fmframework.util.FMObjList;
public class FmFramework extends Application { public class FmFramework extends Application {
@ -21,6 +25,8 @@ public class FmFramework extends Application {
private static Config config; private static Config config;
private static StaticCache<FMObjList, String> tagPool = null;
private static RootController control; private static RootController control;
@Override @Override
@ -28,6 +34,7 @@ public class FmFramework extends Application {
FmFramework.stage = stage; FmFramework.stage = stage;
initConfig(); initConfig();
initCaches();
FXMLLoader loader = new FXMLLoader(getClass().getResource("ui/RootPane.fxml")); FXMLLoader loader = new FXMLLoader(getClass().getResource("ui/RootPane.fxml"));
@ -62,18 +69,28 @@ public class FmFramework extends Application {
SaveConfigService saveConfig = new SaveConfigService(".fm/", config); SaveConfigService saveConfig = new SaveConfigService(".fm/", config);
saveConfig.start(); saveConfig.start();
}else { } else {
Logger.getLog().log(new LogEntry("load config").addArg("null config returned")); 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<FMObjList, String> getTagPool(){
return tagPool;
}
public static Config getSessionConfig() { public static Config getSessionConfig() {
return config; return config;
} }
public static void main(String[] args) { public static void main(String[] args) {
Runtime.getRuntime().addShutdownHook( Runtime.getRuntime().addShutdownHook(new Thread(() -> (new ConfigPersister()).saveConfig(".fm/", config)));
new Thread(() -> (new ConfigPersister()).saveConfig(".fm/", config)));
launch(args); launch(args);
} }

View File

@ -7,7 +7,8 @@ import java.util.Comparator;
import javafx.collections.FXCollections; import javafx.collections.FXCollections;
import javafx.collections.ObservableList; import javafx.collections.ObservableList;
import javafx.scene.chart.PieChart; 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; import sarsoo.fmframework.util.FMObjList;
public class GenrePieChart extends PieChart{ public class GenrePieChart extends PieChart{
@ -25,13 +26,13 @@ public class GenrePieChart extends PieChart{
pieChartData = FXCollections.observableArrayList(); pieChartData = FXCollections.observableArrayList();
TagPool tagPool = TagPool.getPool(); StaticCache<FMObjList, String> tagPool = FmFramework.getTagPool();
ArrayList<FMObjList> tagObjs = new ArrayList<FMObjList>(); ArrayList<FMObjList> tagObjs = new ArrayList<FMObjList>();
int i; int i;
for(i = 0; i < tagNames.size(); 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++) { for(i = 0; i < tagObjs.size(); i++) {

View File

@ -8,7 +8,6 @@ import javafx.concurrent.Task;
import javafx.event.ActionEvent; import javafx.event.ActionEvent;
import javafx.event.EventHandler; import javafx.event.EventHandler;
import javafx.scene.control.MenuItem; import javafx.scene.control.MenuItem;
import sarsoo.fmframework.cache.TagPool;
import sarsoo.fmframework.fx.FmFramework; import sarsoo.fmframework.fx.FmFramework;
import sarsoo.fmframework.fx.tab.FMObjListTab; import sarsoo.fmframework.fx.tab.FMObjListTab;
import sarsoo.fmframework.log.Logger; import sarsoo.fmframework.log.Logger;
@ -54,7 +53,7 @@ public class GetTagMenuItemsService extends Service<ArrayList<MenuItem>> {
@Override @Override
protected Void call() throws Exception { 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() { Platform.runLater(new Runnable() {
@Override @Override

View File

@ -3,7 +3,9 @@ package sarsoo.fmframework.music;
import java.io.Serializable; import java.io.Serializable;
import java.util.ArrayList; 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; private static final long serialVersionUID = 1L;
protected Artist artist; protected Artist artist;
@ -150,4 +152,9 @@ public class Album extends FMObj implements Serializable{
} }
return null; return null;
} }
@Override
public boolean matches(Object o) {
return equals(o);
}
} }

View File

@ -3,7 +3,9 @@ package sarsoo.fmframework.music;
import java.io.Serializable; import java.io.Serializable;
import java.util.ArrayList; import java.util.ArrayList;
public class Artist extends FMObj implements Serializable { import sarsoo.fmframework.cache.Cacheable;
public class Artist extends FMObj implements Serializable, Cacheable {
/** /**
* *
@ -67,6 +69,11 @@ public class Artist extends FMObj implements Serializable {
return false; return false;
} }
@Override
public boolean matches(Object o) {
return equals(o);
}
public String toString() { public String toString() {
return "Artist: " + name; return "Artist: " + name;
} }

View File

@ -3,7 +3,9 @@ package sarsoo.fmframework.music;
import java.io.Serializable; import java.io.Serializable;
import java.util.ArrayList; import java.util.ArrayList;
public class Track extends FMObj implements Serializable { import sarsoo.fmframework.cache.Cacheable;
public class Track extends FMObj implements Serializable, Cacheable {
/** /**
* *
@ -93,6 +95,10 @@ public class Track extends FMObj implements Serializable {
return false; return false;
} }
@Override
public boolean matches(Object o) {
return equals(o);
}
public String toString() { public String toString() {
if (album != null) if (album != null)

View File

@ -3,6 +3,7 @@ package sarsoo.fmframework.util;
import java.io.Serializable; import java.io.Serializable;
import java.util.ArrayList; import java.util.ArrayList;
import sarsoo.fmframework.cache.Cacheable;
import sarsoo.fmframework.config.Config; import sarsoo.fmframework.config.Config;
import sarsoo.fmframework.fm.FmUserNetwork; import sarsoo.fmframework.fm.FmUserNetwork;
import sarsoo.fmframework.fx.FmFramework; import sarsoo.fmframework.fx.FmFramework;
@ -11,7 +12,7 @@ import sarsoo.fmframework.music.Artist;
import sarsoo.fmframework.music.FMObj; import sarsoo.fmframework.music.FMObj;
import sarsoo.fmframework.music.Track; import sarsoo.fmframework.music.Track;
public class FMObjList extends ArrayList<FMObj> implements Comparable<FMObjList>, Serializable { public class FMObjList extends ArrayList<FMObj> implements Comparable<FMObjList>, Serializable, Cacheable {
private static final long serialVersionUID = 1L; private static final long serialVersionUID = 1L;
@ -86,6 +87,24 @@ public class FMObjList extends ArrayList<FMObj> implements Comparable<FMObjList>
return getTotalUserScrobbles() - list.getTotalUserScrobbles(); 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() { public void refresh() {
Config config = FmFramework.getSessionConfig(); Config config = FmFramework.getSessionConfig();