AbstractOrASTNode.java 3.6 KB
Newer Older
T
terrymanu 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38
/**
 * Copyright 1999-2015 dangdang.com.
 * <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 com.dangdang.ddframe.rdb.sharding.parser.visitor.or.node;

import java.util.ArrayList;
import java.util.List;

import com.dangdang.ddframe.rdb.sharding.parser.result.router.Condition;
import com.dangdang.ddframe.rdb.sharding.parser.result.router.ConditionContext;
import com.google.common.base.Function;
import com.google.common.collect.Lists;

import lombok.AccessLevel;
import lombok.Getter;

/**
 * 抽象的OR语法树节点.
 * 
 * @author gaohongtao
 */
@Getter(AccessLevel.PROTECTED)
public abstract class AbstractOrASTNode {
    
T
terrymanu 已提交
39
    private final List<AbstractOrASTNode> subNodes = new ArrayList<>();
T
terrymanu 已提交
40
    
T
terrymanu 已提交
41
    private final List<List<Condition>> nestedConditions = new ArrayList<>();
T
terrymanu 已提交
42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107
    
    public final void addSubNode(final AbstractOrASTNode node) {
        subNodes.add(node);
    }
    
    protected final void addNestedConditions(final ConditionContext conditionContext) {
        nestedConditions.add(Lists.newArrayList(conditionContext.getAllConditions()));
    }
    
    /**
     * 使用该节点作为根节点生成抽象语法树.
     * 
     * <p>
     * 使用深度优先后续的方式生成语法树.
     * 其中后续遍历是由于DRUID进行SQL语法解析时产生的行为.
     * </p>
     */
    public abstract void createOrASTAsRootNode();
    
    /**
     * 获取解析结果需要的条件.
     * 
     * @return 解析后的条件
     */
    public final List<ConditionContext> getCondition() {
        return Lists.transform(nestedConditions, new Function<List<Condition>, ConditionContext>() {
            
            @Override
            public ConditionContext apply(final List<Condition> input) {
                ConditionContext result = new ConditionContext();
                for (Condition each : input) {
                    result.add(each);
                }
                return result;
            }
        });
    }
    
    /**
     * 多个子节点之间做笛卡尔积.
     */
    protected final void mergeSubConditions() {
        if (subNodes.isEmpty()) {
            return;
        }
        List<List<Condition>> result = new ArrayList<>();
        result.addAll(subNodes.get(0).getNestedConditions());
        for (int i = 1; i < subNodes.size(); i++) {
            result = cartesianNestedConditions(result, subNodes.get(i).getNestedConditions());
        }
        nestedConditions.addAll(result);
    }
    
    private List<List<Condition>> cartesianNestedConditions(final List<List<Condition>> oneNestedConditions, final List<List<Condition>> anotherNestedConditions) {
        List<List<Condition>> result = new ArrayList<>();
        for (List<Condition> oneNestedCondition : oneNestedConditions) {
            for (List<Condition> anotherNestedCondition : anotherNestedConditions) {
                List<Condition> mergedConditions = new ArrayList<>();
                mergedConditions.addAll(oneNestedCondition);
                mergedConditions.addAll(anotherNestedCondition);
                result.add(mergedConditions);
            }
        }
        return result;
    }
}