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

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.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 for Oracle.
 *
 * @author duhongjun
 */
public final class OracleModifyColumnExtractHandler 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 modifyColumnNode : result) {
            for (ParserRuleContext each : ASTUtils.getAllDescendantNodes(modifyColumnNode, RuleName.MODIFY_COL_PROPERTIES)) {
                // it`s not column definition, but can call this method
                Optional<ColumnDefinition> columnDefinition = columnDefinitionPhraseExtractor.extract(each);
                if (columnDefinition.isPresent()) {
                    extractResult.getColumnDefintions().add(columnDefinition.get());
                }
            }
        }
        return extractResult;
    }
}

@@ -1613,16 +1613,20 @@
 ndler %7B%0A
+    
 %0A    pri
@@ -1741,16 +1741,20 @@
 ctor();%0A
+    
 %0A    @Ov
@@ -1771,16 +1771,25 @@
  public 
+Optional%3C
 ExtractR
@@ -1785,32 +1785,33 @@
 al%3CExtractResult
+%3E
  extract(final P
@@ -1881,22 +1881,33 @@
 ontext%3E 
-result
+modifyColumnNodes
  = ASTUt
@@ -1983,22 +1983,33 @@
     if (
-result
+modifyColumnNodes
 .isEmpty
@@ -2037,12 +2037,25 @@
 urn 
-null
+Optional.absent()
 ;%0A  
@@ -2100,24 +2100,17 @@
 tResult 
-extractR
+r
 esult = 
@@ -2196,22 +2196,33 @@
 nNode : 
-result
+modifyColumnNodes
 ) %7B%0A    
@@ -2596,24 +2596,17 @@
         
-extractR
+r
 esult.ge
@@ -2711,17 +2711,27 @@
  return 
-e
+Optional.%3CE
 xtractRe
@@ -2734,16 +2734,27 @@
 ctResult
+%3Eof(result)
 ;%0A    %7D%0A

use optional for checkstyle