[Minor] Code cleanup in ObjectFilter and ObjectCache

This commit is contained in:
Robert von Burg 2023-04-17 14:37:09 +02:00
parent b515399957
commit 9eb10b860b
Signed by: eitch
GPG Key ID: 75DB9C85C74331F7
2 changed files with 24 additions and 57 deletions

View File

@ -71,18 +71,11 @@ public class ObjectCache {
private Object object;
public ObjectCache(long id, String key, Object objectKey, Object object, Operation operation) {
this.id = id;
this.key = key;
this.objectKey = objectKey;
this.object = object;
this.operation = operation;
if (logger.isDebugEnabled()) {
String sb = "Instanciated Cache: ID" + this.id + " / " + key + " OP: " + this.operation + " / "
+ objectKey.toString();
logger.debug(sb);
}
}
/**
@ -92,10 +85,6 @@ public class ObjectCache {
* the object to set
*/
public void setObject(Object object) {
if (logger.isDebugEnabled()) {
logger.debug(
MessageFormat.format("Updating ID {0} to value {1}", this.id, object.toString()));
}
this.object = object;
}
@ -106,11 +95,6 @@ public class ObjectCache {
* the operation to set
*/
public void setOperation(Operation newOperation) {
if (logger.isDebugEnabled()) {
String msg = "Updating Operation of ID {0} from {1} to {2}";
msg = MessageFormat.format(msg, this.id, this.operation, newOperation);
logger.debug(msg);
}
this.operation = newOperation;
}

View File

@ -16,8 +16,10 @@
package li.strolch.utils.objectfilter;
import java.text.MessageFormat;
import java.util.*;
import java.util.stream.Collectors;
import java.util.Collections;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
import java.util.stream.Stream;
import li.strolch.utils.collections.MapOfMaps;
@ -97,11 +99,6 @@ public class ObjectFilter {
private void replaceKey(ObjectCache cached, Object newObjectKey, Object newObject) {
if (cached.getObjectKey() != newObjectKey) {
if (ObjectFilter.logger.isDebugEnabled()) {
String msg = "Replacing key for object as they are not the same reference: old: {0} / new: {1}";
msg = MessageFormat.format(msg, cached.getObjectKey(), newObjectKey);
ObjectFilter.logger.warn(msg);
}
ObjectCache objectCache = this.cache.removeElement(cached.getKey(), cached.getObjectKey());
this.cache.addElement(objectCache.getKey(), newObjectKey, objectCache);
}
@ -137,10 +134,6 @@ public class ObjectFilter {
*/
public void add(String key, Object objectKey, Object objectToAdd) {
if (ObjectFilter.logger.isDebugEnabled())
ObjectFilter.logger
.debug(MessageFormat.format("add object {0} with key {1}", objectToAdd, key));
// BEWARE: you fix a bug here, be sure to update BOTH tables on the logic.
ObjectCache cached = this.cache.getElement(key, objectKey);
if (cached == null) {
@ -155,8 +148,8 @@ public class ObjectFilter {
String existingKey = cached.getKey();
if (!existingKey.equals(key)) {
String msg = "Invalid key provided for object with transaction ID {0} and operation {1}: existing key is {2}, new key is {3}. Object may be present in the same filter instance only once, registered using one key only. Object:{4}";
throw new IllegalArgumentException(MessageFormat
.format(msg, Long.toString(id), Operation.ADD.toString(), existingKey, key,
throw new IllegalArgumentException(
MessageFormat.format(msg, Long.toString(id), Operation.ADD.toString(), existingKey, key,
objectKey.toString()));
}
@ -164,10 +157,9 @@ public class ObjectFilter {
// of the cases here will be mistakes...
Operation op = cached.getOperation();
switch (op) {
case ADD -> throw new IllegalStateException(
"Stale State exception: Invalid + after + for " + objectKey);
case MODIFY -> throw new IllegalStateException(
"Stale State exception: Invalid + after += for " + objectKey);
case ADD -> throw new IllegalStateException("Stale State exception: Invalid + after + for " + objectKey);
case MODIFY ->
throw new IllegalStateException("Stale State exception: Invalid + after += for " + objectKey);
case REMOVE -> {
// replace key if necessary
replaceKey(cached, objectKey, objectToAdd);
@ -213,10 +205,6 @@ public class ObjectFilter {
*/
public void update(String key, Object objectKey, Object objectToUpdate) {
if (ObjectFilter.logger.isDebugEnabled())
ObjectFilter.logger
.debug(MessageFormat.format("update object {0} with key {1}", objectKey, key));
// BEWARE: you fix a bug here, be sure to update BOTH tables on the logic.
ObjectCache cached = this.cache.getElement(key, objectKey);
@ -239,8 +227,8 @@ public class ObjectFilter {
// update operation's object
cached.setObject(objectToUpdate);
}
case REMOVE -> throw new IllegalStateException(
"Stale State exception: Invalid += after - for " + objectKey);
case REMOVE ->
throw new IllegalStateException("Stale State exception: Invalid += after - for " + objectKey);
default -> throw new IllegalStateException("Stale State exception: Unhandled state " + op);
} // switch
} // else of object not in cache
@ -278,10 +266,6 @@ public class ObjectFilter {
*/
public void remove(String key, Object objectKey, Object objectToRemove) {
if (ObjectFilter.logger.isDebugEnabled())
ObjectFilter.logger
.debug(MessageFormat.format("remove object {0} with key {1}", objectKey, key));
// BEWARE: you fix a bug here, be sure to update BOTH tables on the logic.
ObjectCache cached = this.cache.getElement(key, objectKey);
if (cached == null) {
@ -294,8 +278,8 @@ public class ObjectFilter {
String existingKey = cached.getKey();
if (!existingKey.equals(key)) {
String msg = "Invalid key provided for object with transaction ID {0} and operation {1}: existing key is {2}, new key is {3}. Object may be present in the same filter instance only once, registered using one key only. Object:{4}";
throw new IllegalArgumentException(MessageFormat
.format(msg, Long.toString(id), Operation.REMOVE.toString(), existingKey, key,
throw new IllegalArgumentException(
MessageFormat.format(msg, Long.toString(id), Operation.REMOVE.toString(), existingKey, key,
objectKey.toString()));
}
@ -314,8 +298,7 @@ public class ObjectFilter {
cached.setObject(objectToRemove);
cached.setOperation(Operation.REMOVE);
}
case REMOVE -> throw new IllegalStateException(
"Stale State exception: Invalid - after - for " + objectKey);
case REMOVE -> throw new IllegalStateException("Stale State exception: Invalid - after - for " + objectKey);
default -> throw new IllegalStateException("Stale State exception: Unhandled state " + op);
} // switch
}
@ -577,7 +560,7 @@ public class ObjectFilter {
* @return The list of all objects registered under the given key and that need to be added.
*/
public List<Object> getAdded(String key) {
return streamAdded(key).collect(Collectors.toList());
return streamAdded(key).toList();
}
/**
@ -605,7 +588,7 @@ public class ObjectFilter {
* @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) {
return streamAdded(clazz, key).collect(Collectors.toList());
return streamAdded(clazz, key).toList();
}
/**
@ -629,7 +612,7 @@ public class ObjectFilter {
* @return The list of all objects registered under the given key and that need to be updated.
*/
public List<Object> getUpdated(String key) {
return streamUpdated(key).collect(Collectors.toList());
return streamUpdated(key).toList();
}
/**
@ -657,7 +640,7 @@ public class ObjectFilter {
* @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) {
return streamUpdated(clazz, key).collect(Collectors.toList());
return streamUpdated(clazz, key).toList();
}
/**
@ -681,7 +664,7 @@ public class ObjectFilter {
* @return The list of object registered under the given key that have, as a final action, removal.
*/
public List<Object> getRemoved(String key) {
return streamRemoved(key).collect(Collectors.toList());
return streamRemoved(key).toList();
}
/**
@ -709,7 +692,7 @@ public class ObjectFilter {
* @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) {
return streamRemoved(clazz, key).collect(Collectors.toList());
return streamRemoved(clazz, key).toList();
}
/**
@ -737,7 +720,7 @@ public class ObjectFilter {
* @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) {
return streamAll(clazz, key).collect(Collectors.toList());
return streamAll(clazz, key).toList();
}
/**
@ -761,7 +744,7 @@ public class ObjectFilter {
* @return The list of all objects that of the given class
*/
public <V> List<V> getAll(Class<V> clazz) {
return streamAll(clazz).collect(Collectors.toList());
return streamAll(clazz).toList();
}
/**
@ -787,7 +770,7 @@ public class ObjectFilter {
* @return The list of objects matching the given key.
*/
public List<Object> getAll(String key) {
return streamAll(key).collect(Collectors.toList());
return streamAll(key).toList();
}
/**
@ -846,7 +829,7 @@ public class ObjectFilter {
/**
* @return get a unique transaction ID
*/
public synchronized long dispenseID() {
private static synchronized long dispenseID() {
ObjectFilter.id++;
if (ObjectFilter.id == Long.MAX_VALUE) {
ObjectFilter.logger.error("Rolling IDs of objectFilter back to 1. Hope this is fine.");