chapter_010/src/main/java/ru/job4j/generator/SimpleGenerator.java
/**
     * 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.
     */
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.
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;
        while (matcher.find()) {
            String key = matcher.group().substring(2, matcher.group().length() - 1);
            if (!this.validator.checkKey(data, key)) {
                throw new CheckExeption(String.format("This key  '%s' ,doesn't exist in the data.", key));
            }
            keys.add(key);
        }
        if (data.size() != keys.size()) {
            throw new CheckExeption("Don't use all keys in the data.");
        }
        matcher = PATTERN.matcher(line);
        Iterator<String> itr = keys.iterator();
        while (matcher.find()) {
            value = data.get(itr.next());
            matcher.appendReplacement(sb, value);
        }
        matcher.appendTail(sb);
        return sb.toString();
    }
@ 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 ; while ( matcher . find ( ) ) { String key = matcher . group ( ) . substring ( 2 , matcher . group ( ) . length ( ) - 1 ) ; if ( ! this . validator . checkKey ( data , key ) ) { throw new CheckExeption ( String . format ( "This key  '%s' ,doesn't exist in the data." , key ) ) ; } keys . add ( key ) ; } if ( data . size ( ) != keys . size ( ) ) { throw new CheckExeption ( "Don't use all keys in the data." ) ; } matcher = PATTERN . matcher ( line ) ; Iterator < String > itr = keys . iterator ( ) ; while ( matcher . find ( ) ) { value = data . get ( itr . next ( ) ) ; matcher . appendReplacement ( sb , value ) ; } matcher . appendTail ( sb ) ; return sb . toString ( ) ; }
Replace markers on the correct words.
Replace markers on the correct words .