001/*
002 *  Copyright (c) 2022-2023, Mybatis-Flex (fuhai999@gmail.com).
003 *  <p>
004 *  Licensed under the Apache License, Version 2.0 (the "License");
005 *  you may not use this file except in compliance with the License.
006 *  You may obtain a copy of the License at
007 *  <p>
008 *  http://www.apache.org/licenses/LICENSE-2.0
009 *  <p>
010 *  Unless required by applicable law or agreed to in writing, software
011 *  distributed under the License is distributed on an "AS IS" BASIS,
012 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013 *  See the License for the specific language governing permissions and
014 *  limitations under the License.
015 */
016package com.mybatisflex.core.query;
017
018import com.mybatisflex.core.constant.SqlOperator;
019import com.mybatisflex.core.util.LambdaGetter;
020import com.mybatisflex.core.util.LambdaUtil;
021
022import java.util.HashMap;
023
024/**
025 * @author michael
026 */
027public class SqlOperators extends HashMap<String, SqlOperator> {
028
029    private final static SqlOperators EMPTY = new SqlOperators();
030
031    static SqlOperators empty() {
032        return EMPTY;
033    }
034
035    public static SqlOperators of() {
036        return new SqlOperators();
037    }
038
039    public static <T> SqlOperators of( LambdaGetter<T> getter, SqlOperator operator ) {
040        SqlOperators map = new SqlOperators(1);
041        map.put(LambdaUtil.getFieldName(getter), operator);
042        return map;
043    }
044
045    public static <T> SqlOperators of(String fieldName, SqlOperator operator) {
046        SqlOperators map = new SqlOperators(1);
047        map.put(fieldName, operator);
048        return map;
049    }
050
051    public SqlOperators() {
052    }
053
054    public SqlOperators(int initialCapacity) {
055        super(initialCapacity);
056    }
057
058    public <T> SqlOperators set(LambdaGetter<T> getter, SqlOperator operator) {
059        this.put(LambdaUtil.getFieldName(getter), operator);
060        return this;
061    }
062
063    public SqlOperators set(String fieldName, SqlOperator operator) {
064        this.put(fieldName, operator);
065        return this;
066    }
067
068    public SqlOperators set(QueryColumn column, SqlOperator operator) {
069        this.put(column.getName(), operator);
070        return this;
071    }
072
073}