/*
 * 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.optimize.sharding.engnie.ddl;

import com.google.common.base.Optional;
import com.google.common.base.Preconditions;
import org.apache.shardingsphere.core.metadata.table.ShardingTableMetaData;
import org.apache.shardingsphere.core.optimize.api.segment.Tables;
import org.apache.shardingsphere.core.optimize.sharding.engnie.ShardingOptimizeEngine;
import org.apache.shardingsphere.core.optimize.sharding.statement.ddl.ShardingDropIndexOptimizedStatement;
import org.apache.shardingsphere.core.parse.sql.segment.ddl.index.IndexSegment;
import org.apache.shardingsphere.core.parse.sql.statement.ddl.DropIndexStatement;
import org.apache.shardingsphere.core.rule.ShardingRule;

import java.util.Collection;
import java.util.Collections;
import java.util.LinkedList;
import java.util.List;

/**
 * Drop index optimize engine for sharding.
 *
 * @author panjuan
 */
public final class ShardingDropIndexOptimizeEngine implements ShardingOptimizeEngine<DropIndexStatement> {
    
    @Override
    public ShardingDropIndexOptimizedStatement optimize(final ShardingRule shardingRule, 
                                                        final ShardingTableMetaData shardingTableMetaData, final String sql, final List<Object> parameters, final DropIndexStatement sqlStatement) {
        Tables tables = new Tables(sqlStatement);
        return new ShardingDropIndexOptimizedStatement(sqlStatement, tables, getTableNames(shardingTableMetaData, sqlStatement, tables));
    }
    
    // TODO simplify from tables
    private Collection<String> getTableNames(final ShardingTableMetaData shardingTableMetaData, final DropIndexStatement sqlStatement, final Tables tables) {
        if (!tables.isEmpty()) {
            return Collections.singletonList(tables.getSingleTableName());
        }
        Collection<String> result = new LinkedList<>();
        for (IndexSegment each : sqlStatement.getIndexes()) {
            Optional<String> tableName = shardingTableMetaData.getLogicTableName(each.getName());
            Preconditions.checkState(tableName.isPresent(), "Cannot find table for index name `%s` from sharding rule.", each.getName());
            result.add(tableName.get());
        }
        return result;
    }
}

@@ -2310,41 +2310,8 @@
    %0A
-    // TODO simplify from tables%0A
     
@@ -2476,13 +2476,15 @@
     
-if (!
+return 
 tabl
@@ -2499,30 +2499,63 @@
 ty()
-) %7B%0A            return
+ ? getTableNames(shardingTableMetaData, sqlStatement) :
  Col
@@ -2603,37 +2603,175 @@
 bleName());%0A    
+%7D%0A
     
-%7D
+%0A    private Collection%3CString%3E getTableNames(final ShardingTableMetaData shardingTableMetaData, final DropIndexStatement sqlStatement) %7B
 %0A        Collect

refactor ShardingDropIndexOptimizeEngine