/**
 * 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<>();

        List<DictionaryHelper.TreeData> treeDataList = prefixTree.get(toFind).get();//todo: check for null
        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;
    }


}

@@ -1548,24 +1548,33 @@
 );%0A%0A        
+Optional%3C
 List%3CDiction
@@ -1592,16 +1592,17 @@
 reeData%3E
+%3E
  treeDat
@@ -1606,16 +1606,19 @@
 DataList
+Opt
  = prefi
@@ -1638,38 +1638,138 @@
 ind)
-.get();//todo: check for null%0A
+;%0A        if(treeDataListOpt.isPresent()) %7B%0A            List%3CDictionaryHelper.TreeData%3E treeDataList = treeDataListOpt.get();%0A    
     
@@ -1826,24 +1826,28 @@
 DataList) %7B%0A
+    
             
@@ -1922,32 +1922,36 @@
 m);%0A            
+   
+ 
 Optional%3CString%3E
@@ -1984,16 +1984,20 @@
 ancode;%0A
+    
         
@@ -2089,16 +2089,20 @@
 Index);%0A
+    
         
@@ -2214,32 +2214,36 @@
 );%0A%0A            
+    
 // Build found w
@@ -2246,24 +2246,28 @@
 nd wordform%0A
+    
             
@@ -2352,16 +2352,20 @@
         
+    
 WordForm
@@ -2387,32 +2387,40 @@
 = new WordForm(%0A
+        
                 
@@ -2503,32 +2503,40 @@
                 
+     
+   
 lexemeRec.basis,
@@ -2528,32 +2528,40 @@
 exemeRec.basis,%0A
+        
                 
@@ -2594,32 +2594,40 @@
                 
+      
+  
 foundParadigmRul
@@ -2647,16 +2647,20 @@
         
+    
 );%0A%0A    
@@ -2659,32 +2659,36 @@
 );%0A%0A            
+   
+ 
 // Build lexeme%0A
@@ -2687,16 +2687,20 @@
  lexeme%0A
+    
         
@@ -2760,24 +2760,28 @@
             
+   
+ 
 for (Paradig
@@ -2834,16 +2834,20 @@
         
+    
 omonims.
@@ -2856,32 +2856,36 @@
 d(new WordForm(%0A
+    
                 
@@ -2979,32 +2979,36 @@
                 
+    
 lexemeRec.basis,
@@ -3000,32 +3000,36 @@
 exemeRec.basis,%0A
+    
                 
@@ -3073,32 +3073,36 @@
                 
+    
 paradigmRule.anc
@@ -3121,16 +3121,20 @@
         
+    
 ));%0A    
@@ -3141,18 +3141,26 @@
         
+ 
-%7D%0A
+   %7D%0A    
         
@@ -3227,16 +3227,20 @@
         
+ 
+   
 lookupRe
@@ -3290,24 +3290,38 @@
 , lexeme));%0A
+            %7D%0A
         %7D%0A%0A 

Add null-check