/*
 * Copyright 2016-2018 shardingsphere.io.
 * <p>
 * Licensed 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.
 * </p>
 */

package io.shardingsphere.core.keygen;

import com.google.common.base.Predicate;
import com.google.common.collect.Collections2;
import io.shardingsphere.core.keygen.generator.KeyGenerator;
import io.shardingsphere.spi.NewInstanceServiceLoader;
import lombok.AccessLevel;
import lombok.NoArgsConstructor;

import java.util.Collections;

/**
 * Key generator factory.
 * 
 * @author zhangliang
 */
@NoArgsConstructor(access = AccessLevel.PRIVATE)
public final class KeyGeneratorFactory {
    
    /**
     * Create key generator.
     * 
     * @param keyGeneratorType key generator type
     * @return key generator instance
     */
    public static KeyGenerator newInstance(final String keyGeneratorType) {
        try {
            return (KeyGenerator) Class.forName(keyGeneratorClassName).newInstance();
        } catch (final ReflectiveOperationException ex) {
            throw new IllegalArgumentException(String.format("Class %s should have public privilege and no argument constructor", keyGeneratorClassName));
        }
    }
    
    private static boolean isValid(final String keyGeneratorType) {
        return !Collections2.filter(NewInstanceServiceLoader.load(KeyGenerator.class), new Predicate<KeyGenerator>() {
            
            @Override
            public boolean apply(final KeyGenerator input) {
                return keyGeneratorType.equals(input.getType());
            }
        }).isEmpty();
    }
}

@@ -741,16 +741,88 @@
 tions2;%0A
+import io.shardingsphere.core.exception.ShardingConfigurationException;%0A
 import i
@@ -1017,17 +1017,16 @@
 llection
-s
 ;%0A%0A/**%0A 
@@ -1076,16 +1076,35 @@
 ngliang%0A
+ * @author panjuan%0A
  */%0A@NoA
@@ -1423,322 +1423,299 @@
     
-try %7B%0A            return (KeyGenerator) Class.forName(keyGeneratorClassName).newInstance();%0A        %7D catch (final ReflectiveOperationException ex) %7B%0A            throw new IllegalArgumentException(String.format(%22Class %25s should have public privilege and no argument constructor%22, keyGeneratorClassName));%0A        %7D
+Collection%3CKeyGenerator%3E keyGenerators = NewInstanceServiceLoader.load(KeyGenerator.class);%0A        if (!isValid(keyGeneratorType, keyGenerators)) %7B%0A            throw new ShardingConfigurationException(%22Invalid key generator type.%22);%0A        %7D%0A        return keyGenerators.iterator().next();
 %0A   
@@ -1778,32 +1778,78 @@
 keyGeneratorType
+, final Collection%3CKeyGenerator%3E keyGenerators
 ) %7B%0A        retu
@@ -1876,39 +1876,9 @@
 ter(
-NewInstanceServiceLoader.load(K
+k
 eyGe
@@ -1884,23 +1884,17 @@
 enerator
-.class)
+s
 , new Pr

modify KeyGeneratorFactory