From 3974e77aa6a554764559a6591a6c44e4637b848a Mon Sep 17 00:00:00 2001 From: Kould <91525956+KKould@users.noreply.github.com> Date: Fri, 1 Jul 2022 21:58:56 +0800 Subject: [PATCH] =?UTF-8?q?style(utils):=20=E5=91=BD=E5=90=8D=E4=BC=98?= =?UTF-8?q?=E5=8C=96=E3=80=81=E6=B3=A8=E9=87=8A=E8=A1=A5=E5=85=A8?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 个别工具命名优化、注释补全,BeanCopierUtils的getBeanCopier方法实现优化 --- .../com/rymcu/forest/util/BaiDuUtils.java | 23 +++--- .../com/rymcu/forest/util/BeanCopierUtil.java | 58 +++++++------- .../com/rymcu/forest/util/CacheUtils.java | 76 +++++++++---------- .../rymcu/forest/util/ContextHolderUtils.java | 17 ++--- .../java/com/rymcu/forest/util/DateUtil.java | 13 ++-- .../java/com/rymcu/forest/util/Digests.java | 4 +- .../java/com/rymcu/forest/util/ErrorCode.java | 4 +- 7 files changed, 91 insertions(+), 104 deletions(-) diff --git a/src/main/java/com/rymcu/forest/util/BaiDuUtils.java b/src/main/java/com/rymcu/forest/util/BaiDuUtils.java index a85d72e..3e943aa 100644 --- a/src/main/java/com/rymcu/forest/util/BaiDuUtils.java +++ b/src/main/java/com/rymcu/forest/util/BaiDuUtils.java @@ -11,18 +11,18 @@ import java.util.concurrent.*; */ public class BaiDuUtils { - private final static String token = "9cdKR6bVCJzxDEJS"; + private final static String TOKEN = "9cdKR6bVCJzxDEJS"; - private final static String site = "https://rymcu.com"; + private final static String SITE = "https://rymcu.com"; public static void sendSEOData(String permalink) { - if (StringUtils.isBlank(permalink) || StringUtils.isBlank(token)) { + if (StringUtils.isBlank(permalink) || StringUtils.isBlank(TOKEN)) { return; } - ExecutorService executor= new ThreadPoolExecutor(1,1,0L, TimeUnit.MILLISECONDS, new LinkedBlockingQueue()); + ExecutorService executor= new ThreadPoolExecutor(1,1,0L, TimeUnit.MILLISECONDS, new LinkedBlockingQueue<>()); CompletableFuture.supplyAsync(()-> { try { - HttpResponse response = HttpRequest.post("http://data.zz.baidu.com/urls?site=" + site + "&token=" + token). + HttpResponse response = HttpRequest.post("http://data.zz.baidu.com/urls?site=" + SITE + "&token=" + TOKEN). header("User-Agent", "curl/7.12.1"). header("Host", "data.zz.baidu.com"). header("Content-Type", "text/plain"). @@ -34,17 +34,16 @@ public class BaiDuUtils { } return 0; },executor); - return; } public static void sendUpdateSEOData(String permalink) { - if (StringUtils.isBlank(permalink) || StringUtils.isBlank(token)) { + if (StringUtils.isBlank(permalink) || StringUtils.isBlank(TOKEN)) { return; } - ExecutorService executor= new ThreadPoolExecutor(1,1,0L, TimeUnit.MILLISECONDS, new LinkedBlockingQueue()); + ExecutorService executor= new ThreadPoolExecutor(1,1,0L, TimeUnit.MILLISECONDS, new LinkedBlockingQueue<>()); CompletableFuture.supplyAsync(()-> { try { - HttpResponse response = HttpRequest.post("http://data.zz.baidu.com/update?site=" + site + "&token=" + token). + HttpResponse response = HttpRequest.post("http://data.zz.baidu.com/update?site=" + SITE + "&token=" + TOKEN). header("User-Agent", "curl/7.12.1"). header("Host", "data.zz.baidu.com"). header("Content-Type", "text/plain"). @@ -59,13 +58,13 @@ public class BaiDuUtils { } public static void deleteSEOData(String permalink) { - if (StringUtils.isBlank(permalink) || StringUtils.isBlank(token)) { + if (StringUtils.isBlank(permalink) || StringUtils.isBlank(TOKEN)) { return; } - ExecutorService executor= new ThreadPoolExecutor(1,1,0L, TimeUnit.MILLISECONDS, new LinkedBlockingQueue()); + ExecutorService executor= new ThreadPoolExecutor(1,1,0L, TimeUnit.MILLISECONDS, new LinkedBlockingQueue<>()); CompletableFuture.supplyAsync(()-> { try { - HttpResponse response = HttpRequest.post("http://data.zz.baidu.com/del?site=" + site + "&token=" + token). + HttpResponse response = HttpRequest.post("http://data.zz.baidu.com/del?site=" + SITE + "&token=" + TOKEN). header("User-Agent", "curl/7.12.1"). header("Host", "data.zz.baidu.com"). header("Content-Type", "text/plain"). diff --git a/src/main/java/com/rymcu/forest/util/BeanCopierUtil.java b/src/main/java/com/rymcu/forest/util/BeanCopierUtil.java index 6186f19..230d76d 100644 --- a/src/main/java/com/rymcu/forest/util/BeanCopierUtil.java +++ b/src/main/java/com/rymcu/forest/util/BeanCopierUtil.java @@ -2,7 +2,6 @@ package com.rymcu.forest.util; import lombok.extern.slf4j.Slf4j; import org.springframework.cglib.beans.BeanCopier; -import org.springframework.util.CollectionUtils; import java.util.Map; import java.util.concurrent.ConcurrentHashMap; @@ -16,14 +15,14 @@ public class BeanCopierUtil { * beanCopier缓存 * (A拷贝到B,确定一个beanCopier) */ - private static Map,Map, BeanCopier>> beanCopierMap = new ConcurrentHashMap<>(); + private static final Map BEAN_COPIER_MAP = new ConcurrentHashMap<>(); /** * 拷贝方法 - * @param sourceBean - * @param targetBean - * @param - * @param + * @param sourceBean 源对象 + * @param targetBean 目标对象 + * @param 源对象类型 + * @param 目标对象类型 */ public static void copy(S sourceBean,T targetBean){ @SuppressWarnings("unchecked") @@ -37,16 +36,15 @@ public class BeanCopierUtil { /** * 转换方法 - * @param sourceBean 原对象 - * @param targetClass 目标类 - * @param - * @param - * @return + * @param sourceBean 源对象 + * @param targetBean 目标对象 + * @param 源对象类型 + * @param 目标对象类型 + * @return 拷贝值后的targetBean */ - public static T convert(S sourceBean,Class targetClass){ + public static T convert(S sourceBean,T targetBean){ try { assert sourceBean!=null; - T targetBean = targetClass.newInstance(); copy(sourceBean,targetBean); return targetBean; } catch (Exception e) { @@ -56,24 +54,20 @@ public class BeanCopierUtil { } - private static BeanCopier getBeanCopier(Class sourceClass, Class targetClass ){ - Map, BeanCopier> map = beanCopierMap.get(sourceClass); - if(CollectionUtils.isEmpty(map)){ - BeanCopier newBeanCopier = BeanCopier.create(sourceClass, targetClass, false); - Map, BeanCopier> newMap = new ConcurrentHashMap<>(); - newMap.put(targetClass,newBeanCopier); - beanCopierMap.put(sourceClass,newMap); - return newBeanCopier; - } - - BeanCopier beanCopier = map.get(targetClass); - if(beanCopier == null){ - BeanCopier newBeanCopier = BeanCopier.create(sourceClass, targetClass, false); - map.put(targetClass,newBeanCopier); - - return newBeanCopier; - } - - return beanCopier; + /** + * beanCopier获取方法 + * + * 使用beanCopierMap重用BeanCopier对象 + * + * 线程安全 + * @param sourceClass 源类型 + * @param targetClass 目标类型 + * @param 源类型 + * @param 目标类型 + * @return BeanCopier实例 + */ + private static BeanCopier getBeanCopier(Class sourceClass, Class targetClass){ + String classKey = sourceClass.getTypeName() + targetClass.getTypeName(); + return BEAN_COPIER_MAP.computeIfAbsent(classKey, key -> BeanCopier.create(sourceClass, targetClass, false)); } } diff --git a/src/main/java/com/rymcu/forest/util/CacheUtils.java b/src/main/java/com/rymcu/forest/util/CacheUtils.java index 2e99989..bac94a2 100644 --- a/src/main/java/com/rymcu/forest/util/CacheUtils.java +++ b/src/main/java/com/rymcu/forest/util/CacheUtils.java @@ -5,7 +5,6 @@ import org.apache.shiro.cache.CacheManager; import org.slf4j.Logger; import org.slf4j.LoggerFactory; -import java.util.Iterator; import java.util.Set; /** @@ -13,15 +12,15 @@ import java.util.Set; */ public class CacheUtils { - private static Logger logger = LoggerFactory.getLogger(CacheUtils.class); - private static CacheManager cacheManager = SpringContextHolder.getBean(CacheManager.class); + private static final Logger LOGGER = LoggerFactory.getLogger(CacheUtils.class); + private static final CacheManager CACHE_MANAGER = SpringContextHolder.getBean(CacheManager.class); private static final String SYS_CACHE = "system"; /** * 获取SYS_CACHE缓存 - * @param key - * @return + * @param key 键值 + * @return 缓存对象 */ public static Object get(String key) { return get(SYS_CACHE, key); @@ -29,9 +28,9 @@ public class CacheUtils { /** * 获取SYS_CACHE缓存 - * @param key - * @param defaultValue - * @return + * @param key 键值 + * @param defaultValue 默认返回值 + * @return 缓存对象或默认值 */ public static Object get(String key, Object defaultValue) { Object value = get(key); @@ -40,8 +39,8 @@ public class CacheUtils { /** * 写入SYS_CACHE缓存 - * @param key - * @return + * @param key 键值 + * @param value 被缓存对象 */ public static void put(String key, Object value) { put(SYS_CACHE, key, value); @@ -49,29 +48,28 @@ public class CacheUtils { /** * 从SYS_CACHE缓存中移除 - * @param key - * @return + * @param key 键值 */ public static void remove(String key) { remove(SYS_CACHE, key); } /** - * 获取缓存 - * @param cacheName - * @param key - * @return + * 获取指定命名空间下的缓存 + * @param cacheName 缓存命名空间 + * @param key 键值 + * @return 该命名空间下的对应键值缓存 */ public static Object get(String cacheName, String key) { return getCache(cacheName).get(getKey(key)); } /** - * 获取缓存 - * @param cacheName - * @param key - * @param defaultValue - * @return + * 获取指定命名空间下的缓存,并在无缓存对象时返回默认值 + * @param cacheName 缓存命名空间 + * @param key 键值 + * @param defaultValue 默认返回值 + * @return 缓存对象或默认值 */ public static Object get(String cacheName, String key, Object defaultValue) { Object value = get(cacheName, getKey(key)); @@ -79,41 +77,39 @@ public class CacheUtils { } /** - * 写入缓存 - * @param cacheName - * @param key - * @param value + * 向指定命名空间下存入被缓存对象 + * @param cacheName 缓存命名空间 + * @param key 键值 + * @param value 该命名空间下被缓存对象 */ public static void put(String cacheName, String key, Object value) { getCache(cacheName).put(getKey(key), value); } /** - * 从缓存中移除 - * @param cacheName - * @param key + * 将指定命名空间下的缓存移除 + * @param cacheName 缓存命名空间 + * @param key 键值 */ public static void remove(String cacheName, String key) { getCache(cacheName).remove(getKey(key)); } /** - * 从缓存中移除所有 - * @param cacheName + * 清空命名空间下所有缓存 + * @param cacheName 命名空间 */ public static void removeAll(String cacheName) { Cache cache = getCache(cacheName); Set keys = cache.keys(); - for (Iterator it = keys.iterator(); it.hasNext();){ - cache.remove(it.next()); - } - logger.info("清理缓存: {} => {}", cacheName, keys); + cache.clear(); + LOGGER.info("清理缓存: {} => {}", cacheName, keys); } /** * 获取缓存键名,多数据源下增加数据源名称前缀 - * @param key - * @return + * @param key 键值 + * @return 返回对应数据源前缀拼接键值 */ private static String getKey(String key){ // String dsName = DataSourceHolder.getDataSourceName(); @@ -124,12 +120,12 @@ public class CacheUtils { } /** - * 获得一个Cache,没有则显示日志。 - * @param cacheName - * @return + * 获得指定命名空间的Cache,没有则显示日志。 + * @param cacheName 命名空间 + * @return 命名空间对应的Cache */ private static Cache getCache(String cacheName){ - Cache cache = cacheManager.getCache(cacheName); + Cache cache = CACHE_MANAGER.getCache(cacheName); if (cache == null){ throw new RuntimeException("当前系统中没有定义“"+cacheName+"”这个缓存。"); } diff --git a/src/main/java/com/rymcu/forest/util/ContextHolderUtils.java b/src/main/java/com/rymcu/forest/util/ContextHolderUtils.java index e8fa42b..57c474e 100644 --- a/src/main/java/com/rymcu/forest/util/ContextHolderUtils.java +++ b/src/main/java/com/rymcu/forest/util/ContextHolderUtils.java @@ -8,6 +8,7 @@ import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpSession; import java.util.HashMap; import java.util.Map; +import java.util.Objects; /** * @ClassName: ContextHolderUtils @@ -17,24 +18,22 @@ import java.util.Map; * */ public class ContextHolderUtils { - private static final Map sessionMap = new HashMap(); + private static final Map sessionMap = new HashMap<>(); /** * SpringMvc下获取request * - * @return + * @return HttpServletRequest */ public static HttpServletRequest getRequest() { - - HttpServletRequest request = ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getRequest(); - return request; + return ((ServletRequestAttributes) Objects.requireNonNull(RequestContextHolder.getRequestAttributes())).getRequest(); } /** * SpringMvc下获取session * - * @return + * @return HttpSession */ public static HttpSession getSession() { HttpServletRequest request = getRequest(); @@ -54,7 +53,7 @@ public class ContextHolderUtils { } public static HttpSession getSession2() { - HttpServletRequest request = ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getRequest(); + HttpServletRequest request = ((ServletRequestAttributes) Objects.requireNonNull(RequestContextHolder.getRequestAttributes())).getRequest(); return request.getSession(); } @@ -65,9 +64,7 @@ public class ContextHolderUtils { } public static void removeSession(String sessionId){ - if(sessionMap.containsKey(sessionId)){ - sessionMap.remove(sessionId); - } + sessionMap.remove(sessionId); } } diff --git a/src/main/java/com/rymcu/forest/util/DateUtil.java b/src/main/java/com/rymcu/forest/util/DateUtil.java index 3da5633..3dff51e 100644 --- a/src/main/java/com/rymcu/forest/util/DateUtil.java +++ b/src/main/java/com/rymcu/forest/util/DateUtil.java @@ -13,22 +13,23 @@ public class DateUtil { if (StringUtils.isBlank(s)) { s = ""; } - for (int i = 0; i < len - s.length(); ++i) { - s = "0" + s; + StringBuilder sBuilder = new StringBuilder(s); + for (int i = 0; i < len - sBuilder.length(); ++i) { + sBuilder.insert(0, "0"); } - return s; + return sBuilder.toString(); } public static String getYear(Calendar cal) { - return String.valueOf(cal.get(1)); + return String.valueOf(cal.get(Calendar.YEAR)); } public static String getMonth(Calendar cal) { - return strLen(String.valueOf(cal.get(2) + 1), 2); + return strLen(String.valueOf(cal.get(Calendar.MONTH) + 1), 2); } public static String getDay(Calendar cal) { - return strLen(String.valueOf(cal.get(5)), 2); + return strLen(String.valueOf(cal.get(Calendar.DATE)), 2); } public static String getNowDateNum() { diff --git a/src/main/java/com/rymcu/forest/util/Digests.java b/src/main/java/com/rymcu/forest/util/Digests.java index 52464cf..f6f6544 100644 --- a/src/main/java/com/rymcu/forest/util/Digests.java +++ b/src/main/java/com/rymcu/forest/util/Digests.java @@ -20,7 +20,7 @@ public class Digests { private static final String SHA1 = "SHA-1"; private static final String MD5 = "MD5"; - private static SecureRandom random = new SecureRandom(); + private static final SecureRandom SECURE_RANDOM = new SecureRandom(); /** * 对输入字符串进行md5散列. @@ -79,7 +79,7 @@ public class Digests { Validate.isTrue(numBytes > 0, "numBytes argument must be a positive integer (1 or larger)", numBytes); byte[] bytes = new byte[numBytes]; - random.nextBytes(bytes); + SECURE_RANDOM.nextBytes(bytes); return bytes; } diff --git a/src/main/java/com/rymcu/forest/util/ErrorCode.java b/src/main/java/com/rymcu/forest/util/ErrorCode.java index 79f818e..839d1bd 100644 --- a/src/main/java/com/rymcu/forest/util/ErrorCode.java +++ b/src/main/java/com/rymcu/forest/util/ErrorCode.java @@ -17,9 +17,9 @@ public enum ErrorCode { this.message = message; } - private String code; + private final String code; - private String message; + private final String message; public String getCode() { return code;