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
@ -11,7 +10,7 @@ import java.util.Map;
* Project: <strong>envoy-client</strong><br> * Project: <strong>envoy-client</strong><br>
* File: <strong>CacheMap.java</strong><br> * File: <strong>CacheMap.java</strong><br>
* Created: <strong>09.07.2020</strong><br> * Created: <strong>09.07.2020</strong><br>
* *
* @author Kai S. K. Engelbart * @author Kai S. K. Engelbart
* @since Envoy Client v0.1-beta * @since Envoy Client v0.1-beta
*/ */
@ -23,7 +22,7 @@ public final class CacheMap implements Serializable {
/** /**
* Adds a cache to the map. * Adds a cache to the map.
* *
* @param <T> the type accepted by the cache * @param <T> the type accepted by the cache
* @param key the class that maps to the cache * @param key the class that maps to the cache
* @param cache the cache to store * @param cache the cache to store
@ -33,7 +32,7 @@ public final class CacheMap implements Serializable {
/** /**
* Returns a cache mapped by a class. * Returns a cache mapped by a class.
* *
* @param <T> the type accepted by the cache * @param <T> the type accepted by the cache
* @param key the class that maps to the cache * @param key the class that maps to the cache
* @return the cache * @return the cache
@ -43,7 +42,7 @@ public final class CacheMap implements Serializable {
/** /**
* Returns a cache mapped by a class or any of its subclasses. * Returns a cache mapped by a class or any of its subclasses.
* *
* @param <T> the type accepted by the cache * @param <T> the type accepted by the cache
* @param key the class that maps to the cache * @param key the class that maps to the cache
* @return the cache * @return the cache
@ -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() {
// A logout happened so the timer should be restarted
if (autoSaveRestart) { if (autoSaveRestart) {
// A logout happened so the timer should be restarted
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() {}