package plagiarism;

import plagiarism.filter.CharacterFilter;
import plagiarism.filter.IrrelevantWordsFilter;
import plagiarism.filter.WordSeparatorFilter;

import java.io.*;
import java.util.HashMap;
import java.util.Map;

/**
 * Plagiarism builds a filter chain to read and filter an input text.
 * <p>
 * The filtered data will be stored in this object and the object provides a method to calculate a simple checksum of the input text.
 * <p>
 * This checksum gives information, if the input text might be a plagiarism or not.
 *
 * @author Hendrik Brinkmann
 */
public class Plagiarism {

    private Map<String, Integer> map;
    private File file;

    public Plagiarism(File file) throws IOException {
        this.file = file;
        this.map = new HashMap<>();
        init();
    }

    private void init() throws IOException {

        BufferedReader bufferedReader = new BufferedReader(new FileReader(file));
        FilterReader filterChain = new IrrelevantWordsFilter(new WordSeparatorFilter(new CharacterFilter(bufferedReader)));

        char[] buffer = new char[256];
        int readChars;

        while ((readChars = filterChain.read(buffer)) != -1) {
            if (readChars != 0) {
                String word = new String(buffer, 0, readChars);
                map.compute(word, (k, v) -> (v == null) ? 1 : ++v);
            }
        }
    }

    public int checksum() {
        return map.entrySet()
                .stream()
                .mapToInt(entry -> digitSum(entry.getKey()) * entry.getValue())
                .sum();
    }

    private int digitSum(String word) {
        return word.chars().sum();
    }

    @Override
    public String toString() {
        return map.toString();
    }
}

@@ -375,16 +375,19 @@
 ovides a
+%0A *
  method 
@@ -521,16 +521,19 @@
 arism or
+%0A *
  not.%0A *
@@ -852,90 +852,13 @@
     
-BufferedReader bufferedReader = new BufferedReader(new FileReader(file));%0A        
+try (
 Filt
@@ -950,17 +950,21 @@
 rFilter(
-b
+new B
 ufferedR
@@ -972,22 +972,48 @@
 ader
-)));%0A%0A
+(new FileReader(file)))))) %7B%0A   
         
 char
@@ -1008,16 +1008,17 @@
         
+ 
 char%5B%5D b
@@ -1040,16 +1040,20 @@
 r%5B256%5D;%0A
+    
         
@@ -1076,16 +1076,20 @@
         
+    
 while ((
@@ -1147,16 +1147,20 @@
         
+ 
+   
 if (read
@@ -1169,24 +1169,28 @@
 ars != 0) %7B%0A
+    
             
@@ -1257,16 +1257,20 @@
         
+   
+ 
 map.comp
@@ -1313,16 +1313,34 @@
 : ++v);%0A
+                %7D%0A
         

use try-with-resource to ensure that the reader will be closed properly