From 28a5a5bee5ff8006ae3473fb5ff41fde71424297 Mon Sep 17 00:00:00 2001
From: Kai Reinhard <K.Reinhard@micromata.de>
Date: Sat, 08 Dec 2018 22:22:38 +0000
Subject: [PATCH] Tried to merge all caches to one EntityCache (failed due to Jackson-marshalling/unmarshalling problems with elements). I ended up in LinkedHashMaps instead of PoJos.

---
 borgbutler-core/src/main/java/de/micromata/borgbutler/cache/EntityCache.java |   26 ++++++++++++++++----------
 1 files changed, 16 insertions(+), 10 deletions(-)

diff --git a/borgbutler-core/src/main/java/de/micromata/borgbutler/cache/AbstractCache.java b/borgbutler-core/src/main/java/de/micromata/borgbutler/cache/EntityCache.java
similarity index 79%
rename from borgbutler-core/src/main/java/de/micromata/borgbutler/cache/AbstractCache.java
rename to borgbutler-core/src/main/java/de/micromata/borgbutler/cache/EntityCache.java
index 0b70220..0b8ef19 100644
--- a/borgbutler-core/src/main/java/de/micromata/borgbutler/cache/AbstractCache.java
+++ b/borgbutler-core/src/main/java/de/micromata/borgbutler/cache/EntityCache.java
@@ -2,6 +2,7 @@
 
 import com.fasterxml.jackson.annotation.JsonIgnore;
 import com.fasterxml.jackson.annotation.JsonProperty;
+import com.fasterxml.jackson.core.type.TypeReference;
 import de.micromata.borgbutler.json.JsonUtils;
 import de.micromata.borgbutler.json.borg.RepositoryMatcher;
 import lombok.Getter;
@@ -16,16 +17,20 @@
 import java.util.ArrayList;
 import java.util.List;
 
-public abstract class AbstractCache<T> {
-    private static Logger log = LoggerFactory.getLogger(AbstractCache.class);
+public class EntityCache<T> {
+    private static Logger log = LoggerFactory.getLogger(EntityCache.class);
     private static final String CACHE_FILE_PREFIX = "cache-";
     private static final String CACHE_FILE_EXTENSION = "json";
+    public static final String CACHE_REPO_INFOS_BASENAME = "repo-infos";
+    public static final String CACHE_REPO_LISTS_BASENAME = "repo-lists";
+    public static final String CACHE_ARCHIVE_LISTS_BASENAME = "archive-lists";
 
     @JsonIgnore
     protected File cacheFile;
     @Getter
     @JsonProperty
     private List<T> elements = new ArrayList<>();
+    private TypeReference<T> typeReference;
 
     public T get(String identifier) {
         if (identifier == null) {
@@ -41,21 +46,21 @@
 
     public boolean matches(T element, String identifier) {
         if (!(element instanceof RepositoryMatcher)) {
-            throw new UnsupportedOperationException("matches not implemented, only available for RepositoryMatcher: " + this.getClass());
+            throw new UnsupportedOperationException("matches not implemented, only available for RepositoryMatcher: " + element.getClass());
         }
         return ((RepositoryMatcher) element).matches(identifier);
     }
 
     public String getIdentifier(T element) {
         if (!(element instanceof RepositoryMatcher)) {
-            throw new UnsupportedOperationException("matches not implemented, only available for RepositoryMatcher: " + this.getClass());
+            throw new UnsupportedOperationException("matches not implemented, only available for RepositoryMatcher: " + element.getClass());
         }
         return ((RepositoryMatcher)element).getRepository().getId();
     }
 
     public void updateFrom(T dest, T source) {
         if (!(dest instanceof RepositoryMatcher)) {
-            throw new UnsupportedOperationException("matches not implemented, only available for RepositoryMatcher: " + this.getClass());
+            throw new UnsupportedOperationException("matches not implemented, only available for RepositoryMatcher: " + dest.getClass());
         }
         ((RepositoryMatcher)dest).updateFrom(((RepositoryMatcher)source));
     }
@@ -84,9 +89,9 @@
             }
             log.info("Parsing cache file '" + cacheFile.getAbsolutePath() + "'.");
             String json = FileUtils.readFileToString(cacheFile, Charset.forName("UTF-8"));
-            AbstractCache readCache = JsonUtils.fromJson(this.getClass(), json);
-            if (readCache != null) {
-                this.elements = readCache.elements;
+            List<T> elements = (List<T>)JsonUtils.fromJson(typeReference, json);
+            if (elements != null) {
+                this.elements = elements;
             } else {
                 log.error("Error while parsing cache: " + cacheFile.getAbsolutePath());
             }
@@ -110,11 +115,12 @@
     /**
      * Needed by jackson for deserialization.
      */
-    AbstractCache() {
+    EntityCache() {
     }
 
-    AbstractCache(File cacheDir, String cacheFilename) {
+    EntityCache(File cacheDir, String cacheFilename, TypeReference typeReference) {
         cacheFile = new File(cacheDir, CACHE_FILE_PREFIX + cacheFilename + "." + CACHE_FILE_EXTENSION);
+        this.typeReference = typeReference;
     }
 
     public static boolean isCacheFile(File file) {

--
Gitblit v1.10.0