/*
 * Licensed to the Apache Software Foundation (ASF) under one or more
 * contributor license agreements.  See the NOTICE file distributed with
 * this work for additional information regarding copyright ownership.
 * The ASF licenses this file to You under the Apache License, Version 2.0
 * (the "License"); you may not use this file except in compliance with
 * the License.  You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package org.apache.shardingsphere.core.rule;

import com.google.common.base.Function;
import com.google.common.collect.Collections2;
import lombok.Getter;
import org.apache.shardingsphere.api.config.encryptor.EncryptRuleConfiguration;
import org.apache.shardingsphere.api.config.encryptor.EncryptTableRuleConfiguration;
import org.apache.shardingsphere.core.strategy.encrypt.ShardingEncryptorEngine;
import org.apache.shardingsphere.core.strategy.encrypt.ShardingEncryptorStrategy;

import java.util.Collection;
import java.util.LinkedHashMap;
import java.util.LinkedList;
import java.util.Map;

/**
 * Encrypt rule.
 *
 * @author panjuan
 */
public final class EncryptRule implements BaseRule {
    
    private final Collection<EncryptTableRule> tableRules;
    
    @Getter
    private final ShardingEncryptorEngine encryptorEngine;
    
    public EncryptRule(final EncryptRuleConfiguration encryptRuleConfiguration) {
        tableRules = new LinkedList<>();
        Map<String, ShardingEncryptorStrategy> shardingEncryptorStrategies = new LinkedHashMap<>();
        for (EncryptTableRuleConfiguration each : encryptRuleConfiguration.getTableRuleConfigs()) {
            EncryptTableRule tableRule = new EncryptTableRule(each);
            tableRules.add(tableRule);
            shardingEncryptorStrategies.put(tableRule.getTable(), tableRule.getShardingEncryptorStrategy());
        }
        if (null == encryptRuleConfiguration.getDefaultEncryptorConfig()) {
            encryptorEngine = new ShardingEncryptorEngine(shardingEncryptorStrategies);
        } else {
            encryptorEngine = new ShardingEncryptorEngine(shardingEncryptorStrategies, new ShardingEncryptorStrategy(encryptRuleConfiguration.getDefaultEncryptorConfig()));
        }
    }
    
    /**
     * Get encrypt table names.
     * 
     * @return encrypt table names
     */
    public Collection<String> getEncryptTableNames() {
        return Collections2.transform(tableRules, new Function<EncryptTableRule, String>() {
            
            @Override
            public String apply(final EncryptTableRule input) {
                return input.getTable();
            }
        });
    }
}

@@ -846,95 +846,8 @@
 e;%0A%0A
-import com.google.common.base.Function;%0Aimport com.google.common.collect.Collections2;%0A
 impo
@@ -981,333 +981,83 @@
 ere.
-api.config.encryptor.EncryptTableRuleConfiguration;%0Aimport org.apache.shardingsphere.core.strategy.encrypt.ShardingEncryptorEngine;%0Aimport org.apache.shardingsphere.core.strategy.encrypt.ShardingEncryptorStrategy;%0A%0Aimport java.util.Collection;%0Aimport java.util.LinkedHashMap;%0Aimport java.util.LinkedList;%0Aimport java.util.Map
+core.strategy.encrypt.ShardingEncryptorEngine;%0A%0Aimport java.util.Collection
 ;%0A%0A/
@@ -1164,72 +1164,8 @@
    %0A
-    private final Collection%3CEncryptTableRule%3E tableRules;%0A    %0A
     
@@ -1330,1245 +1330,290 @@
     
-tableRules = new LinkedList%3C%3E();%0A        Map%3CString, ShardingEncryptorStrategy%3E shardingEncryptorStrategies = new LinkedHashMap%3C%3E();%0A        for (EncryptTableRuleConfiguration each : encryptRuleConfiguration.getTableRuleConfigs()) %7B%0A            EncryptTableRule tableRule = new EncryptTableRule(each);%0A            tableRules.add(tableRule);%0A            shardingEncryptorStrategies.put(tableRule.getTable(), tableRule.getShardingEncryptorStrategy());%0A        %7D%0A        if (null == encryptRuleConfiguration.getDefaultEncryptorConfig()) %7B%0A            encryptorEngine = new ShardingEncryptorEngine(shardingEncryptorStrategies);%0A        %7D else %7B%0A            encryptorEngine = new ShardingEncryptorEngine(shardingEncryptorStrategies, new ShardingEncryptorStrategy(encryptRuleConfiguration.getDefaultEncryptorConfig()));%0A        %7D%0A    %7D%0A    %0A    /**%0A     * Get encrypt table names.%0A     * %0A     * @return encrypt table names%0A     */%0A    public Collection%3CString%3E getEncryptTableNames() %7B%0A        return Collections2.transform(tableRules, new Function%3CEncryptTableRule, String%3E() %7B%0A            %0A            @Override%0A            public String apply(final EncryptTableRule input) %7B%0A                return input.getTable();%0A            %7D%0A        %7D
+encryptorEngine = new ShardingEncryptorEngine(encryptRuleConfiguration);%0A    %7D%0A    %0A    /**%0A     * Get encrypt table names.%0A     * %0A     * @return encrypt table names%0A     */%0A    public Collection%3CString%3E getEncryptTableNames() %7B%0A        return encryptorEngine.getEncryptTableNames(
 );%0A 

delete tableRules