Some clean code refactorings

Signed-off-by: SirWayne <dennis.melzer@bosch-si.com>
This commit is contained in:
SirWayne
2016-05-19 17:28:45 +02:00
parent 0f75c63878
commit 79d604ca0b
10 changed files with 102 additions and 105 deletions

View File

@@ -32,9 +32,6 @@ import org.springframework.hateoas.Identifiable;
* by this entity listener cause the cache keys are calculated with the ID of
* the entity.
*
*
*
*
*/
public class CacheFieldEntityListener {
@@ -48,24 +45,21 @@ public class CacheFieldEntityListener {
*/
@PostLoad
public void postLoad(final Object target) {
if (target instanceof Identifiable) {
final CacheManager cacheManager = CacheManagerHolder.getInstance().getCacheManager();
@SuppressWarnings("rawtypes")
final String id = ((Identifiable) target).getId().toString();
final Class<? extends Object> type = target.getClass();
findCacheFields(type, id, new CacheFieldCallback() {
@Override
public void fromCache(final Field field, final String cacheKey, final Serializable id)
throws IllegalAccessException {
final Cache cache = cacheManager.getCache(type.getName());
final ValueWrapper valueWrapper = cache
.get(CacheKeys.entitySpecificCacheKey(id.toString(), cacheKey));
if (valueWrapper != null && valueWrapper.get() != null) {
FieldUtils.writeField(field, target, valueWrapper.get(), true);
}
}
});
if (!isIdentifiable(target)) {
return;
}
final CacheFieldCallback cacheFieldCallback = (type, field, cacheKey, entityId) -> {
final CacheManager cacheManager = CacheManagerHolder.getInstance().getCacheManager();
final Cache cache = cacheManager.getCache(type.getName());
final ValueWrapper valueWrapper = cache
.get(CacheKeys.entitySpecificCacheKey(entityId.toString(), cacheKey));
if (valueWrapper != null && valueWrapper.get() != null) {
FieldUtils.writeField(field, target, valueWrapper.get(), true);
}
};
findCacheFields((Identifiable<?>) target, cacheFieldCallback);
}
/**
@@ -77,30 +71,32 @@ public class CacheFieldEntityListener {
*/
@PostRemove
public void postDelete(final Object target) {
if (target instanceof Identifiable) {
final CacheManager cacheManager = CacheManagerHolder.getInstance().getCacheManager();
@SuppressWarnings("rawtypes")
final String id = ((Identifiable) target).getId().toString();
final Class<? extends Object> type = target.getClass();
findCacheFields(type, id, new CacheFieldCallback() {
@Override
public void fromCache(final Field field, final String cacheKey, final Serializable id)
throws IllegalAccessException {
final Cache cache = cacheManager.getCache(type.getName());
cache.evict(CacheKeys.entitySpecificCacheKey(id.toString(), cacheKey));
}
});
if (!isIdentifiable(target)) {
return;
}
final CacheFieldCallback cacheFieldCallback = (type, field, cacheKey, entityId) -> {
final CacheManager cacheManager = CacheManagerHolder.getInstance().getCacheManager();
final Cache cache = cacheManager.getCache(type.getName());
cache.evict(CacheKeys.entitySpecificCacheKey(entityId.toString(), cacheKey));
};
findCacheFields((Identifiable<?>) target, cacheFieldCallback);
}
private void findCacheFields(final Class<? extends Object> type, final Serializable id,
final CacheFieldCallback callback) {
private boolean isIdentifiable(final Object target) {
return target instanceof Identifiable;
}
private void findCacheFields(final Identifiable<?> target, final CacheFieldCallback callback) {
final String id = target.getId().toString();
final Class<?> type = target.getClass();
final Field[] declaredFields = type.getDeclaredFields();
for (final Field field : declaredFields) {
if (field.getAnnotation(CacheField.class) != null) {
try {
final CacheField annotation = field.getAnnotation(CacheField.class);
callback.fromCache(field, annotation.key(), id);
callback.fromCache(type, field, annotation.key(), id);
} catch (final IllegalAccessException e) {
LOGGER.error("cannot access the field {} for the entity {}, ignoring the cacheable field", field,
type, e);
@@ -109,23 +105,26 @@ public class CacheFieldEntityListener {
}
}
@FunctionalInterface
private interface CacheFieldCallback {
/**
* callback methods which is called by the
* {@link CacheFieldEntityListener#findCacheFields(Class, Serializable, CacheFieldCallback)}
* in case a field is annotated with {@link CacheField}.
* callback methods which is called when a field is annotated with
* {@link CacheField}.
*
* @param type
* the type of the target.
* @param field
* the field which is annotaed with {@link CacheField}
* @param cacheKey
* the configured cache key in the annotation
* {@link CacheField#key()}
* @param id
* @param entityId
* the ID of the entity
* @throws IllegalAccessException
* in case the field cannot be accessed
*/
void fromCache(final Field field, final String cacheKey, Serializable id) throws IllegalAccessException;
void fromCache(Class<?> type, final Field field, final String cacheKey, Serializable entityId)
throws IllegalAccessException;
}
}