From 76536ed2799c4a2974924ab3ff4c34799d567987 Mon Sep 17 00:00:00 2001 From: kske Date: Thu, 9 Jul 2020 10:53:27 +0200 Subject: [PATCH] Add subclass consumption for Cache with CacheMap#getApplicable --- src/main/java/envoy/client/data/Cache.java | 2 +- src/main/java/envoy/client/data/CacheMap.java | 22 +++++++++- src/main/java/envoy/client/data/LocalDB.java | 40 ++++++------------- .../envoy/client/data/PersistentLocalDB.java | 9 ++--- .../java/envoy/client/net/WriteProxy.java | 15 +++---- .../client/ui/controller/LoginScene.java | 4 +- 6 files changed, 46 insertions(+), 46 deletions(-) diff --git a/src/main/java/envoy/client/data/Cache.java b/src/main/java/envoy/client/data/Cache.java index 786e96b..25b985e 100644 --- a/src/main/java/envoy/client/data/Cache.java +++ b/src/main/java/envoy/client/data/Cache.java @@ -20,7 +20,7 @@ import envoy.util.EnvoyLog; * @author Kai S. K. Engelbart * @since Envoy Client v0.3-alpha */ -public class Cache implements Consumer, Serializable { +public final class Cache implements Consumer, Serializable { private final Queue elements = new LinkedList<>(); private transient Consumer processor; diff --git a/src/main/java/envoy/client/data/CacheMap.java b/src/main/java/envoy/client/data/CacheMap.java index 462b966..8c1fcb2 100644 --- a/src/main/java/envoy/client/data/CacheMap.java +++ b/src/main/java/envoy/client/data/CacheMap.java @@ -1,5 +1,6 @@ package envoy.client.data; +import java.io.Serializable; import java.util.HashMap; import java.util.Map; @@ -14,10 +15,12 @@ import java.util.Map; * @author Kai S. K. Engelbart * @since Envoy Client v0.1-beta */ -public final class CacheMap { +public final class CacheMap implements Serializable { private final Map, Cache> map = new HashMap<>(); + private static final long serialVersionUID = 1L; + /** * Adds a cache to the map. * @@ -38,6 +41,23 @@ public final class CacheMap { */ public Cache get(Class key) { return (Cache) map.get(key); } + /** + * Returns a cache mapped by a class or any of its subclasses. + * + * @param the type accepted by the cache + * @param key the class that maps to the cache + * @return the cache + * @since Envoy Client v0.1-beta + */ + public Cache getApplicable(Class key) { + Cache cache = get(key); + if (cache == null) + for (var e : map.entrySet()) + if (e.getKey().isAssignableFrom(key)) + cache = (Cache) e.getValue(); + return cache; + } + /** * @return the map in which the caches are stored * @since Envoy Client v0.1-beta diff --git a/src/main/java/envoy/client/data/LocalDB.java b/src/main/java/envoy/client/data/LocalDB.java index 7899510..421cb38 100644 --- a/src/main/java/envoy/client/data/LocalDB.java +++ b/src/main/java/envoy/client/data/LocalDB.java @@ -20,12 +20,16 @@ import envoy.event.NameChange; */ public abstract class LocalDB { - protected User user; - protected Map users = new HashMap<>(); - protected List chats = new ArrayList<>(); - protected IDGenerator idGenerator; - protected Cache messageCache = new Cache<>(); - protected Cache statusCache = new Cache<>(); + protected User user; + protected Map users = new HashMap<>(); + protected List chats = new ArrayList<>(); + protected IDGenerator idGenerator; + protected CacheMap cacheMap = new CacheMap(); + + { + cacheMap.put(Message.class, new Cache<>()); + cacheMap.put(MessageStatusChange.class, new Cache<>()); + } /** * Initializes a storage space for a user-specific list of chats. @@ -139,28 +143,10 @@ public abstract class LocalDB { public boolean hasIDGenerator() { return idGenerator != null; } /** - * @return the offline message cache - * @since Envoy Client v0.3-alpha + * @return the cache map for messages and message status changes + * @since Envoy Client v0.1-beta */ - public Cache getMessageCache() { return messageCache; } - - /** - * @param messageCache the offline message cache to set - * @since Envoy Client v0.3-alpha - */ - public void setMessageCache(Cache messageCache) { this.messageCache = messageCache; } - - /** - * @return the offline status cache - * @since Envoy Client v0.3-alpha - */ - public Cache getStatusCache() { return statusCache; } - - /** - * @param statusCache the offline status cache to set - * @since Envoy Client v0.3-alpha - */ - public void setStatusCache(Cache statusCache) { this.statusCache = statusCache; } + public CacheMap getCacheMap() { return cacheMap; } /** * Searches for a message by ID. diff --git a/src/main/java/envoy/client/data/PersistentLocalDB.java b/src/main/java/envoy/client/data/PersistentLocalDB.java index cc6cad9..182a4c8 100644 --- a/src/main/java/envoy/client/data/PersistentLocalDB.java +++ b/src/main/java/envoy/client/data/PersistentLocalDB.java @@ -5,8 +5,6 @@ import java.util.ArrayList; import java.util.HashMap; import envoy.data.IDGenerator; -import envoy.data.Message; -import envoy.event.MessageStatusChange; import envoy.util.SerializationUtils; /** @@ -64,7 +62,7 @@ public final class PersistentLocalDB extends LocalDB { SerializationUtils.write(usersFile, users); // Save user data - if (user != null) SerializationUtils.write(userFile, chats, messageCache, statusCache); + if (user != null) SerializationUtils.write(userFile, chats, cacheMap); // Save id generator if (hasIDGenerator()) SerializationUtils.write(idGeneratorFile, idGenerator); @@ -76,9 +74,8 @@ public final class PersistentLocalDB extends LocalDB { @Override public void loadUserData() throws ClassNotFoundException, IOException { try (var in = new ObjectInputStream(new FileInputStream(userFile))) { - chats = (ArrayList) in.readObject(); - messageCache = (Cache) in.readObject(); - statusCache = (Cache) in.readObject(); + chats = (ArrayList) in.readObject(); + cacheMap = (CacheMap) in.readObject(); } } diff --git a/src/main/java/envoy/client/net/WriteProxy.java b/src/main/java/envoy/client/net/WriteProxy.java index 339f1f2..0eccf6a 100644 --- a/src/main/java/envoy/client/net/WriteProxy.java +++ b/src/main/java/envoy/client/net/WriteProxy.java @@ -4,6 +4,7 @@ import java.io.IOException; import java.util.logging.Level; import java.util.logging.Logger; +import envoy.client.data.Cache; import envoy.client.data.LocalDB; import envoy.data.Message; import envoy.event.MessageStatusChange; @@ -43,7 +44,7 @@ public class WriteProxy { this.localDB = localDB; // Initialize cache processors for messages and message status change events - localDB.getMessageCache().setProcessor(msg -> { + localDB.getCacheMap().get(Message.class).setProcessor(msg -> { try { logger.log(Level.FINER, "Sending cached " + msg); client.sendMessage(msg); @@ -51,7 +52,7 @@ public class WriteProxy { logger.log(Level.SEVERE, "Could not send cached message: ", e); } }); - localDB.getStatusCache().setProcessor(evt -> { + localDB.getCacheMap().get(MessageStatusChange.class).setProcessor(evt -> { logger.log(Level.FINER, "Sending cached " + evt); try { client.sendEvent(evt); @@ -68,11 +69,7 @@ public class WriteProxy { * @since Envoy Client v0.3-alpha */ public void flushCache() { - // Send messages - localDB.getMessageCache().relay(); - - // Send message status change events - localDB.getStatusCache().relay(); + localDB.getCacheMap().getMap().values().forEach(Cache::relay); } /** @@ -85,7 +82,7 @@ public class WriteProxy { */ public void writeMessage(Message message) throws IOException { if (client.isOnline()) client.sendMessage(message); - else localDB.getMessageCache().accept(message); + else localDB.getCacheMap().getApplicable(Message.class).accept(message); } /** @@ -98,6 +95,6 @@ public class WriteProxy { */ public void writeMessageStatusChange(MessageStatusChange evt) throws IOException { if (client.isOnline()) client.sendEvent(evt); - else localDB.getStatusCache().accept(evt); + else localDB.getCacheMap().getApplicable(MessageStatusChange.class).accept(evt); } } diff --git a/src/main/java/envoy/client/ui/controller/LoginScene.java b/src/main/java/envoy/client/ui/controller/LoginScene.java index 7d7ba99..7dfb467 100644 --- a/src/main/java/envoy/client/ui/controller/LoginScene.java +++ b/src/main/java/envoy/client/ui/controller/LoginScene.java @@ -196,7 +196,7 @@ public final class LoginScene { sceneContext.load(SceneContext.SceneInfo.CHAT_SCENE); sceneContext.getController().initializeData(sceneContext, localDB, client, writeProxy); - // Relay unread messages from cache - if (client.isOnline()) cacheMap.getMap().values().forEach(cache -> { if (cache != null) cache.relay(); }); + // Relay the caches if online + if (client.isOnline()) cacheMap.getMap().values().forEach(Cache::relay); } }