ref #7709 use more generics for CdmCacher (not completed yet)
authorAndreas Müller <a.mueller@bgbm.org>
Thu, 30 Aug 2018 11:36:33 +0000 (13:36 +0200)
committerAndreas Müller <a.mueller@bgbm.org>
Thu, 30 Aug 2018 11:37:14 +0000 (13:37 +0200)
cdmlib-cache/src/main/java/eu/etaxonomy/cdm/cache/CacheLoader.java
cdmlib-cache/src/main/java/eu/etaxonomy/cdm/cache/CdmEntityCacheKey.java
cdmlib-cache/src/main/java/eu/etaxonomy/cdm/cache/CdmTransientEntityCacher.java

index 46468e351fbe46f25e775e8b70114732ca7ffa9d..15b3919e99908c7ec9a5bf1919ee07070bc02892 100644 (file)
@@ -252,23 +252,23 @@ public class CacheLoader {
             }
         }
 
-        CdmBase loadedCdmBase;
+        T loadedCdmBase;
         if(isRecursiveEnabled && recursive) {
             logger.debug("---- starting recursive load for cdm entity " + cdmEntity.getClass().getName() + " with id " + cdmEntity.getId());
             List<Object> alreadyVisitedEntities = new ArrayList<Object>();
-            CdmBase cb =  loadRecursive(cdmEntity, alreadyVisitedEntities, update);
+            T cb =  loadRecursive(cdmEntity, alreadyVisitedEntities, update);
             alreadyVisitedEntities.clear();
             logger.debug("---- ending recursive load for cdm entity " + cdmEntity.getClass().getName() + " with id " + cdmEntity.getId() + "\n");
             loadedCdmBase =  cb;
         } else {
             loadedCdmBase = load(cdmEntity);
         }
-        return (T) loadedCdmBase;
+        return loadedCdmBase;
 
     }
 
 
-    protected CdmBase load(CdmBase cdmEntity) {
+    protected <T extends CdmBase> T load(T cdmEntity) {
         logger.debug("loading object of type " + cdmEntity.getClass().getName() + " with id " + cdmEntity.getId());
         cdmCacher.put((CdmBase)ProxyUtils.deproxy(cdmEntity));
         return cdmCacher.getFromCache(cdmEntity);
@@ -293,15 +293,16 @@ public class CacheLoader {
      *            them to the value of the cdm entity being loaded
      * @return
      */
-    private CdmBase loadRecursive(CdmBase cdmEntity,  List<Object> alreadyVisitedEntities, boolean update) {
+    private <T extends CdmBase> T loadRecursive(T cdmEntity,  List<Object> alreadyVisitedEntities, boolean update) {
 
-        CdmBase cachedCdmEntity = load(cdmEntity);
+        T cachedCdmEntity = load(cdmEntity);
 
         // we want to recursive through the cdmEntity (and not the cachedCdmEntity)
         // since there could be new or deleted objects in the cdmEntity sub-graph
 
         // start by getting the fields from the cdm entity
-        CdmBase deproxiedEntity = (CdmBase)ProxyUtils.deproxyOrNull(cdmEntity);
+        //TODO improve generics for deproxyOrNull, probably need to split the method
+        T deproxiedEntity = (T)ProxyUtils.deproxyOrNull(cdmEntity);
         if(deproxiedEntity != null){
             String className = deproxiedEntity.getClass().getName();
             CdmModelFieldPropertyFromClass cmgmfc = getFromCdmlibModelCache(className);
index 9c18fbc6ca30a870cfc140d7ae5d7af20f5411e1..4589026911ac8c599b554f511671a7d9e692d7a8 100644 (file)
@@ -2,27 +2,28 @@ package eu.etaxonomy.cdm.cache;
 
 import eu.etaxonomy.cdm.model.common.CdmBase;
 
-public class CdmEntityCacheKey {
+public class CdmEntityCacheKey<T extends CdmBase> {
 
-       private Class<? extends CdmBase> persistenceClass;
+       private Class<T> persistenceClass;
        private int persistenceId;
-       
-       public CdmEntityCacheKey(CdmBase cdmBase) {
-               this.persistenceClass = cdmBase.getClass();
+
+
+       public CdmEntityCacheKey(T cdmBase) {
+               this.persistenceClass = (Class<T>)cdmBase.getClass();
                this.persistenceId = cdmBase.getId();
        }
-       
-       public CdmEntityCacheKey(Class<? extends CdmBase> clazz, int id) {
+
+       public CdmEntityCacheKey(Class<T> clazz, int id) {
                this.persistenceClass = clazz;
                this.persistenceId = id;
        }
-       
 
-       
-       public Class<? extends CdmBase> getPersistenceClass() {
+
+
+       public Class<? extends T> getPersistenceClass() {
                return persistenceClass;
        }
-       
+
        public int getPersistenceId() {
                return persistenceId;
        }
@@ -31,23 +32,23 @@ public class CdmEntityCacheKey {
                if(obj == null || !(obj instanceof CdmEntityCacheKey)) {
                        return false;
                }
-               
+
                if(this == obj) {
                        return true;
                }
-               CdmEntityCacheKey that = (CdmEntityCacheKey) obj;
+               CdmEntityCacheKey<T> that = (CdmEntityCacheKey<T>) obj;
                if(this.persistenceClass.equals(that.persistenceClass) && this.persistenceId == that.persistenceId) {
                        return true;
                }
-               
+
                return false;
        }
-       
+
        @Override
        public int hashCode() {
                return (this.persistenceClass.getName() + String.valueOf(this.persistenceId)).hashCode();
        }
-       
+
        @Override
        public String toString() {
                return this.persistenceClass.getName() + String.valueOf(this.persistenceId);
index 04d14c7adf8da414db342bf93829f09c2eb48ba4..c79d3a505873b81e2c27a9fa2ecc406d50e509be 100644 (file)
@@ -230,7 +230,7 @@ public class CdmTransientEntityCacher implements ICdmCacher {
             logger.info("Cdm Entity with id : " + cdmEntity.getId() + " already exists in permanent cache. Ignoring put.");
             return;
         }
-        CdmEntityCacheKey id = new CdmEntityCacheKey(cdmEntity);
+        CdmEntityCacheKey<?> id = new CdmEntityCacheKey<>(cdmEntity);
 
         cachedCdmEntity = getFromCache(id);
         if(cachedCdmEntity == null) {
@@ -250,39 +250,42 @@ public class CdmTransientEntityCacher implements ICdmCacher {
     }
 
 
-    private Element getCacheElement(CdmEntityCacheKey key) {
+    private Element getCacheElement(CdmEntityCacheKey<?> key) {
         return getCache().get(key);
     }
 
 
-    public CdmBase getFromCache(CdmEntityCacheKey id) {
+    public <T extends CdmBase> T getFromCache(CdmEntityCacheKey<T> id) {
         Element e = getCacheElement(id);
 
         if (e == null) {
             return null;
         } else {
-            return (CdmBase) e.getObjectValue();
+            @SuppressWarnings("unchecked")
+            T result = (T) e.getObjectValue();
+            return result;
         }
     }
 
-    public CdmBase getFromCache(Class<? extends CdmBase> clazz, int id) {
-        CdmEntityCacheKey cacheId = generateKey(clazz,id);
+    public <T extends CdmBase> T getFromCache(Class<T> clazz, int id) {
+        CdmEntityCacheKey<T> cacheId = generateKey(clazz, id);
         return getFromCache(cacheId);
     }
 
     @Override
     public <T extends CdmBase> T getFromCache(T cdmBase) {
 
-        CdmEntityCacheKey cacheId = generateKey((CdmBase)ProxyUtils.deproxy(cdmBase));
+        CdmEntityCacheKey<T> cacheId = generateKey((T)ProxyUtils.deproxy(cdmBase));
         // first try this cache
-        CdmBase  cachedCdmEntity = getFromCache(cacheId);
+        T  cachedCdmEntity = getFromCache(cacheId);
 
         if(cachedCdmEntity == null) {
             // ... then try the permanent cache
-            cachedCdmEntity = cdmCacher.getFromCache(cdmBase.getUuid());
+            //TODO also use generics and clazz parameter for getFromCache(uuid)
+            cachedCdmEntity = (T)cdmCacher.getFromCache(cdmBase.getUuid());
         }
 
-        return (T) cachedCdmEntity;
+        return cachedCdmEntity;
     }
 
     public CdmBase getFromCache(CdmBase cdmBase, Class<? extends CdmBase> clazz) {
@@ -300,11 +303,11 @@ public class CdmTransientEntityCacher implements ICdmCacher {
         return entities;
     }
 
-    public boolean exists(CdmEntityCacheKey key) {
+    public boolean exists(CdmEntityCacheKey<?> key) {
         return (getCacheElement(key) != null);
     }
 
-    public boolean existsAndIsNotNull(CdmEntityCacheKey id) {
+    public boolean existsAndIsNotNull(CdmEntityCacheKey<?> id) {
         return getFromCache(id) != null;
     }
 
@@ -320,15 +323,15 @@ public class CdmTransientEntityCacher implements ICdmCacher {
     }
 
 
-    public static CdmEntityCacheKey generateKey(Class<? extends CdmBase> clazz, int id) {
-        return new CdmEntityCacheKey(clazz, id);
+    public static <T extends CdmBase> CdmEntityCacheKey<T> generateKey(Class<T> clazz, int id) {
+        return new CdmEntityCacheKey<T>(clazz, id);
     }
 
 
-    public static CdmEntityCacheKey generateKey(CdmBase cdmBase) {
-        Class<? extends CdmBase> entityClass = cdmBase.getClass();
+    public static <T extends CdmBase> CdmEntityCacheKey<T> generateKey(T cdmBase) {
+        Class<T> entityClass = (Class<T>)cdmBase.getClass();
         int id = cdmBase.getId();
-        return new CdmEntityCacheKey(entityClass, id);
+        return new CdmEntityCacheKey<T>(entityClass, id);
     }
 
     @Override