/*
 * 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.parsing.antlr.extractor.statement.handler.dialect.mysql;

import java.util.Collection;

import org.antlr.v4.runtime.ParserRuleContext;

import com.google.common.base.Optional;

import io.shardingsphere.core.parsing.antlr.extractor.statement.handler.ASTExtractHandler;
import io.shardingsphere.core.parsing.antlr.extractor.statement.handler.RuleName;
import io.shardingsphere.core.parsing.antlr.extractor.statement.handler.result.ExtractResult;
import io.shardingsphere.core.parsing.antlr.extractor.statement.handler.result.SQLTokenExtractResult;
import io.shardingsphere.core.parsing.antlr.extractor.statement.util.ASTUtils;
import io.shardingsphere.core.parsing.parser.token.IndexToken;
import io.shardingsphere.core.util.SQLUtil;

/**
 * Add index extract handler for MySQL.
 *
 * @author duhongjun
 */
public final class MySQLAddIndexExtractHandler implements ASTExtractHandler {

    @Override
    public ExtractResult extract(final ParserRuleContext ancestorNode) {
        Collection<ParserRuleContext> result = ASTUtils.getAllDescendantNodes(ancestorNode, RuleName.ADD_INDEX);
        if (result.isEmpty()) {
            return null;
        }
        SQLTokenExtractResult extractResult = new SQLTokenExtractResult();
        for (ParserRuleContext each : result) {
            Optional<ParserRuleContext> indexNameNode = ASTUtils.findFirstChildNode(each, RuleName.INDEX_NAME);
            if (indexNameNode.isPresent()) {
                extractResult.getSqlTokens().add(
                        new IndexToken(indexNameNode.get().getStop().getStartIndex(), SQLUtil.getNameWithoutSchema(indexNameNode.get().getText()), null));
            }
        }
        return extractResult;
    }
}

@@ -1527,16 +1527,20 @@
 ndler %7B%0A
+    
 %0A    @Ov
@@ -1557,16 +1557,25 @@
  public 
+Optional%3C
 ExtractR
@@ -1571,32 +1571,33 @@
 al%3CExtractResult
+%3E
  extract(final P
@@ -1667,22 +1667,29 @@
 ontext%3E 
-result
+addIndexNodes
  = ASTUt
@@ -1761,22 +1761,29 @@
     if (
-result
+addIndexNodes
 .isEmpty
@@ -1807,20 +1807,33 @@
  return 
-null
+Optional.absent()
 ;%0A      
@@ -1866,24 +1866,17 @@
 tResult 
-extractR
+r
 esult = 
@@ -1942,22 +1942,29 @@
  each : 
-result
+addIndexNodes
 ) %7B%0A    
@@ -2132,24 +2132,17 @@
         
-extractR
+r
 esult.ge
@@ -2353,17 +2353,27 @@
  return 
-e
+Optional.%3CE
 xtractRe
@@ -2376,16 +2376,27 @@
 ctResult
+%3Eof(result)
 ;%0A    %7D%0A

use optional for checkstyle