package org.fakekoji.storage;

import com.fasterxml.jackson.databind.ObjectMapper;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.IOException;
import java.io.PrintWriter;
import java.nio.file.Paths;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

public class DirectoryJsonStorage<T> implements Storage<T> {

    private static final String SUFFIX= ".json";
    private final File storageFile;

    DirectoryJsonStorage(File storageFile) {
        this.storageFile = storageFile;
    }

    @Override
    public void store(String id, T t) throws StorageException {
        try {
            final File file = Paths.get(storageFile.getAbsolutePath(), id + SUFFIX).toFile();
            final PrintWriter writer = new PrintWriter(file.getAbsolutePath(), "UTF-8");
            final ObjectMapper objectMapper = new ObjectMapper();
            writer.write(objectMapper.writerWithDefaultPrettyPrinter().writeValueAsString(t));
            writer.flush();
            writer.close();
        } catch (IOException e) {
            throw new StorageException(e.getMessage());
        }
    }


    @Override
    public void delete(String id) throws StorageException {
        final File file = Paths.get(storageFile.getAbsolutePath(), id + SUFFIX).toFile();
        if (!file.delete()) {
            throw new StorageException("Failed to delete " + file.getName());
        }
    }

    @Override
    public T load(String id, Class<T> valueType) throws StorageException {
        final File fileToLoad = Paths.get(storageFile.getAbsolutePath(), id + SUFFIX).toFile();
        try {
            final BufferedReader reader = new BufferedReader(
                    new FileReader(fileToLoad.getAbsolutePath())
            );
            final StringBuilder stringBuilder = new StringBuilder();
            String line;
            while ((line = reader.readLine()) != null) {
                stringBuilder.append(line);
            }
            return new ObjectMapper().readValue(stringBuilder.toString(), valueType);
        } catch (IOException e) {
            throw new StorageException(e.getMessage());
        }
    }

    @Override
    public List<T> loadAll(Class<T> valueType) throws StorageException {
        final File[] files = storageFile.listFiles();
        final List<T> list = new ArrayList<>();
        if (files == null) {
            return Collections.emptyList();
        }
        for (final File file : files) {
            if (file.getName().endsWith(SUFFIX)) {
                list.add(load(file.getName().replace(SUFFIX, ""), valueType));
            }
        }
        return list;
    }

    @Override
    public boolean contains(String id) {
        final String fileName = id + SUFFIX;
        final File[] files = storageFile.listFiles();
        if (files == null) {
            return false;
        }
        for (final File file : files) {
            if (file.getName().equals(fileName)) {
                return true;
            }
        }
        return false;
    }
}

@@ -80,60 +80,35 @@
 er;%0A
-%0A
 import 
-java.io.BufferedReader;%0Aimport java.io.File;
+org.fakekoji.Utils;%0A
 %0Aimp
@@ -123,22 +123,16 @@
 .io.File
-Reader
 ;%0Aimport
@@ -157,36 +157,8 @@
 on;%0A
-import java.io.PrintWriter;%0A
 impo
@@ -607,33 +607,54 @@
         
-final File file =
+Utils.writeToFile(%0A                   
  Paths.g
@@ -695,34 +695,25 @@
 id + SUFFIX)
-.toFile();
+,
 %0A           
@@ -717,188 +717,34 @@
     
-final PrintWriter writer = new PrintWriter(file.getAbsolutePath(), %22UTF-8%22);%0A            final ObjectMapper objectMapper = new ObjectMapper();%0A            writer.write(objectMapper
+        new ObjectMapper()
 .wri
@@ -794,18 +794,16 @@
 tring(t)
-);
 %0A       
@@ -811,49 +811,8 @@
     
-writer.flush();%0A            writer.close(
 );%0A 
@@ -1308,544 +1308,205 @@
     
-final File fileToLoad = Paths.get(storageFile.getAbsolutePath(), id + SUFFIX).toFile();%0A        try %7B%0A            final BufferedReader reader = new BufferedReader(%0A                    new FileReader(fileToLoad.getAbsolutePath())%0A            );%0A            final StringBuilder stringBuilder = new StringBuilder();%0A            String line;%0A            while ((line = reader.readLine()) != null) %7B%0A                stringBuilder.append(line);%0A            %7D%0A            return new ObjectMapper().readValue(stringBuilder.toString(), valueType
+try %7B%0A            return new ObjectMapper().readValue(%0A                    Utils.readFile(Paths.get(storageFile.getAbsolutePath(), id + SUFFIX).toFile()),%0A                    valueType%0A            
 );%0A 

Use Utils class in DirectoryJsonStorage