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(
040        LambdaGetter<T> getter, SqlOperator operator
041    ) {
042        SqlOperators map = new SqlOperators(1);
043        map.put(LambdaUtil.getFieldName(getter), operator);
044        return map;
045    }
046
047    public static <T> SqlOperators of(
048        String fieldName, SqlOperator operator
049    ) {
050        SqlOperators map = new SqlOperators(1);
051        map.put(fieldName, operator);
052        return map;
053    }
054
055    public SqlOperators() {
056    }
057
058    public SqlOperators(int initialCapacity) {
059        super(initialCapacity);
060    }
061
062    public <T> SqlOperators set(LambdaGetter<T> getter, SqlOperator operator) {
063        this.put(LambdaUtil.getFieldName(getter), operator);
064        return this;
065    }
066
067    public <T> SqlOperators set(String fieldName, SqlOperator operator) {
068        this.put(fieldName, operator);
069        return this;
070    }
071}