001/**
002 * Copyright (c) 2015-2022, Michael Yang 杨福海 (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 io.jboot.db.model;
017
018import java.util.Map;
019import java.util.concurrent.ConcurrentHashMap;
020
021public class JbootModelExts {
022
023    private static Map<Integer, Map<String, Object>> extAttrs = new ConcurrentHashMap<>();
024
025    private static final String DISTINCT = "distinct";
026
027
028    public static Object getExtAttr(Object holder, String attr) {
029        Map<String, Object> map = extAttrs.get(System.identityHashCode(holder));
030        return map != null ? map.get(attr) : null;
031    }
032
033
034    public static void setExtAttr(Object holder, String attr, Object value) {
035        Map<String, Object> map = extAttrs.get(System.identityHashCode(holder));
036        if (map == null) {
037            synchronized (holder) {
038                map = extAttrs.get(System.identityHashCode(holder));
039                if (map == null) {
040                    map = new ConcurrentHashMap<>();
041                    extAttrs.put(System.identityHashCode(holder), map);
042                }
043            }
044        }
045
046        map.put(attr, value);
047    }
048
049
050    public static void setDistinctColumn(JbootModel<?> holder, String column) {
051        setExtAttr(holder, DISTINCT, column);
052    }
053
054    public static String getDistinctColumn(JbootModel<?> holder) {
055        return (String) getExtAttr(holder, DISTINCT);
056    }
057
058
059    public static void setDatasourceDAO(JbootModel<?> holder, String key, JbootModel datasourceDAO) {
060        setExtAttr(holder, key, datasourceDAO);
061    }
062
063    public static <T> T getDatasourceDAO(JbootModel<?> holder, String key) {
064        return (T) getExtAttr(holder, key);
065    }
066
067
068}