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 static final SqlOperators EMPTY = new SqlOperators() { 030 @Override 031 public SqlOperator put(String key, SqlOperator value) { 032 throw new IllegalArgumentException("Can not set SqlOperator for \"empty\" SqlOperators"); 033 } 034 }; 035 036 public static SqlOperators empty() { 037 return EMPTY; 038 } 039 040 public static SqlOperators of() { 041 return new SqlOperators(); 042 } 043 044 public static <T> SqlOperators of(LambdaGetter<T> getter, SqlOperator operator) { 045 SqlOperators map = new SqlOperators(1); 046 map.put(LambdaUtil.getFieldName(getter), operator); 047 return map; 048 } 049 050 public static <T> SqlOperators of(String fieldName, SqlOperator operator) { 051 SqlOperators map = new SqlOperators(1); 052 map.put(fieldName, operator); 053 return map; 054 } 055 056 public SqlOperators() { 057 } 058 059 public SqlOperators(int initialCapacity) { 060 super(initialCapacity); 061 } 062 063 public SqlOperators(SqlOperators sqlOperators) { 064 this.putAll(sqlOperators); 065 } 066 067 068 public <T> SqlOperators set(LambdaGetter<T> getter, SqlOperator operator) { 069 this.put(LambdaUtil.getFieldName(getter), operator); 070 return this; 071 } 072 073 public SqlOperators set(String fieldName, SqlOperator operator) { 074 this.put(fieldName, operator); 075 return this; 076 } 077 078 public SqlOperators set(QueryColumn column, SqlOperator operator) { 079 this.put(column.getName(), operator); 080 return this; 081 } 082 083}