/*
 * 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;

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.result.ColumnDefinitionExtractResult;
import io.shardingsphere.core.parsing.antlr.extractor.statement.handler.result.ExtractResult;
import io.shardingsphere.core.parsing.antlr.extractor.statement.phrase.ColumnDefinitionPhraseExtractor;
import io.shardingsphere.core.parsing.antlr.extractor.statement.util.ASTUtils;
import io.shardingsphere.core.parsing.antlr.sql.ddl.ColumnDefinition;

/**
 * Modify column extract handler.
 *
 * @author duhongjun
 */
public class ModifyColumnExtractHandler implements ASTExtractHandler {

    private final ColumnDefinitionPhraseExtractor columnDefinitionPhraseExtractor = new ColumnDefinitionPhraseExtractor();

    @Override
    public ExtractResult extract(final ParserRuleContext ancestorNode) {
        Collection<ParserRuleContext> result = ASTUtils.getAllDescendantNodes(ancestorNode, RuleName.MODIFY_COLUMN);
        if (result.isEmpty()) {
            return null;
        }
        ColumnDefinitionExtractResult extractResult = new ColumnDefinitionExtractResult();
        for (ParserRuleContext each : result) {
            Optional<ColumnDefinition> columnDefinition = columnDefinitionPhraseExtractor.extract(each);
            if (columnDefinition.isPresent()) {
                postExtractColumnDefinition(each, columnDefinition.get());
                extractResult.getColumnDefintions().add(columnDefinition.get());
            }
        }
        return extractResult;
    }

    protected void postExtractColumnDefinition(final ParserRuleContext ancestorNode, final ColumnDefinition columnDefinition) {

    }
}

@@ -1402,16 +1402,20 @@
 ndler %7B%0A
+    
 %0A    pri
@@ -1530,16 +1530,20 @@
 ctor();%0A
+    
 %0A    @Ov
@@ -1560,16 +1560,25 @@
  public 
+Optional%3C
 ExtractR
@@ -1574,32 +1574,33 @@
 al%3CExtractResult
+%3E
  extract(final P
@@ -1670,22 +1670,33 @@
 ontext%3E 
-result
+modifyColumnNodes
  = ASTUt
@@ -1772,22 +1772,33 @@
     if (
-result
+modifyColumnNodes
 .isEmpty
@@ -1826,12 +1826,25 @@
 urn 
-null
+Optional.absent()
 ;%0A  
@@ -1889,24 +1889,17 @@
 tResult 
-extractR
+r
 esult = 
@@ -1973,22 +1973,33 @@
  each : 
-result
+modifyColumnNodes
 ) %7B%0A    
@@ -2238,24 +2238,17 @@
         
-extractR
+r
 esult.ge
@@ -2335,17 +2335,27 @@
  return 
-e
+Optional.%3CE
 xtractRe
@@ -2362,16 +2362,27 @@
 sult
+%3Eof(result)
 ;%0A    %7D%0A
 %0A   
@@ -2377,16 +2377,20 @@
 ;%0A    %7D%0A
+    
 %0A    pro
@@ -2510,17 +2510,16 @@
 tion) %7B%0A
-%0A
     %7D%0A%7D%0A

use optional for checkstyle