/* * Copyright (c) 2011-2020, baomidou (jobob@qq.com). *

* 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 *

* https://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. */ package com.ruoyi.common.numgen; import com.baomidou.mybatisplus.core.metadata.TableInfo; import com.baomidou.mybatisplus.core.metadata.TableInfoHelper; import com.baomidou.mybatisplus.core.toolkit.StringUtils; import com.baomidou.mybatisplus.core.toolkit.support.ColumnCache; import com.baomidou.mybatisplus.core.toolkit.support.SFunction; import java.lang.ref.WeakReference; import java.util.HashMap; import java.util.Map; import java.util.Optional; import java.util.concurrent.ConcurrentHashMap; import static java.util.Locale.ENGLISH; /** * Lambda 解析工具类 * * @author HCL, MieMie * @since 2018-05-10 */ public final class LambdaUtils { /** * 字段映射 */ private static final Map> COLUMN_CACHE_MAP = new ConcurrentHashMap<>(); /** * SerializedLambda 反序列化缓存 */ private static final Map> FUNC_CACHE = new ConcurrentHashMap<>(); /** * 解析 lambda 表达式, 该方法只是调用了 {@link SerializedLambda#resolve(SFunction)} 中的方法,在此基础上加了缓存。 * 该缓存可能会在任意不定的时间被清除 * * @param func 需要解析的 lambda 对象 * @param 类型,被调用的 Function 对象的目标类型 * @return 返回解析后的结果 * @see SerializedLambda#resolve(SFunction) */ public static SerializedLambda resolve(SFunction func) { Class clazz = func.getClass(); String canonicalName = clazz.getCanonicalName(); return Optional.ofNullable(FUNC_CACHE.get(canonicalName)) .map(WeakReference::get) .orElseGet(() -> { SerializedLambda lambda = SerializedLambda.resolve(func); FUNC_CACHE.put(canonicalName, new WeakReference<>(lambda)); return lambda; }); } /** * 格式化 key 将传入的 key 变更为大写格式 * *

     *     Assert.assertEquals("USERID", formatKey("userId"))
     * 
* * @param key key * @return 大写的 key */ public static String formatKey(String key) { return key.toUpperCase(ENGLISH); } /** * 将传入的表信息加入缓存 * * @param tableInfo 表信息 */ public static void installCache(TableInfo tableInfo) { COLUMN_CACHE_MAP.put(tableInfo.getEntityType().getName(), createColumnCacheMap(tableInfo)); } /** * 缓存实体字段 MAP 信息 * * @param info 表信息 * @return 缓存 map */ private static Map createColumnCacheMap(TableInfo info) { Map map = new HashMap<>(); String kp = info.getKeyProperty(); if (StringUtils.isNotBlank(kp)) { map.put(formatKey(kp), new ColumnCache(info.getKeyColumn(), info.getKeySqlSelect())); } info.getFieldList().forEach(i -> map.put(formatKey(i.getProperty()), new ColumnCache(i.getColumn(), i.getSqlSelect())) ); return map; } /** * 获取实体对应字段 MAP * * @param clazz 实体类 * @return 缓存 map */ public static Map getColumnMap(Class clazz) { return COLUMN_CACHE_MAP.computeIfAbsent(clazz.getName(), key -> { TableInfo info = TableInfoHelper.getTableInfo(clazz); return info == null ? null : createColumnCacheMap(info); }); } }