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

import lombok.Getter;
import lombok.Setter;
import org.apache.shardingsphere.core.rewrite.placeholder.Alterable;
import org.apache.shardingsphere.core.rewrite.placeholder.ShardingPlaceholder;
import org.apache.shardingsphere.core.rewrite.token.pojo.SQLToken;
import org.apache.shardingsphere.core.rewrite.token.pojo.Substitutable;
import org.apache.shardingsphere.core.route.type.RoutingUnit;

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

/**
 * SQL builder.
 *
 * @author gaohongtao
 * @author zhangliang
 * @author maxiaoguang
 * @author panjuan
 */
public final class SQLBuilder {
    
    @Setter
    private String logicSQL;
    
    @Getter
    private final List<SQLToken> sqlTokens;
    
    private int currentSQLTokenIndex;
    
    private final List<Object> segments;
    
    private StringBuilder currentSegment;
    
    public SQLBuilder() {
        sqlTokens = new LinkedList<>();
        segments = new LinkedList<>();
        currentSegment = new StringBuilder();
        segments.add(currentSegment);
    }
    
    /**
     * Append literals.
     *
     * @param literals literals for SQL
     */
    public void appendLiterals(final String literals) {
        currentSegment.append(literals);
    }
    
    /**
     * Append sharding placeholder.
     *
     * @param shardingPlaceholder sharding placeholder
     */
    public void appendPlaceholder(final ShardingPlaceholder shardingPlaceholder) {
        segments.add(shardingPlaceholder);
        currentSegment = new StringBuilder();
        segments.add(currentSegment);
    }
    
    private int getStartIndex(final SQLToken sqlToken) {
        return sqlToken instanceof Substitutable ? ((Substitutable) sqlToken).getStopIndex() + 1 : sqlToken.getStartIndex();
    }
    
    /**
     * Convert to SQL.
     *
     * @return SQL
     */
    public String toSQL() {
        return toSQL(null, Collections.<String, String>emptyMap());
    }
    
    /**
     * Convert to SQL.
     *
     * @param routingUnit routing unit
     * @param logicAndActualTables logic and actual map
     * @return SQL
     */
    public String toSQL(final RoutingUnit routingUnit, final Map<String, String> logicAndActualTables) {
        if (sqlTokens.isEmpty()) {
            return logicSQL;
        }
        return createLogicSQL(routingUnit, logicAndActualTables);
    }
    
    private String createLogicSQL(final RoutingUnit routingUnit, final Map<String, String> logicAndActualTables) {
        StringBuilder result = new StringBuilder();
        result.append(logicSQL.substring(0, sqlTokens.get(0).getStartIndex()));
        for (SQLToken each : sqlTokens) {
            result.append(getSQLTokenLiterals(each, routingUnit, logicAndActualTables));
            result.append(getConjunction(each));
        }
        return result.toString();
    }
    
    private String getSQLTokenLiterals(final SQLToken sqlToken, final RoutingUnit routingUnit, final Map<String, String> logicAndActualTables) {
        return sqlToken instanceof Alterable ? ((Alterable) sqlToken).toString(routingUnit, logicAndActualTables) : sqlToken.toString();
    }
    
    private String getConjunction(final SQLToken sqlToken) {
        int stopIndex = sqlTokens.size() - 1 == currentSQLTokenIndex ? logicSQL.length() : sqlTokens.get(currentSQLTokenIndex + 1).getStartIndex();
        currentSQLTokenIndex++;
        return logicSQL.substring(getStartIndex(sqlToken) > logicSQL.length() ? logicSQL.length() : getStartIndex(sqlToken), stopIndex);
    }
}

@@ -871,35 +871,30 @@
 bok.
-Getter;%0Aimport lombok.Sette
+RequiredArgsConstructo
 r;%0Ai
@@ -965,87 +965,8 @@
 le;%0A
-import org.apache.shardingsphere.core.rewrite.placeholder.ShardingPlaceholder;%0A
 impo
@@ -1197,37 +1197,8 @@
 ns;%0A
-import java.util.LinkedList;%0A
 impo
@@ -1352,16 +1352,41 @@
 uan%0A */%0A
+@RequiredArgsConstructor%0A
 public f
@@ -1418,20 +1418,8 @@
    %0A
-    @Setter%0A
     
@@ -1422,24 +1422,30 @@
     private 
+final 
 String logic
@@ -1458,20 +1458,8 @@
    %0A
-    @Getter%0A
     
@@ -1550,831 +1550,8 @@
    %0A
-    private final List%3CObject%3E segments;%0A    %0A    private StringBuilder currentSegment;%0A    %0A    public SQLBuilder() %7B%0A        sqlTokens = new LinkedList%3C%3E();%0A        segments = new LinkedList%3C%3E();%0A        currentSegment = new StringBuilder();%0A        segments.add(currentSegment);%0A    %7D%0A    %0A    /**%0A     * Append literals.%0A     *%0A     * @param literals literals for SQL%0A     */%0A    public void appendLiterals(final String literals) %7B%0A        currentSegment.append(literals);%0A    %7D%0A    %0A    /**%0A     * Append sharding placeholder.%0A     *%0A     * @param shardingPlaceholder sharding placeholder%0A     */%0A    public void appendPlaceholder(final ShardingPlaceholder shardingPlaceholder) %7B%0A        segments.add(shardingPlaceholder);%0A        currentSegment = new StringBuilder();%0A        segments.add(currentSegment);%0A    %7D%0A    %0A
     

use RequiredArgsConstructor