package referencelibrary.data;

import referencelibrary.reference.Reference;

import java.io.*;
import java.util.ArrayList;
import java.util.List;

/**
 * Creted by petri on 11/04/16
 */
public class FileReferenceDao implements ReferenceDao {
    private File file;
    private List<Reference> references;

    /**
     * Creates a FileReferenceDao that uses a file with a given name for storage
     * @param filename filename for storing References
     */
    public FileReferenceDao(String filename) {
        file = new File(filename);
        references = readReferences();
    }

    /**
     * Reads stored References
     * @return List of References
     */
    private List<Reference> readReferences() {
        try {
            return readFromStream();
        } catch (IOException | ClassNotFoundException e) {
            return new ArrayList<>();
        }
    }

    /**
     * Reads the Reference List from the file
     * @return List of References
     * @throws IOException
     * @throws ClassNotFoundException
     */
    private List<Reference> readFromStream() throws IOException, ClassNotFoundException {
        FileInputStream fis = new FileInputStream(file);
        ObjectInputStream ois = new ObjectInputStream(fis);
        List<Reference> refs = (List<Reference>)ois.readObject();
        ois.close();
        fis.close();
        return refs;
    }

    @Override
    public List<Reference> listAll() {
        this.references = readReferences();
        return references;
    }

    @Override
    public void add(Reference reference) {
        references.add(reference);
        try {
            writeReferenceListToFile();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    /**
     * Writes the Reference List to the file
     * @throws IOException
     */
    private void writeReferenceListToFile() throws IOException {
        FileOutputStream fos = new FileOutputStream(file);
        ObjectOutputStream oos = new ObjectOutputStream(fos);

        oos.writeObject(references);
        oos.flush();
        oos.close();
        fos.close();
    }
}
@@ -236,16 +236,17 @@
 ceDao %7B%0A
+%0A
     priv
@@ -249,16 +249,22 @@
 private 
+final 
 File fil
@@ -396,16 +396,23 @@
 storage%0A
+     *%0A
      * @
@@ -625,32 +625,39 @@
 ored References%0A
+     *%0A
      * @return L
@@ -940,32 +940,39 @@
 t from the file%0A
+     *%0A
      * @return L
@@ -1156,24 +1156,59 @@
 n %7B%0A        
+List%3CReference%3E refs;%0A        try (
 FileInputStr
@@ -1248,24 +1248,32 @@
 e);%0A        
+   
+     
 ObjectInputS
@@ -1310,17 +1310,19 @@
 eam(fis)
-;
+) %7B
 %0A       
@@ -1318,39 +1318,27 @@
 ) %7B%0A        
-List%3CReference%3E
+   
  refs = (Lis
@@ -1350,16 +1350,17 @@
 erence%3E)
+ 
 ois.read
@@ -1381,41 +1381,9 @@
     
-ois.close();%0A        fis.close();
+%7D
 %0A   
@@ -1817,24 +1817,31 @@
 to the file%0A
+     *%0A
      * @thro
@@ -1936,16 +1936,21 @@
         
+try (
 FileOutp
@@ -1996,24 +1996,32 @@
 e);%0A        
+   
+     
 ObjectOutput
@@ -2060,19 +2060,25 @@
 eam(fos)
-;%0A%0A
+) %7B%0A%0A    
         
@@ -2116,40 +2116,23 @@
     
-  oos.flush();%0A
       
-  
 oos.
-close
+flush
 ();%0A
@@ -2143,24 +2143,13 @@
     
-fos.close();
+%7D
 %0A    %7D%0A%7D
@@ -2148,8 +2148,9 @@
 %0A    %7D%0A%7D
+%0A

Kill resource close mutations