IdAlgorithmRegister.java 4.3 KB
Newer Older
1 2 3 4
package com.kx.utils.id;

import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
5
import org.springframework.core.annotation.Order;
6 7 8 9
import org.springframework.stereotype.Component;
import org.springframework.util.StringUtils;

import javax.annotation.PostConstruct;
10
import java.util.LinkedHashMap;
11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.concurrent.ConcurrentHashMap;
/**
 * id算法注册器
 * @author kongxiang
 */
@Component
@Slf4j
public class IdAlgorithmRegister {
    /**
     * 获取所有类型是 IdGenerator
     */
    @Autowired
    private List<? extends IdGenerator> idGeneratorList;

    protected Map<String ,IdGenerator> algorithmMap = new ConcurrentHashMap<>(8);

    @PostConstruct
    public void  init(){
        if (idGeneratorList!= null ){
33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75
            // name , order, index --> 只保存order最大时的 name对应的idgen
            Map<String,Integer[]> nameOrderMap = new LinkedHashMap<>(idGeneratorList.size());
            for (int i = 0; i < idGeneratorList.size(); i++) {
                IdGenerator idGenerator = idGeneratorList.get(i);
                String algorithm = idGenerator.getAlgorithm();

                int orderValue = 0;
                if (idGenerator.getClass().isAnnotationPresent(Order.class)){
                    Order order = idGenerator.getClass().getDeclaredAnnotation(Order.class);
                    orderValue = order.value();
                    if (StringUtils.isEmpty(orderValue)){
                        orderValue = 0 ;
                    }
                }

                // 选取最大order进行存入
                if (!nameOrderMap.containsKey(algorithm)){
                    Integer[] orderIndexArr = new Integer[2];
                    orderIndexArr[1] = i;
                    orderIndexArr[0] = orderValue;
                    nameOrderMap.put(algorithm,orderIndexArr);
                }else {
                    Integer[] arr = nameOrderMap.get(algorithm);
                    if (arr[0] < orderValue){
                        arr[0] = orderValue;// 取较大的order
                        arr[1] = i; // 更新下表
                       // nameOrderMap.put(algorithm,arr) ;
                    }else if (arr[0 ] == orderValue){
                        // 如果order一致,比较类名的字符串字典大小,取大值
                        String up = idGeneratorList.get(arr[0]).getClass().getSimpleName();
                        String thisGen = idGenerator.getClass().getSimpleName();
                        if (up.compareToIgnoreCase(thisGen) < 0 ){
                            arr[0] = orderValue;
                            arr[1] = i ;
                        }
                    }
                }
            }

            nameOrderMap.forEach((name,arr) -> {
                IdGenerator idGenerator = idGeneratorList.get(arr[1]);
                algorithmMap.put(name,idGenerator);
               log.info(">> Load id generator successful -- : algorithm :  [ {} ]  -- order [{}]  --  class : [{}] ",name,arr[0] , idGenerator.getClass() );
76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126
            });
        }
    }


    /**
     * 注册生成算法
     * @param algorithm
     * @param idGenerator
     * @return 注册结果
     */
    protected boolean register(String algorithm, IdGenerator idGenerator){
        boolean registerStatus = false;
        if (!StringUtils.isEmpty(algorithm) && idGenerator != null ){
            algorithmMap.put(algorithm,idGenerator);
            registerStatus = true;
        }
        return registerStatus;
    }

    /**
     * 获取算法生成器类
     * @param algorithm 算法
     * @return 算法生成器
     */
    public IdGenerator getAlgorithm(String algorithm){
        IdGenerator idGenerator = this.algorithmMap.get(algorithm);
        if (idGenerator == null ){
            throw new UnsupportedOperationException("id generate is not supported : " + algorithm);
        }
        return idGenerator;
    }

    /**
     * 是否存有算法生成器
     * @param algorithm 算法
     * @return
     */
    protected boolean containAlgorithm(String algorithm){
        return this.algorithmMap.containsKey(algorithm);
    }

    /**
     * 列出所有支持的算法
     * @return
     */
    protected Set<String> supportAlgorithms(){
        return this.algorithmMap.keySet();
    }

}