From afed799dba6851b0d83c66fd29a141eff5bf267e Mon Sep 17 00:00:00 2001 From: Robert von Burg Date: Fri, 16 Jul 2021 15:39:24 +0200 Subject: [PATCH] [Minor] code cleanup in ObjectFilter --- .../utils/objectfilter/ObjectFilter.java | 317 ++++++++++++------ 1 file changed, 215 insertions(+), 102 deletions(-) diff --git a/li.strolch.utils/src/main/java/li/strolch/utils/objectfilter/ObjectFilter.java b/li.strolch.utils/src/main/java/li/strolch/utils/objectfilter/ObjectFilter.java index 93d6b3b63..baf008b34 100644 --- a/li.strolch.utils/src/main/java/li/strolch/utils/objectfilter/ObjectFilter.java +++ b/li.strolch.utils/src/main/java/li/strolch/utils/objectfilter/ObjectFilter.java @@ -17,6 +17,8 @@ package li.strolch.utils.objectfilter; import java.text.MessageFormat; import java.util.*; +import java.util.stream.Collectors; +import java.util.stream.Stream; import li.strolch.utils.collections.MapOfMaps; import org.slf4j.Logger; @@ -476,6 +478,105 @@ public class ObjectFilter { this.cache.removeElement(key, objectKey); } + /** + * Streams all objects with the given operation and key + * + * @param operation + * the operation to match + * @param key + * The registration key of the objects to match + * + * @return The list of all objects registered under the given key and that need to be added. + */ + public Stream streamFor(String key, Operation operation) { + if (!this.cache.containsMap(key)) + return Stream.empty(); + return this.cache.getAllElements(key).stream() // + .filter(objectCache -> objectCache.getOperation() == operation) // + .map(ObjectCache::getObject); + } + + /** + * Streams all objects with the given key + * + * @param key + * The registration key of the objects to match + * + * @return The list of all objects registered under the given key and that need to be added. + */ + public Stream streamFor(String key) { + if (!this.cache.containsMap(key)) + return Stream.empty(); + return this.cache.getAllElements(key).stream() // + .map(ObjectCache::getObject); + } + + /** + * Streams all objects with the given key + * + * @return The list of all objects registered under the given key and that need to be added. + */ + public Stream streamFor(Class clazz) { + return this.cache.stream().flatMap(m -> m.getValue().values().stream()) // + .filter(objectCache -> objectCache.getObject().getClass() == clazz) // + .map(objectCache -> { + @SuppressWarnings("unchecked") + V v = (V) objectCache.getObject(); + return v; + }); + } + + /** + * Streams all objects with the given key + * + * @param clazz + * the class to match + * @param key + * The registration key of the objects to match + * + * @return The list of all objects registered under the given key and that need to be added. + */ + public Stream streamFor(Class clazz, String key) { + if (!this.cache.containsMap(key)) + return Stream.empty(); + return this.cache.getAllElements(key).stream() // + .filter(objectCache -> objectCache.getObject().getClass() == clazz) // + .map(objectCache -> { + @SuppressWarnings("unchecked") + V v = (V) objectCache.getObject(); + return v; + }); + } + + /** + * Streams all objects with the given operation, key and clazz type + * + * @param key + * The registration key of the objects to match + * + * @return The list of all objects registered under the given key and that need to be added. + */ + public Stream streamFor(Class clazz, String key, Operation operation) { + return streamFor(key, operation).filter(o -> o.getClass() == clazz) // + .map(o -> { + @SuppressWarnings("unchecked") + V v = (V) o; + return v; + }); + } + + /** + * Get all objects that were registered under the given key and that have as a resulting final action an addition. + * + * @param key + * The registration key of the objects to match + * + * @return The list of all objects registered under the given key and that need to be added. + */ + public Stream streamAdded(String key) { + return streamFor(key, Operation.ADD); + } + /** * Get all objects that were registered under the given key and that have as a resulting final action an addition. * @@ -485,16 +586,21 @@ public class ObjectFilter { * @return The list of all objects registered under the given key and that need to be added. */ public List getAdded(String key) { - if (!this.cache.containsMap(key)) - return Collections.emptyList(); - List addedObjects = new LinkedList<>(); - List allObjs = this.cache.getAllElements(key); - for (ObjectCache objectCache : allObjs) { - if (objectCache.getOperation() == Operation.ADD) { - addedObjects.add(objectCache.getObject()); - } - } - return addedObjects; + return streamAdded(key).collect(Collectors.toList()); + } + + /** + * Get all objects that were registered under the given key and that have as a resulting final action an addition. + * + * @param clazz + * The class type of the object to be retrieved, that acts as an additional filter criterion. + * @param key + * The registration key of the objects to match + * + * @return The list of all objects registered under the given key and that need to be added. + */ + public Stream streamAdded(Class clazz, String key) { + return streamFor(clazz, key, Operation.ADD); } /** @@ -508,20 +614,19 @@ public class ObjectFilter { * @return The list of all objects registered under the given key and that need to be added. */ public List getAdded(Class clazz, String key) { - if (!this.cache.containsMap(key)) - return Collections.emptyList(); - List addedObjects = new LinkedList<>(); - List allObjs = this.cache.getAllElements(key); - for (ObjectCache objectCache : allObjs) { - if (objectCache.getOperation() == Operation.ADD) { - if (objectCache.getObject().getClass() == clazz) { - @SuppressWarnings("unchecked") - V object = (V) objectCache.getObject(); - addedObjects.add(object); - } - } - } - return addedObjects; + return streamAdded(clazz, key).collect(Collectors.toList()); + } + + /** + * Get all objects that were registered under the given key and that have as a resulting final action an update. + * + * @param key + * registration key of the objects to match + * + * @return The list of all objects registered under the given key and that need to be updated. + */ + public Stream streamUpdated(String key) { + return streamFor(key, Operation.MODIFY); } /** @@ -533,16 +638,21 @@ public class ObjectFilter { * @return The list of all objects registered under the given key and that need to be updated. */ public List getUpdated(String key) { - if (!this.cache.containsMap(key)) - return Collections.emptyList(); - List updatedObjects = new LinkedList<>(); - List allObjs = this.cache.getAllElements(key); - for (ObjectCache objectCache : allObjs) { - if (objectCache.getOperation() == Operation.MODIFY) { - updatedObjects.add(objectCache.getObject()); - } - } - return updatedObjects; + return streamUpdated(key).collect(Collectors.toList()); + } + + /** + * Get all objects that were registered under the given key and that have as a resulting final action an update. + * + * @param clazz + * The class type of the object to be retrieved, that acts as an additional filter criterion. + * @param key + * registration key of the objects to match + * + * @return The list of all objects registered under the given key and that need to be updated. + */ + public Stream streamUpdated(Class clazz, String key) { + return streamFor(clazz, key, Operation.MODIFY); } /** @@ -556,20 +666,19 @@ public class ObjectFilter { * @return The list of all objects registered under the given key and that need to be updated. */ public List getUpdated(Class clazz, String key) { - if (!this.cache.containsMap(key)) - return Collections.emptyList(); - List updatedObjects = new LinkedList<>(); - List allObjs = this.cache.getAllElements(key); - for (ObjectCache objectCache : allObjs) { - if (objectCache.getOperation() == Operation.MODIFY) { - if (objectCache.getObject().getClass() == clazz) { - @SuppressWarnings("unchecked") - V object = (V) objectCache.getObject(); - updatedObjects.add(object); - } - } - } - return updatedObjects; + return streamUpdated(clazz, key).collect(Collectors.toList()); + } + + /** + * Get all objects that were registered under the given key that have as a resulting final action their removal. + * + * @param key + * The registration key of the objects to match + * + * @return The list of object registered under the given key that have, as a final action, removal. + */ + public Stream streamRemoved(String key) { + return streamFor(key, Operation.REMOVE); } /** @@ -581,16 +690,21 @@ public class ObjectFilter { * @return The list of object registered under the given key that have, as a final action, removal. */ public List getRemoved(String key) { - if (!this.cache.containsMap(key)) - return Collections.emptyList(); - List removedObjects = new LinkedList<>(); - List allObjs = this.cache.getAllElements(key); - for (ObjectCache objectCache : allObjs) { - if (objectCache.getOperation() == Operation.REMOVE) { - removedObjects.add(objectCache.getObject()); - } - } - return removedObjects; + return streamRemoved(key).collect(Collectors.toList()); + } + + /** + * Get all objects that were registered under the given key that have as a resulting final action their removal. + * + * @param clazz + * The class type of the object to be retrieved, that acts as an additional filter criterion. + * @param key + * The registration key of the objects to match + * + * @return The list of object registered under the given key that have, as a final action, removal. + */ + public Stream streamRemoved(Class clazz, String key) { + return streamFor(clazz, key, Operation.REMOVE); } /** @@ -604,20 +718,21 @@ public class ObjectFilter { * @return The list of object registered under the given key that have, as a final action, removal. */ public List getRemoved(Class clazz, String key) { - if (!this.cache.containsMap(key)) - return Collections.emptyList(); - List removedObjects = new LinkedList<>(); - List allObjs = this.cache.getAllElements(key); - for (ObjectCache objectCache : allObjs) { - if (objectCache.getOperation() == Operation.REMOVE) { - if (objectCache.getObject().getClass() == clazz) { - @SuppressWarnings("unchecked") - V object = (V) objectCache.getObject(); - removedObjects.add(object); - } - } - } - return removedObjects; + return streamRemoved(clazz, key).collect(Collectors.toList()); + } + + /** + * Get all objects that were registered under the given key + * + * @param clazz + * The class type of the object to be retrieved, that acts as an additional filter criterion. + * @param key + * The registration key of the objects to match + * + * @return The list of object registered under the given key that have, as a final action, removal. + */ + public Stream streamAll(Class clazz, String key) { + return streamFor(clazz, key); } /** @@ -631,18 +746,19 @@ public class ObjectFilter { * @return The list of object registered under the given key that have, as a final action, removal. */ public List getAll(Class clazz, String key) { - if (!this.cache.containsMap(key)) - return Collections.emptyList(); - List objects = new LinkedList<>(); - List allObjs = this.cache.getAllElements(key); - for (ObjectCache objectCache : allObjs) { - if (objectCache.getObject().getClass() == clazz) { - @SuppressWarnings("unchecked") - V object = (V) objectCache.getObject(); - objects.add(object); - } - } - return objects; + return streamAll(clazz, key).collect(Collectors.toList()); + } + + /** + * Get all objects that of the given class + * + * @param clazz + * The class type of the object to be retrieved, that acts as an additional filter criterion. + * + * @return The list of all objects that of the given class + */ + public Stream streamAll(Class clazz) { + return streamFor(clazz); } /** @@ -654,16 +770,20 @@ public class ObjectFilter { * @return The list of all objects that of the given class */ public List getAll(Class clazz) { - List objects = new LinkedList<>(); - Collection allObjs = this.cache.getAllElements(); - for (ObjectCache objectCache : allObjs) { - if (objectCache.getObject().getClass() == clazz) { - @SuppressWarnings("unchecked") - V object = (V) objectCache.getObject(); - objects.add(object); - } - } - return objects; + return streamAll(clazz).collect(Collectors.toList()); + } + + /** + * Get all the objects that were processed in this transaction, that were registered under the given key. No action + * is associated to the object. + * + * @param key + * The registration key for which the objects shall be retrieved + * + * @return The list of objects matching the given key. + */ + public Stream streamAll(String key) { + return streamFor(key); } /** @@ -676,14 +796,7 @@ public class ObjectFilter { * @return The list of objects matching the given key. */ public List getAll(String key) { - if (!this.cache.containsMap(key)) - return Collections.emptyList(); - List allObjects = new LinkedList<>(); - List allObjs = this.cache.getAllElements(key); - for (ObjectCache objectCache : allObjs) { - allObjects.add(objectCache.getObject()); - } - return allObjects; + return streamAll(key).collect(Collectors.toList()); } /**