[Minor] code cleanup in ObjectFilter

This commit is contained in:
Robert von Burg 2021-07-16 15:39:24 +02:00
parent 110569b895
commit afed799dba
1 changed files with 215 additions and 102 deletions

View File

@ -17,6 +17,8 @@ package li.strolch.utils.objectfilter;
import java.text.MessageFormat; import java.text.MessageFormat;
import java.util.*; import java.util.*;
import java.util.stream.Collectors;
import java.util.stream.Stream;
import li.strolch.utils.collections.MapOfMaps; import li.strolch.utils.collections.MapOfMaps;
import org.slf4j.Logger; import org.slf4j.Logger;
@ -476,6 +478,105 @@ public class ObjectFilter {
this.cache.removeElement(key, objectKey); 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<Object> 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<Object> 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 <V> Stream<V> streamFor(Class<V> 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 <V> Stream<V> streamFor(Class<V> 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 <V> Stream<V> streamFor(Class<V> 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<Object> 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. * 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. * @return The list of all objects registered under the given key and that need to be added.
*/ */
public List<Object> getAdded(String key) { public List<Object> getAdded(String key) {
if (!this.cache.containsMap(key)) return streamAdded(key).collect(Collectors.toList());
return Collections.emptyList(); }
List<Object> addedObjects = new LinkedList<>();
List<ObjectCache> allObjs = this.cache.getAllElements(key); /**
for (ObjectCache objectCache : allObjs) { * Get all objects that were registered under the given key and that have as a resulting final action an addition.
if (objectCache.getOperation() == Operation.ADD) { *
addedObjects.add(objectCache.getObject()); * @param clazz
} * The class type of the object to be retrieved, that acts as an additional filter criterion.
} * @param key
return addedObjects; * 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 <V> Stream<V> streamAdded(Class<V> 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. * @return The list of all objects registered under the given key and that need to be added.
*/ */
public <V> List<V> getAdded(Class<V> clazz, String key) { public <V> List<V> getAdded(Class<V> clazz, String key) {
if (!this.cache.containsMap(key)) return streamAdded(clazz, key).collect(Collectors.toList());
return Collections.emptyList(); }
List<V> addedObjects = new LinkedList<>();
List<ObjectCache> allObjs = this.cache.getAllElements(key); /**
for (ObjectCache objectCache : allObjs) { * Get all objects that were registered under the given key and that have as a resulting final action an update.
if (objectCache.getOperation() == Operation.ADD) { *
if (objectCache.getObject().getClass() == clazz) { * @param key
@SuppressWarnings("unchecked") * registration key of the objects to match
V object = (V) objectCache.getObject(); *
addedObjects.add(object); * @return The list of all objects registered under the given key and that need to be updated.
} */
} public Stream<Object> streamUpdated(String key) {
} return streamFor(key, Operation.MODIFY);
return addedObjects;
} }
/** /**
@ -533,16 +638,21 @@ public class ObjectFilter {
* @return The list of all objects registered under the given key and that need to be updated. * @return The list of all objects registered under the given key and that need to be updated.
*/ */
public List<Object> getUpdated(String key) { public List<Object> getUpdated(String key) {
if (!this.cache.containsMap(key)) return streamUpdated(key).collect(Collectors.toList());
return Collections.emptyList(); }
List<Object> updatedObjects = new LinkedList<>();
List<ObjectCache> allObjs = this.cache.getAllElements(key); /**
for (ObjectCache objectCache : allObjs) { * Get all objects that were registered under the given key and that have as a resulting final action an update.
if (objectCache.getOperation() == Operation.MODIFY) { *
updatedObjects.add(objectCache.getObject()); * @param clazz
} * The class type of the object to be retrieved, that acts as an additional filter criterion.
} * @param key
return updatedObjects; * 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 <V> Stream<V> streamUpdated(Class<V> 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. * @return The list of all objects registered under the given key and that need to be updated.
*/ */
public <V> List<V> getUpdated(Class<V> clazz, String key) { public <V> List<V> getUpdated(Class<V> clazz, String key) {
if (!this.cache.containsMap(key)) return streamUpdated(clazz, key).collect(Collectors.toList());
return Collections.emptyList(); }
List<V> updatedObjects = new LinkedList<>();
List<ObjectCache> allObjs = this.cache.getAllElements(key); /**
for (ObjectCache objectCache : allObjs) { * Get all objects that were registered under the given key that have as a resulting final action their removal.
if (objectCache.getOperation() == Operation.MODIFY) { *
if (objectCache.getObject().getClass() == clazz) { * @param key
@SuppressWarnings("unchecked") * The registration key of the objects to match
V object = (V) objectCache.getObject(); *
updatedObjects.add(object); * @return The list of object registered under the given key that have, as a final action, removal.
} */
} public Stream<Object> streamRemoved(String key) {
} return streamFor(key, Operation.REMOVE);
return updatedObjects;
} }
/** /**
@ -581,16 +690,21 @@ public class ObjectFilter {
* @return The list of object registered under the given key that have, as a final action, removal. * @return The list of object registered under the given key that have, as a final action, removal.
*/ */
public List<Object> getRemoved(String key) { public List<Object> getRemoved(String key) {
if (!this.cache.containsMap(key)) return streamRemoved(key).collect(Collectors.toList());
return Collections.emptyList(); }
List<Object> removedObjects = new LinkedList<>();
List<ObjectCache> allObjs = this.cache.getAllElements(key); /**
for (ObjectCache objectCache : allObjs) { * Get all objects that were registered under the given key that have as a resulting final action their removal.
if (objectCache.getOperation() == Operation.REMOVE) { *
removedObjects.add(objectCache.getObject()); * @param clazz
} * The class type of the object to be retrieved, that acts as an additional filter criterion.
} * @param key
return removedObjects; * 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 <V> Stream<V> streamRemoved(Class<V> 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. * @return The list of object registered under the given key that have, as a final action, removal.
*/ */
public <V> List<V> getRemoved(Class<V> clazz, String key) { public <V> List<V> getRemoved(Class<V> clazz, String key) {
if (!this.cache.containsMap(key)) return streamRemoved(clazz, key).collect(Collectors.toList());
return Collections.emptyList(); }
List<V> removedObjects = new LinkedList<>();
List<ObjectCache> allObjs = this.cache.getAllElements(key); /**
for (ObjectCache objectCache : allObjs) { * Get all objects that were registered under the given key
if (objectCache.getOperation() == Operation.REMOVE) { *
if (objectCache.getObject().getClass() == clazz) { * @param clazz
@SuppressWarnings("unchecked") * The class type of the object to be retrieved, that acts as an additional filter criterion.
V object = (V) objectCache.getObject(); * @param key
removedObjects.add(object); * 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.
} */
return removedObjects; public <V> Stream<V> streamAll(Class<V> 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. * @return The list of object registered under the given key that have, as a final action, removal.
*/ */
public <V> List<V> getAll(Class<V> clazz, String key) { public <V> List<V> getAll(Class<V> clazz, String key) {
if (!this.cache.containsMap(key)) return streamAll(clazz, key).collect(Collectors.toList());
return Collections.emptyList(); }
List<V> objects = new LinkedList<>();
List<ObjectCache> allObjs = this.cache.getAllElements(key); /**
for (ObjectCache objectCache : allObjs) { * Get all objects that of the given class
if (objectCache.getObject().getClass() == clazz) { *
@SuppressWarnings("unchecked") * @param clazz
V object = (V) objectCache.getObject(); * The class type of the object to be retrieved, that acts as an additional filter criterion.
objects.add(object); *
} * @return The list of all objects that of the given class
} */
return objects; public <V> Stream<V> streamAll(Class<V> clazz) {
return streamFor(clazz);
} }
/** /**
@ -654,16 +770,20 @@ public class ObjectFilter {
* @return The list of all objects that of the given class * @return The list of all objects that of the given class
*/ */
public <V> List<V> getAll(Class<V> clazz) { public <V> List<V> getAll(Class<V> clazz) {
List<V> objects = new LinkedList<>(); return streamAll(clazz).collect(Collectors.toList());
Collection<ObjectCache> allObjs = this.cache.getAllElements(); }
for (ObjectCache objectCache : allObjs) {
if (objectCache.getObject().getClass() == clazz) { /**
@SuppressWarnings("unchecked") * Get all the objects that were processed in this transaction, that were registered under the given key. No action
V object = (V) objectCache.getObject(); * is associated to the object.
objects.add(object); *
} * @param key
} * The registration key for which the objects shall be retrieved
return objects; *
* @return The list of objects matching the given key.
*/
public Stream<Object> streamAll(String key) {
return streamFor(key);
} }
/** /**
@ -676,14 +796,7 @@ public class ObjectFilter {
* @return The list of objects matching the given key. * @return The list of objects matching the given key.
*/ */
public List<Object> getAll(String key) { public List<Object> getAll(String key) {
if (!this.cache.containsMap(key)) return streamAll(key).collect(Collectors.toList());
return Collections.emptyList();
List<Object> allObjects = new LinkedList<>();
List<ObjectCache> allObjs = this.cache.getAllElements(key);
for (ObjectCache objectCache : allObjs) {
allObjects.add(objectCache.getObject());
}
return allObjects;
} }
/** /**