package com.kx.utils.id; import lombok.extern.slf4j.Slf4j; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.core.annotation.Order; import org.springframework.stereotype.Component; import org.springframework.util.StringUtils; import javax.annotation.PostConstruct; import java.util.LinkedHashMap; 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 idGeneratorList; protected Map algorithmMap = new ConcurrentHashMap<>(8); @PostConstruct public void init(){ if (idGeneratorList!= null ){ // name , order, index --> 只保存order最大时的 name对应的idgen Map 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() ); }); } } /** * 注册生成算法 * @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 supportAlgorithms(){ return this.algorithmMap.keySet(); } }