package com.github.swissquote.carnotzet.core.util;

import java.io.FileInputStream;
import java.io.FileWriter;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.Properties;
import java.util.function.Function;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.datatype.jsr310.JavaTimeModule;

import lombok.extern.slf4j.Slf4j;

@Slf4j
public class FileSystemCache<T> {

	private final Path cachePath;
	private final Properties cache;
	private final Class<T> deserializationType;
	private final ObjectMapper jsonMapper;

	public FileSystemCache(Path cachePath, Class<T> deserializationType) {
		this.cachePath = cachePath;
		this.deserializationType = deserializationType;
		this.jsonMapper = new ObjectMapper();
		this.jsonMapper.registerModule(new JavaTimeModule());
		cache = new Properties();
		if (Files.notExists(cachePath)) {
			try {
				Files.createFile(cachePath);
			}
			catch (Exception e) {
				throw new RuntimeException(e.getMessage(), e);
			}
		}
	}

	public void load() throws IOException {
		cache.load(new FileInputStream(cachePath.toFile()));
	}

	public T computeIfAbsent(String key, Function<String, String> mappingFunction) throws IOException {
		this.cache.load(new FileInputStream(this.cachePath.toFile()));

		String value = (String) this.cache.get(key);
		if(value == null)
		{
			value = mappingFunction.apply(key);
			this.cache.put(key, value);
			this.cache.store(new FileWriter(this.cachePath.toFile()), "image manifest cache");
		}

		T deserializedValue = this.jsonMapper.readValue(value, this.deserializationType);
		return deserializedValue;
	}
}

@@ -96,22 +96,28 @@
 .io.File
-Writer
+OutputStream
 ;%0Aimport
@@ -138,16 +138,107 @@
 eption;%0A
+import java.io.OutputStreamWriter;%0Aimport java.io.Writer;%0Aimport java.nio.charset.Charset;%0A
 import j
@@ -1182,27 +1182,60 @@
 ion %7B%0A%09%09
-cache.load(
+try (FileInputStream cacheFileInputStream = 
 new File
@@ -1246,16 +1246,21 @@
 tStream(
+this.
 cachePat
@@ -1271,17 +1271,59 @@
 File()))
-;
+ %7B%0A%09%09%09cache.load(cacheFileInputStream);%0A%09%09%7D
 %0A%09%7D%0A%0A%09pu
@@ -1427,31 +1427,146 @@
 %0A%09%09t
-his.cache.load(
+ry (FileInputStream cacheFileInputStream = new FileInputStream(this.cachePath.toFile()); Writer out = new OutputStreamWriter(%0A%09%09%09%09
 new File
 Inpu
@@ -1553,34 +1553,35 @@
 er(%0A%09%09%09%09new File
-In
+Out
 putStream(this.c
@@ -1602,12 +1602,96 @@
 e())
+, Charset.forName(%22UTF-8%22).newEncoder())) %7B%0A%09%09%09this.cache.load(cacheFileInputStream
 );%0A%0A
+%09
 %09%09St
@@ -1735,18 +1735,20 @@
 key);%0A%09%09
+%09
 if
+ 
 (value =
@@ -1758,13 +1758,12 @@
 ull)
-%0A%09%09
+ 
 %7B%0A
+%09
 %09%09%09v
@@ -1796,24 +1796,25 @@
 ly(key);%0A%09%09%09
+%09
 this.cache.p
@@ -1829,16 +1829,17 @@
 value);%0A
+%09
 %09%09%09this.
@@ -1854,47 +1854,11 @@
 ore(
-new FileWriter(this.cachePath.toFile())
+out
 , %22i
@@ -1882,19 +1882,21 @@
 he%22);%0A%09%09
+%09
 %7D%0A%0A
+%09
 %09%09T dese
@@ -1973,16 +1973,17 @@
 ype);%0A%09%09
+%09
 return d
@@ -2000,13 +2000,18 @@
 dValue;%0A
+%09%09%7D%0A%0A
 %09%7D%0A%7D%0A

Fix spotbugs alerts