package ru.job4j.generator;

import java.util.*;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * Created by Intellij IDEA.
 * User: Vitaly Zubov.
 * Email: Zubov.VP@yandex.ru.
 * Version: $Id$.
 * Date: 22.12.2019.
 */
public class SimpleGenerator implements Checkable {
    private static final Pattern PATTERN = Pattern.compile("\\$\\{\\w*}");
    private Validator validator;

    /**
     * Constructor.
     */
    public SimpleGenerator() {
        this.validator = new Validator();
    }

    /**
     * Replace markers on the correct words.
     *
     * @param data - collection with words.
     * @param line - line with markers.
     * @return - correct line.
     * @throws CheckExeption - if markers in the collection don't use or key of marker don't exist on the collection.
     */
    @Override
    public String checkLine(Map<String, String> data, String line) throws CheckExeption {
        Set<String> keys = new LinkedHashSet<>();
        StringBuilder sb = new StringBuilder();
        Matcher matcher = PATTERN.matcher(line);
        String value;
        String result;
        while (matcher.find()) {
            String key = matcher.group().substring(2, matcher.group().length() - 1);
            if (this.validator.checkKey(data, key)) {
                keys.add(key);
            } else {
                throw new CheckExeption(String.format("This key  '%s' ,doesn't exist in the data.", key));
            }
        }
        if (data.size() ==  keys.size()) {
            matcher = PATTERN.matcher(line);
            Iterator<String> itr = keys.iterator();
            while (matcher.find()) {
                value = data.get(itr.next());
                matcher.appendReplacement(sb, value);
            }
            matcher.appendTail(sb);
            result = sb.toString();
        } else {
            throw new CheckExeption("Don't use all keys in the data.");
        }
        return result;
    }
}

@@ -1091,31 +1091,8 @@
 ue;%0A
-        String result;%0A
     
@@ -1221,16 +1221,17 @@
     if (
+!
 this.val
@@ -1264,60 +1264,8 @@
 ) %7B%0A
-                keys.add(key);%0A            %7D else %7B%0A
     
@@ -1373,32 +1373,59 @@
 ;%0A            %7D%0A
+            keys.add(key);%0A
         %7D%0A      
@@ -1446,11 +1446,10 @@
 e() 
+!
 =
-= 
  key
@@ -1456,32 +1456,110 @@
 s.size()) %7B%0A    
+        throw new CheckExeption(%22Don't use all keys in the data.%22);%0A        %7D%0A
         matcher 
@@ -1591,20 +1591,16 @@
         
- 
-   
 Iterator
@@ -1635,28 +1635,24 @@
 ();%0A        
-    
 while (match
@@ -1676,20 +1676,16 @@
         
- 
-   
 value = 
@@ -1702,28 +1702,24 @@
 tr.next());%0A
-    
             
@@ -1760,34 +1760,26 @@
 e);%0A        
- 
-   %7D%0A    
+%7D%0A
         matc
@@ -1810,20 +1810,14 @@
     
- 
-   result =
+return
  sb.
@@ -1832,130 +1832,8 @@
 ();%0A
-        %7D else %7B%0A            throw new CheckExeption(%22Don't use all keys in the data.%22);%0A        %7D%0A        return result;%0A
     

Correct simple generator.