Applied suggestions from @kske

This commit is contained in:
Leon Hofmeister 2020-09-26 21:38:31 +02:00
parent 618a4aa3cf
commit 61fbeda05e
Signed by: delvh
GPG Key ID: 3DECE05F6D9A647C
5 changed files with 22 additions and 19 deletions

View File

@ -1,8 +1,7 @@
package envoy.client.data; package envoy.client.data;
import java.io.Serializable; import java.io.Serializable;
import java.util.HashMap; import java.util.*;
import java.util.Map;
/** /**
* Stores a heterogeneous map of {@link Cache} objects with different type * Stores a heterogeneous map of {@link Cache} objects with different type
@ -51,10 +50,8 @@ public final class CacheMap implements Serializable {
*/ */
public <T> Cache<? super T> getApplicable(Class<T> key) { public <T> Cache<? super T> getApplicable(Class<T> key) {
Cache<? super T> cache = get(key); Cache<? super T> cache = get(key);
if (cache == null) if (cache == null) for (final var e : map.entrySet())
for (var e : map.entrySet()) if (e.getKey().isAssignableFrom(key)) cache = (Cache<? super T>) e.getValue();
if (e.getKey().isAssignableFrom(key))
cache = (Cache<? super T>) e.getValue();
return cache; return cache;
} }
@ -63,4 +60,11 @@ public final class CacheMap implements Serializable {
* @since Envoy Client v0.1-beta * @since Envoy Client v0.1-beta
*/ */
public Map<Class<?>, Cache<?>> getMap() { return map; } public Map<Class<?>, Cache<?>> getMap() { return map; }
/**
* Clears the caches of this map of any values.
*
* @since Envoy Client v0.2-beta
*/
public void clear() { map.values().forEach(Cache::clear); }
} }

View File

@ -40,7 +40,7 @@ public final class LocalDB implements EventListener {
private CacheMap cacheMap = new CacheMap(); private CacheMap cacheMap = new CacheMap();
private String authToken; private String authToken;
// auto save Timer // Auto save timer
private Timer autoSaver; private Timer autoSaver;
private boolean autoSaveRestart = true; private boolean autoSaveRestart = true;
@ -172,8 +172,8 @@ public final class LocalDB implements EventListener {
*/ */
public void initAutoSave() { public void initAutoSave() {
if (autoSaveRestart) {
// A logout happened so the timer should be restarted // A logout happened so the timer should be restarted
if (autoSaveRestart) {
autoSaver = new Timer("LocalDB Autosave", true); autoSaver = new Timer("LocalDB Autosave", true);
autoSaveRestart = false; autoSaveRestart = false;
} }
@ -229,7 +229,7 @@ public final class LocalDB implements EventListener {
* @since Envoy Client v0.2-beta * @since Envoy Client v0.2-beta
*/ */
@Event(eventType = Logout.class, priority = 100) @Event(eventType = Logout.class, priority = 100)
public void onLogout() { private void onLogout() {
autoSaver.cancel(); autoSaver.cancel();
autoSaveRestart = true; autoSaveRestart = true;
lastLoginFile.delete(); lastLoginFile.delete();
@ -238,8 +238,7 @@ public final class LocalDB implements EventListener {
authToken = null; authToken = null;
chats.clear(); chats.clear();
lastSync = Instant.EPOCH; lastSync = Instant.EPOCH;
cacheMap.getMap().forEach((key, cache) -> cache.clear()); cacheMap.clear();
// cacheMap = new CacheMap();
} }
/** /**

View File

@ -5,7 +5,7 @@ import envoy.event.Event.Valueless;
/** /**
* Indicates that a logout has been requested. * Indicates that a logout has been requested.
* *
* @author leon * @author Leon Hofmeister
* @since Envoy Client v0.2-beta * @since Envoy Client v0.2-beta
*/ */
public final class Logout extends Valueless { public final class Logout extends Valueless {

View File

@ -10,7 +10,7 @@ import envoy.client.data.Settings;
* @author Leon Hofmeister * @author Leon Hofmeister
* @since Envoy Client v0.2-beta * @since Envoy Client v0.2-beta
*/ */
public class AlertHelper { public final class AlertHelper {
private AlertHelper() {} private AlertHelper() {}

View File

@ -13,12 +13,12 @@ import envoy.util.EnvoyLog;
import dev.kske.eventbus.EventBus; import dev.kske.eventbus.EventBus;
/** /**
* Contains methods that have a direct impact on the user. * Simplifies shutdown actions.
* *
* @author Leon Hofmeister * @author Leon Hofmeister
* @since Envoy Client v0.2-beta * @since Envoy Client v0.2-beta
*/ */
public class ShutdownHelper { public final class ShutdownHelper {
private ShutdownHelper() {} private ShutdownHelper() {}