Merge pull request #95 from KKould/wx-dev

🎨 style(utils): 命名优化、注释补全
This commit is contained in:
ronger 2022-07-02 07:09:16 +08:00 committed by GitHub
commit e9dbda42b0
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
7 changed files with 91 additions and 104 deletions

View File

@ -11,18 +11,18 @@ import java.util.concurrent.*;
*/ */
public class BaiDuUtils { 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) { public static void sendSEOData(String permalink) {
if (StringUtils.isBlank(permalink) || StringUtils.isBlank(token)) { if (StringUtils.isBlank(permalink) || StringUtils.isBlank(TOKEN)) {
return; return;
} }
ExecutorService executor= new ThreadPoolExecutor(1,1,0L, TimeUnit.MILLISECONDS, new LinkedBlockingQueue<Runnable>()); ExecutorService executor= new ThreadPoolExecutor(1,1,0L, TimeUnit.MILLISECONDS, new LinkedBlockingQueue<>());
CompletableFuture.supplyAsync(()-> { CompletableFuture.supplyAsync(()-> {
try { 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("User-Agent", "curl/7.12.1").
header("Host", "data.zz.baidu.com"). header("Host", "data.zz.baidu.com").
header("Content-Type", "text/plain"). header("Content-Type", "text/plain").
@ -34,17 +34,16 @@ public class BaiDuUtils {
} }
return 0; return 0;
},executor); },executor);
return;
} }
public static void sendUpdateSEOData(String permalink) { public static void sendUpdateSEOData(String permalink) {
if (StringUtils.isBlank(permalink) || StringUtils.isBlank(token)) { if (StringUtils.isBlank(permalink) || StringUtils.isBlank(TOKEN)) {
return; return;
} }
ExecutorService executor= new ThreadPoolExecutor(1,1,0L, TimeUnit.MILLISECONDS, new LinkedBlockingQueue<Runnable>()); ExecutorService executor= new ThreadPoolExecutor(1,1,0L, TimeUnit.MILLISECONDS, new LinkedBlockingQueue<>());
CompletableFuture.supplyAsync(()-> { CompletableFuture.supplyAsync(()-> {
try { 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("User-Agent", "curl/7.12.1").
header("Host", "data.zz.baidu.com"). header("Host", "data.zz.baidu.com").
header("Content-Type", "text/plain"). header("Content-Type", "text/plain").
@ -59,13 +58,13 @@ public class BaiDuUtils {
} }
public static void deleteSEOData(String permalink) { public static void deleteSEOData(String permalink) {
if (StringUtils.isBlank(permalink) || StringUtils.isBlank(token)) { if (StringUtils.isBlank(permalink) || StringUtils.isBlank(TOKEN)) {
return; return;
} }
ExecutorService executor= new ThreadPoolExecutor(1,1,0L, TimeUnit.MILLISECONDS, new LinkedBlockingQueue<Runnable>()); ExecutorService executor= new ThreadPoolExecutor(1,1,0L, TimeUnit.MILLISECONDS, new LinkedBlockingQueue<>());
CompletableFuture.supplyAsync(()-> { CompletableFuture.supplyAsync(()-> {
try { 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("User-Agent", "curl/7.12.1").
header("Host", "data.zz.baidu.com"). header("Host", "data.zz.baidu.com").
header("Content-Type", "text/plain"). header("Content-Type", "text/plain").

View File

@ -2,7 +2,6 @@ package com.rymcu.forest.util;
import lombok.extern.slf4j.Slf4j; import lombok.extern.slf4j.Slf4j;
import org.springframework.cglib.beans.BeanCopier; import org.springframework.cglib.beans.BeanCopier;
import org.springframework.util.CollectionUtils;
import java.util.Map; import java.util.Map;
import java.util.concurrent.ConcurrentHashMap; import java.util.concurrent.ConcurrentHashMap;
@ -16,14 +15,14 @@ public class BeanCopierUtil {
* beanCopier缓存 * beanCopier缓存
* (A拷贝到B,确定一个beanCopier) * (A拷贝到B,确定一个beanCopier)
*/ */
private static Map<Class<?>,Map<Class<?>, BeanCopier>> beanCopierMap = new ConcurrentHashMap<>(); private static final Map<String, BeanCopier> BEAN_COPIER_MAP = new ConcurrentHashMap<>();
/** /**
* 拷贝方法 * 拷贝方法
* @param sourceBean * @param sourceBean 源对象
* @param targetBean * @param targetBean 目标对象
* @param <S> * @param <S> 源对象类型
* @param <T> * @param <T> 目标对象类型
*/ */
public static <S,T> void copy(S sourceBean,T targetBean){ public static <S,T> void copy(S sourceBean,T targetBean){
@SuppressWarnings("unchecked") @SuppressWarnings("unchecked")
@ -37,16 +36,15 @@ public class BeanCopierUtil {
/** /**
* 转换方法 * 转换方法
* @param sourceBean 对象 * @param sourceBean 对象
* @param targetClass 目标类 * @param targetBean 目标对象
* @param <S> * @param <S> 源对象类型
* @param <T> * @param <T> 目标对象类型
* @return * @return 拷贝值后的targetBean
*/ */
public static <S,T> T convert(S sourceBean,Class<T> targetClass){ public static <S,T> T convert(S sourceBean,T targetBean){
try { try {
assert sourceBean!=null; assert sourceBean!=null;
T targetBean = targetClass.newInstance();
copy(sourceBean,targetBean); copy(sourceBean,targetBean);
return targetBean; return targetBean;
} catch (Exception e) { } catch (Exception e) {
@ -56,24 +54,20 @@ public class BeanCopierUtil {
} }
private static <S,T> BeanCopier getBeanCopier(Class<S> sourceClass, Class<T> targetClass ){ /**
Map<Class<?>, BeanCopier> map = beanCopierMap.get(sourceClass); * beanCopier获取方法
if(CollectionUtils.isEmpty(map)){ *
BeanCopier newBeanCopier = BeanCopier.create(sourceClass, targetClass, false); * 使用beanCopierMap重用BeanCopier对象
Map<Class<?>, BeanCopier> newMap = new ConcurrentHashMap<>(); *
newMap.put(targetClass,newBeanCopier); * 线程安全
beanCopierMap.put(sourceClass,newMap); * @param sourceClass 源类型
return newBeanCopier; * @param targetClass 目标类型
} * @param <S> 源类型
* @param <T> 目标类型
BeanCopier beanCopier = map.get(targetClass); * @return BeanCopier实例
if(beanCopier == null){ */
BeanCopier newBeanCopier = BeanCopier.create(sourceClass, targetClass, false); private static <S,T> BeanCopier getBeanCopier(Class<S> sourceClass, Class<T> targetClass){
map.put(targetClass,newBeanCopier); String classKey = sourceClass.getTypeName() + targetClass.getTypeName();
return BEAN_COPIER_MAP.computeIfAbsent(classKey, key -> BeanCopier.create(sourceClass, targetClass, false));
return newBeanCopier;
}
return beanCopier;
} }
} }

View File

@ -5,7 +5,6 @@ import org.apache.shiro.cache.CacheManager;
import org.slf4j.Logger; import org.slf4j.Logger;
import org.slf4j.LoggerFactory; import org.slf4j.LoggerFactory;
import java.util.Iterator;
import java.util.Set; import java.util.Set;
/** /**
@ -13,15 +12,15 @@ import java.util.Set;
*/ */
public class CacheUtils { public class CacheUtils {
private static Logger logger = LoggerFactory.getLogger(CacheUtils.class); private static final Logger LOGGER = LoggerFactory.getLogger(CacheUtils.class);
private static CacheManager cacheManager = SpringContextHolder.getBean(CacheManager.class); private static final CacheManager CACHE_MANAGER = SpringContextHolder.getBean(CacheManager.class);
private static final String SYS_CACHE = "system"; private static final String SYS_CACHE = "system";
/** /**
* 获取SYS_CACHE缓存 * 获取SYS_CACHE缓存
* @param key * @param key 键值
* @return * @return 缓存对象
*/ */
public static Object get(String key) { public static Object get(String key) {
return get(SYS_CACHE, key); return get(SYS_CACHE, key);
@ -29,9 +28,9 @@ public class CacheUtils {
/** /**
* 获取SYS_CACHE缓存 * 获取SYS_CACHE缓存
* @param key * @param key 键值
* @param defaultValue * @param defaultValue 默认返回值
* @return * @return 缓存对象或默认值
*/ */
public static Object get(String key, Object defaultValue) { public static Object get(String key, Object defaultValue) {
Object value = get(key); Object value = get(key);
@ -40,8 +39,8 @@ public class CacheUtils {
/** /**
* 写入SYS_CACHE缓存 * 写入SYS_CACHE缓存
* @param key * @param key 键值
* @return * @param value 被缓存对象
*/ */
public static void put(String key, Object value) { public static void put(String key, Object value) {
put(SYS_CACHE, key, value); put(SYS_CACHE, key, value);
@ -49,29 +48,28 @@ public class CacheUtils {
/** /**
* 从SYS_CACHE缓存中移除 * 从SYS_CACHE缓存中移除
* @param key * @param key 键值
* @return
*/ */
public static void remove(String key) { public static void remove(String key) {
remove(SYS_CACHE, key); remove(SYS_CACHE, key);
} }
/** /**
* 获取缓存 * 获取指定命名空间下的缓存
* @param cacheName * @param cacheName 缓存命名空间
* @param key * @param key 键值
* @return * @return 该命名空间下的对应键值缓存
*/ */
public static Object get(String cacheName, String key) { public static Object get(String cacheName, String key) {
return getCache(cacheName).get(getKey(key)); return getCache(cacheName).get(getKey(key));
} }
/** /**
* 获取缓存 * 获取指定命名空间下的缓存并在无缓存对象时返回默认值
* @param cacheName * @param cacheName 缓存命名空间
* @param key * @param key 键值
* @param defaultValue * @param defaultValue 默认返回值
* @return * @return 缓存对象或默认值
*/ */
public static Object get(String cacheName, String key, Object defaultValue) { public static Object get(String cacheName, String key, Object defaultValue) {
Object value = get(cacheName, getKey(key)); Object value = get(cacheName, getKey(key));
@ -79,41 +77,39 @@ public class CacheUtils {
} }
/** /**
* 写入缓存 * 向指定命名空间下存入被缓存对象
* @param cacheName * @param cacheName 缓存命名空间
* @param key * @param key 键值
* @param value * @param value 该命名空间下被缓存对象
*/ */
public static void put(String cacheName, String key, Object value) { public static void put(String cacheName, String key, Object value) {
getCache(cacheName).put(getKey(key), value); getCache(cacheName).put(getKey(key), value);
} }
/** /**
* 从缓存中移除 * 将指定命名空间下的缓存移除
* @param cacheName * @param cacheName 缓存命名空间
* @param key * @param key 键值
*/ */
public static void remove(String cacheName, String key) { public static void remove(String cacheName, String key) {
getCache(cacheName).remove(getKey(key)); getCache(cacheName).remove(getKey(key));
} }
/** /**
* 从缓存中移除所有 * 清空命名空间下所有缓存
* @param cacheName * @param cacheName 命名空间
*/ */
public static void removeAll(String cacheName) { public static void removeAll(String cacheName) {
Cache<String, Object> cache = getCache(cacheName); Cache<String, Object> cache = getCache(cacheName);
Set<String> keys = cache.keys(); Set<String> keys = cache.keys();
for (Iterator<String> it = keys.iterator(); it.hasNext();){ cache.clear();
cache.remove(it.next()); LOGGER.info("清理缓存: {} => {}", cacheName, keys);
}
logger.info("清理缓存: {} => {}", cacheName, keys);
} }
/** /**
* 获取缓存键名多数据源下增加数据源名称前缀 * 获取缓存键名多数据源下增加数据源名称前缀
* @param key * @param key 键值
* @return * @return 返回对应数据源前缀拼接键值
*/ */
private static String getKey(String key){ private static String getKey(String key){
// String dsName = DataSourceHolder.getDataSourceName(); // String dsName = DataSourceHolder.getDataSourceName();
@ -124,12 +120,12 @@ public class CacheUtils {
} }
/** /**
* 获得一个Cache没有则显示日志 * 获得指定命名空间的Cache没有则显示日志
* @param cacheName * @param cacheName 命名空间
* @return * @return 命名空间对应的Cache
*/ */
private static Cache<String, Object> getCache(String cacheName){ private static Cache<String, Object> getCache(String cacheName){
Cache<String, Object> cache = cacheManager.getCache(cacheName); Cache<String, Object> cache = CACHE_MANAGER.getCache(cacheName);
if (cache == null){ if (cache == null){
throw new RuntimeException("当前系统中没有定义“"+cacheName+"”这个缓存。"); throw new RuntimeException("当前系统中没有定义“"+cacheName+"”这个缓存。");
} }

View File

@ -8,6 +8,7 @@ import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession; import javax.servlet.http.HttpSession;
import java.util.HashMap; import java.util.HashMap;
import java.util.Map; import java.util.Map;
import java.util.Objects;
/** /**
* @ClassName: ContextHolderUtils * @ClassName: ContextHolderUtils
@ -17,24 +18,22 @@ import java.util.Map;
* *
*/ */
public class ContextHolderUtils { public class ContextHolderUtils {
private static final Map<String, HttpSession> sessionMap = new HashMap<String, HttpSession>(); private static final Map<String, HttpSession> sessionMap = new HashMap<>();
/** /**
* SpringMvc下获取request * SpringMvc下获取request
* *
* @return * @return HttpServletRequest
*/ */
public static HttpServletRequest getRequest() { public static HttpServletRequest getRequest() {
return ((ServletRequestAttributes) Objects.requireNonNull(RequestContextHolder.getRequestAttributes())).getRequest();
HttpServletRequest request = ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getRequest();
return request;
} }
/** /**
* SpringMvc下获取session * SpringMvc下获取session
* *
* @return * @return HttpSession
*/ */
public static HttpSession getSession() { public static HttpSession getSession() {
HttpServletRequest request = getRequest(); HttpServletRequest request = getRequest();
@ -54,7 +53,7 @@ public class ContextHolderUtils {
} }
public static HttpSession getSession2() { public static HttpSession getSession2() {
HttpServletRequest request = ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getRequest(); HttpServletRequest request = ((ServletRequestAttributes) Objects.requireNonNull(RequestContextHolder.getRequestAttributes())).getRequest();
return request.getSession(); return request.getSession();
} }
@ -65,9 +64,7 @@ public class ContextHolderUtils {
} }
public static void removeSession(String sessionId){ public static void removeSession(String sessionId){
if(sessionMap.containsKey(sessionId)){ sessionMap.remove(sessionId);
sessionMap.remove(sessionId);
}
} }
} }

View File

@ -13,22 +13,23 @@ public class DateUtil {
if (StringUtils.isBlank(s)) { if (StringUtils.isBlank(s)) {
s = ""; s = "";
} }
for (int i = 0; i < len - s.length(); ++i) { StringBuilder sBuilder = new StringBuilder(s);
s = "0" + 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) { 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) { 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) { 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() { public static String getNowDateNum() {

View File

@ -20,7 +20,7 @@ public class Digests {
private static final String SHA1 = "SHA-1"; private static final String SHA1 = "SHA-1";
private static final String MD5 = "MD5"; private static final String MD5 = "MD5";
private static SecureRandom random = new SecureRandom(); private static final SecureRandom SECURE_RANDOM = new SecureRandom();
/** /**
* 对输入字符串进行md5散列. * 对输入字符串进行md5散列.
@ -79,7 +79,7 @@ public class Digests {
Validate.isTrue(numBytes > 0, "numBytes argument must be a positive integer (1 or larger)", numBytes); Validate.isTrue(numBytes > 0, "numBytes argument must be a positive integer (1 or larger)", numBytes);
byte[] bytes = new byte[numBytes]; byte[] bytes = new byte[numBytes];
random.nextBytes(bytes); SECURE_RANDOM.nextBytes(bytes);
return bytes; return bytes;
} }

View File

@ -17,9 +17,9 @@ public enum ErrorCode {
this.message = message; this.message = message;
} }
private String code; private final String code;
private String message; private final String message;
public String getCode() { public String getCode() {
return code; return code;