package com.dth.util;

import com.dth.entity.WordOccurrence;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.Optional;
import java.util.logging.Level;
import java.util.logging.Logger;

public class DefaultDocumentProcessor<T> implements DocumentProcessor {

    private final File file;
    private final WordProcessor processor;
    private final ArrayList<WordOccurrence> words = new ArrayList<>(100);

    /**
     * Reads a text file and processes its content, by fetching words and
     * counting their occurrences.
     *
     * @param file the file to be processed.
     * @param processor the word processor used to transform the word.
     */
    public DefaultDocumentProcessor(File file, WordProcessor processor) {
        this.file = file;
        this.processor = processor;
    }
    
    public File getFile() {
        return file;
    }

    @Override
    public ArrayList<WordOccurrence> getWords() {
        return words;
    }

    /**
     * Processes the text file and populates its internal list of words.
     */
    @Override
    public void processFile() {
        try (BufferedReader br = new BufferedReader(
                new InputStreamReader(new FileInputStream(file), "UTF8"))) {

            String line;
            while ((line = br.readLine()) != null) {
                String[] tempWords = line.split(" ");
                
                for (Object temp : tempWords) {
                    temp = processor.processWord(temp);
                    if (temp == null) {
                        continue;
                    }

                    WordOccurrence wordOccurence = new WordOccurrence(temp.toString());

                    Optional<WordOccurrence> sameWord = words.parallelStream()
                            .filter(w -> w.getWord().equals(wordOccurence.getWord()))
                            .findAny();

                    if (sameWord.isPresent()) {
                        sameWord.get().incrementCount();
                    } else {
                        words.add(wordOccurence);
                    }
                }
            }
        } catch (IOException ex) {
            Logger.getLogger(DefaultDocumentProcessor.class.getName()).log(Level.SEVERE, null, ex);
        }
    }
}

@@ -334,16 +334,350 @@
 ger;%0D%0A%0D%0A
+/**%0D%0A * The purpose of the DefaultDocumentProcessor is to parse a text file%0D%0A * and generate a list of words, represented by %7B@code WordOccurrence%7D%0D%0A * objects.%0D%0A * %0D%0A * It stores the words in an %7B@code ArrayList%7D, which is populated by%0D%0A * the %7B@code #processFile()%7D method and retrieved by the%0D%0A * %7B@code #getWordS()%7D method.%0D%0A */%0D%0A
 public c
@@ -709,11 +709,8 @@
 ssor
-%3CT%3E
  imp
@@ -1306,242 +1306,917 @@
     
-public File getFile() %7B%0D%0A        return file;%0D%0A    %7D%0D%0A%0D%0A    @Override%0D%0A    public ArrayList%3CWordOccurrence%3E getWords() %7B%0D%0A        return words;%0D%0A    %7D%0D%0A%0D%0A    /**%0D%0A     * Processes the text file and populates its internal list of words
+/**%0D%0A     * Returns the file provided in the constructor, which should be a%0D%0A     * text document.%0D%0A     * %0D%0A     * @return  the file being used.%0D%0A     */%0D%0A    public File getFile() %7B%0D%0A        return file;%0D%0A    %7D%0D%0A%0D%0A    /**%0D%0A     * Returns the list of words generated by the %7B@code #processFile()%7D%0D%0A     * method. If the %7B@code #processFile()%7D hasn't been run before%0D%0A     * accessing the words, or there was a problem with the text file,%0D%0A     * the list will be empty.%0D%0A     * %0D%0A     * @return  the list of words.%0D%0A     */%0D%0A    @Override%0D%0A    public ArrayList%3CWordOccurrence%3E getWords() %7B%0D%0A        return words;%0D%0A    %7D%0D%0A%0D%0A    /**%0D%0A     * Processes the text file and populates its internal list of words.%0D%0A     * %0D%0A     * %3Cp%3EThe file is read by the BufferedReader object, using the UTF8%0D%0A     * encoding. Results are stored in the internal list, and can be%0D%0A     * accessed from the %7B@code getWords()%7D method
 .%0D%0A 

Add DocumentProcessor javadoc