/*
 * 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.route.router.sharding.validator.routingresult.impl;

import com.google.common.base.Strings;
import com.google.common.collect.Lists;
import com.google.common.collect.Multimap;
import org.apache.shardingsphere.core.exception.ShardingException;
import org.apache.shardingsphere.core.metadata.ShardingSphereMetaData;
import org.apache.shardingsphere.core.optimize.sharding.statement.ShardingOptimizedStatement;
import org.apache.shardingsphere.core.route.router.sharding.condition.ShardingConditions;
import org.apache.shardingsphere.core.route.type.RoutingUnit;
import org.apache.shardingsphere.core.route.type.TableUnit;
import org.apache.shardingsphere.core.rule.ShardingRule;

import java.util.Collection;

/**
 * Complex routing result validator.
 *
 * @author sunbufu
 * @author zhangliang
 */
public final class ComplexRoutingResultValidator extends AbstractRoutingResultValidator {
    
    public ComplexRoutingResultValidator(final ShardingRule shardingRule, final ShardingSphereMetaData metaData) {
        super(shardingRule, metaData);
    }
    
    @Override
    protected void throwException(final ShardingOptimizedStatement shardingStatement, final ShardingConditions shardingConditions, final Multimap<RoutingUnit, TableUnit> unconfiguredRoutingUnits) {
        RoutingUnit routingUnit = unconfiguredRoutingUnits.keySet().iterator().next();
        Collection<String> absentDataNodes = Lists.newArrayListWithExpectedSize(unconfiguredRoutingUnits.get(routingUnit).size());
        for (TableUnit each : unconfiguredRoutingUnits.get(routingUnit)) {
            absentDataNodes.add(routingUnit.getDataSourceName() + "." + each.getActualTableName());
        }
        if (!absentDataNodes.isEmpty()) {
            throwExceptionForAbsentDataNode(absentDataNodes, "");
        }
    }
    
    private void throwExceptionForAbsentDataNode(final Collection<String> absentDataNodes, final CharSequence detail) {
        String msg = "We get some absent DataNodes=" + absentDataNodes + " in routing result, " + (Strings.isNullOrEmpty(detail.toString()) ? "" : detail)
            + "please check the configuration of rule and data node.";
        throw new ShardingException(msg.replace("%", "%%"));
    }
}

@@ -892,87 +892,8 @@
 l;%0A%0A
-import com.google.common.base.Strings;%0Aimport com.google.common.collect.Lists;%0A
 impo
@@ -1433,16 +1433,44 @@
 gRule;%0A%0A
+import java.util.ArrayList;%0A
 import j
@@ -2158,38 +2158,44 @@
 lection%3CString%3E 
-absent
+unconfigured
 DataNodes = List
@@ -2194,17 +2194,12 @@
 s = 
-Lists.
 new
+ 
 Arra
@@ -2207,24 +2207,10 @@
 List
-WithExpectedSize
+%3C%3E
 (unc
@@ -2344,22 +2344,28 @@
         
-absent
+unconfigured
 DataNode
@@ -2461,22 +2461,28 @@
    if (!
-absent
+unconfigured
 DataNode
@@ -2517,484 +2517,187 @@
 hrow
-ExceptionForAbsentDataNode(absentDataNodes, %22%22);%0A        %7D%0A    %7D%0A    %0A    private void throwExceptionForAbsentDataNode(final Collection%3CString%3E absentDataNodes, final CharSequence detail) %7B%0A        String msg = %22We get some absent DataNodes=%22 + absentDataNodes + %22 in routing result, %22 + (Strings.isNullOrEmpty(detail.toString()) ? %22%22 : detail)%0A            + %22please check the configuration of rule and data node.%22;%0A        throw new ShardingException(msg.replace(%22%25%22, %22%25%25%22));
+ new ShardingException(String.format(%22We get some absent DataNodes=%25s in routing result, please check the configuration of rule and data node.%22, unconfiguredDataNodes));%0A        %7D
 %0A   

refactor ComplexRoutingResultValidator