/*
 * 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.api.config.encrypt;

import com.google.common.base.Function;
import com.google.common.base.Preconditions;
import com.google.common.collect.Collections2;
import lombok.Getter;
import org.apache.shardingsphere.api.config.RuleConfiguration;

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

/**
 * Encrypt rule configuration.
 *
 * @author panjuan
 */
@Getter
public final class EncryptRuleConfiguration implements RuleConfiguration {
    
    private final Map<String, EncryptorRuleConfiguration> encryptors;
    
    private final Map<String, EncryptTableRuleConfiguration> tables;
    
    public EncryptRuleConfiguration() {
        this(new LinkedHashMap<String, EncryptorRuleConfiguration>(), new LinkedHashMap<String, EncryptTableRuleConfiguration>());
    }
    
    public EncryptRuleConfiguration(final Map<String, EncryptorRuleConfiguration> encryptors, final Map<String, EncryptTableRuleConfiguration> tables) {
        this.encryptors = encryptors;
        this.tables = tables;
        Preconditions.checkArgument(isValidConfigurations(), "Invalid encryptorNames are used in EncryptTableRuleConfigurations.");
    }
    
    private boolean isValidConfigurations() {
        return (encryptors.isEmpty() && tables.isEmpty()) || isUsingValidEncryptNames();
    }
    
    private boolean isUsingValidEncryptNames() {
        for (EncryptTableRuleConfiguration each : tables.values()) {
            Collection<String> encryptors = Collections2.transform(each.getColumns().values(), new Function<EncryptColumnRuleConfiguration, String>() {
                
                @Override
                public String apply(final EncryptColumnRuleConfiguration input) {
                    return input.getEncryptor();
                }
            });
            if (!this.encryptors.keySet().containsAll(encryptors)) {
                return false;
            }
        }
        return true;
    }
}

@@ -862,152 +862,59 @@
 ort 
-com.google.common.base.Function;%0Aimport com.google.common.base.Preconditions;%0Aimport com.google.common.collect.Collections2;%0Aimport lombok.Gette
+lombok.Getter;%0Aimport lombok.RequiredArgsConstructo
 r;%0Ai
@@ -980,37 +980,8 @@
 n;%0A%0A
-import java.util.Collection;%0A
 impo
@@ -1092,16 +1092,41 @@
 uan%0A */%0A
+@RequiredArgsConstructor%0A
 @Getter%0A
@@ -1390,24 +1390,24 @@
 uration() %7B%0A
+
         this
@@ -1535,1149 +1535,6 @@
   %7D%0A
-    %0A    public EncryptRuleConfiguration(final Map%3CString, EncryptorRuleConfiguration%3E encryptors, final Map%3CString, EncryptTableRuleConfiguration%3E tables) %7B%0A        this.encryptors = encryptors;%0A        this.tables = tables;%0A        Preconditions.checkArgument(isValidConfigurations(), %22Invalid encryptorNames are used in EncryptTableRuleConfigurations.%22);%0A    %7D%0A    %0A    private boolean isValidConfigurations() %7B%0A        return (encryptors.isEmpty() && tables.isEmpty()) %7C%7C isUsingValidEncryptNames();%0A    %7D%0A    %0A    private boolean isUsingValidEncryptNames() %7B%0A        for (EncryptTableRuleConfiguration each : tables.values()) %7B%0A            Collection%3CString%3E encryptors = Collections2.transform(each.getColumns().values(), new Function%3CEncryptColumnRuleConfiguration, String%3E() %7B%0A                %0A                @Override%0A                public String apply(final EncryptColumnRuleConfiguration input) %7B%0A                    return input.getEncryptor();%0A                %7D%0A            %7D);%0A            if (!this.encryptors.keySet().containsAll(encryptors)) %7B%0A                return false;%0A            %7D%0A        %7D%0A        return true;%0A    %7D%0A
 %7D%0A

delete isValidEncryptRuleConfig()