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 {
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<Runnable>());
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<Runnable>());
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<Runnable>());
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").

View File

@ -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<Class<?>,Map<Class<?>, BeanCopier>> beanCopierMap = new ConcurrentHashMap<>();
private static final Map<String, BeanCopier> BEAN_COPIER_MAP = new ConcurrentHashMap<>();
/**
* 拷贝方法
* @param sourceBean
* @param targetBean
* @param <S>
* @param <T>
* @param sourceBean 源对象
* @param targetBean 目标对象
* @param <S> 源对象类型
* @param <T> 目标对象类型
*/
public static <S,T> void copy(S sourceBean,T targetBean){
@SuppressWarnings("unchecked")
@ -37,16 +36,15 @@ public class BeanCopierUtil {
/**
* 转换方法
* @param sourceBean 对象
* @param targetClass 目标类
* @param <S>
* @param <T>
* @return
* @param sourceBean 对象
* @param targetBean 目标对象
* @param <S> 源对象类型
* @param <T> 目标对象类型
* @return 拷贝值后的targetBean
*/
public static <S,T> T convert(S sourceBean,Class<T> targetClass){
public static <S,T> 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 <S,T> BeanCopier getBeanCopier(Class<S> sourceClass, Class<T> targetClass ){
Map<Class<?>, BeanCopier> map = beanCopierMap.get(sourceClass);
if(CollectionUtils.isEmpty(map)){
BeanCopier newBeanCopier = BeanCopier.create(sourceClass, targetClass, false);
Map<Class<?>, 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 <S> 源类型
* @param <T> 目标类型
* @return BeanCopier实例
*/
private static <S,T> BeanCopier getBeanCopier(Class<S> sourceClass, Class<T> targetClass){
String classKey = sourceClass.getTypeName() + targetClass.getTypeName();
return BEAN_COPIER_MAP.computeIfAbsent(classKey, key -> BeanCopier.create(sourceClass, targetClass, false));
}
}

View File

@ -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<String, Object> cache = getCache(cacheName);
Set<String> keys = cache.keys();
for (Iterator<String> 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<String, Object> getCache(String cacheName){
Cache<String, Object> cache = cacheManager.getCache(cacheName);
Cache<String, Object> cache = CACHE_MANAGER.getCache(cacheName);
if (cache == null){
throw new RuntimeException("当前系统中没有定义“"+cacheName+"”这个缓存。");
}

View File

@ -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<String, HttpSession> sessionMap = new HashMap<String, HttpSession>();
private static final Map<String, HttpSession> 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);
}
}

View File

@ -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() {

View File

@ -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;
}

View File

@ -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;