com.dangdang.ddframe.rdb.sharding.parser.result.router.ConditionContext.java Source code

Java tutorial

Introduction

Here is the source code for com.dangdang.ddframe.rdb.sharding.parser.result.router.ConditionContext.java

Source

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

import com.dangdang.ddframe.rdb.sharding.parser.result.router.Condition.BinaryOperator;
import com.dangdang.ddframe.rdb.sharding.parser.result.router.Condition.Column;
import com.google.common.base.Optional;
import lombok.ToString;

import java.util.Collection;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

/**
 * ?.
 * 
 * @author zhangliang
 */
@ToString
public final class ConditionContext {

    private final Map<Column, Condition> conditions = new LinkedHashMap<>();

    /**
     * ?.
     * 
     * @param condition ?
     */
    public void add(final Condition condition) {
        // TODO ??????
        conditions.put(condition.getColumn(), condition);
    }

    /**
     * ?.
     * 
     * @param table ??
     * @param column ??
     * @return ?
     */
    public Optional<Condition> find(final String table, final String column) {
        return Optional.fromNullable(conditions.get(new Column(column, table)));
    }

    /**
     * ?.
     * 
     * @param table ??
     * @param column ??
     * @param operator ?
     * @return ?
     */
    public Optional<Condition> find(final String table, final String column, final BinaryOperator operator) {
        Optional<Condition> result = find(table, column);
        if (!result.isPresent()) {
            return result;
        }
        return result.get().getOperator() == operator ? result : Optional.<Condition>absent();
    }

    public boolean isEmpty() {
        return conditions.isEmpty();
    }

    public void clear() {
        conditions.clear();
    }

    public Collection<Condition> getAllConditions() {
        return conditions.values();
    }

    /**
     * ???.
     * 
     * @param parameters ?
     */
    public void setNewConditionValue(final List<Object> parameters) {
        for (Condition each : conditions.values()) {
            if (each.getValueIndices().isEmpty()) {
                continue;
            }
            for (int i = 0; i < each.getValueIndices().size(); i++) {
                Object value = parameters.get(each.getValueIndices().get(i));
                if (value instanceof Comparable<?>) {
                    each.getValues().set(i, (Comparable<?>) value);
                } else {
                    each.getValues().set(i, "");
                }
            }
        }
    }
}