/**
 * Copyright (c) 2015 Nikolai Mavrenkov <koluch@koluch.ru>
 * <p>
 * Distributed under the MIT License (See accompanying file LICENSE or copy at http://opensource.org/licenses/MIT).
 * <p>
 * Created: 28.10.2015 00:42
 */
package ru.koluch.textWork.morphDict.lookup;


import ru.koluch.textWork.morphDict.dictionary.data.Dictionary;
import ru.koluch.textWork.morphDict.dictionary.data.LexemeRec;
import ru.koluch.textWork.morphDict.dictionary.data.ParadigmRule;
import ru.koluch.textWork.morphDict.lookup.data.Lexeme;
import ru.koluch.textWork.morphDict.lookup.data.LookupResult;
import ru.koluch.textWork.morphDict.lookup.data.WordForm;
import ru.koluch.textWork.morphDict.prefixTree.PrefixTree;
import ru.koluch.textWork.morphDict.dictionary.DictionaryHelper;

import java.util.*;

/**
 * Implementation of lookup service using prefix-tree represenation
 */
public class PrefixTreeLookupService implements LookupService {


    private final Dictionary dictionary;
    private final PrefixTree<DictionaryHelper.TreeData> prefixTree;

    public PrefixTreeLookupService(Dictionary dictionary, PrefixTree<DictionaryHelper.TreeData> prefixTree) {
        this.dictionary = dictionary;
        this.prefixTree = prefixTree;
    }

    /**
     * Search wordform in dictionary
     *
     * @param toFind arbitrary wordform
     * @return list of lexemes-homonym, containing specified wordform
     */
    @Override
    public ArrayList<LookupResult> lookup(String toFind)
    {
        ArrayList<LookupResult> lookupResultList = new ArrayList<>();

        Optional<List<DictionaryHelper.TreeData>> treeDataListOpt = prefixTree.get(toFind);
        if(treeDataListOpt.isPresent()) {
            List<DictionaryHelper.TreeData> treeDataList = treeDataListOpt.get();
            for (DictionaryHelper.TreeData treeData : treeDataList) {
                LexemeRec lexemeRec = dictionary.lexemeRecs.get(treeData.lexemeRecNum);
                Optional<String> commonAncode = lexemeRec.ancode;
                List<ParadigmRule> paradigmRules = dictionary.paradigmList.get(lexemeRec.paradigmIndex);
                Optional<String> globalPrefix = lexemeRec.prefixParadigmIndex.map(dictionary.prefixeParadigmList::get);

                // Build found wordform
                ParadigmRule foundParadigmRule = paradigmRules.get(treeData.paradigmNum);
                WordForm foundWordForm = new WordForm(
                        Optional.of(globalPrefix.orElse("") + foundParadigmRule.prefix.orElse("")),
                        lexemeRec.basis,
                        foundParadigmRule.ending,
                        foundParadigmRule.ancode
                );

                // Build lexeme
                ArrayList<WordForm> omonims = new ArrayList<>();
                for (ParadigmRule paradigmRule : paradigmRules) {
                    omonims.add(new WordForm(
                            Optional.of(globalPrefix.orElse("") + paradigmRule.prefix.orElse("")),
                            lexemeRec.basis,
                            paradigmRule.ending,
                            paradigmRule.ancode
                    ));
                }
                Lexeme lexeme = new Lexeme(omonims, commonAncode);

                lookupResultList.add(new LookupResult(foundWordForm, lexeme));
            }
        }

        return lookupResultList;
    }


}

@@ -2241,24 +2241,25 @@
 d found word
+ 
 form%0A       
@@ -2724,21 +2724,22 @@
 rdForm%3E 
+h
 omon
-i
+y
 ms = new
@@ -2840,21 +2840,22 @@
         
+h
 omon
-i
+y
 ms.add(n
@@ -3197,13 +3197,14 @@
 eme(
+h
 omon
-i
+y
 ms, 

Rename variable