common-module/utils/CommonUtils.java
cuijiawang ea58e018b4 init
2025-05-27 17:58:49 +08:00

200 lines
5.3 KiB
Java

package com.sgcc.utils;
import com.alibaba.fastjson.JSON;
import java.io.IOException;
import java.io.InputStream;
import java.math.RoundingMode;
import java.security.SecureRandom;
import java.text.DecimalFormat;
import java.util.Arrays;
import java.util.List;
import java.util.Random;
import java.util.stream.Collectors;
import com.sgcc.constant.Constants;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.apache.commons.io.IOUtils;
/**
* 共通方法工具类
*
* @author zhaoyuanyuan
* @version 1.0.0
* @date 2024/06/24 17:18
*/
@Slf4j
public class CommonUtils {
public static final String JSON_PATH = "classpath:json/";
/**
* ip和掩码分隔符
*/
public static final String IP_MASK_CODE_SEPARATOR = "\\/";
/**
* 解析JSON值
*
* @param fileName 文件名
* @param clazz 实体类
* @param <T> 泛型
* @return
*/
public static <T> T parseJson(String fileName, Class<T> clazz) throws IOException {
return JSON.parseObject(readJsonFile(fileName), clazz);
}
/**
* 读取json格式文件
*
* @param fileName 文件名
* @return
*/
public static String readJsonFile(String fileName) throws IOException {
return read(JSON_PATH + fileName);
}
/**
* 读取json格式文件
*
* @param path 文件路径
* @return
*/
public static String read(String path) throws IOException {
String content = "";
InputStream stream = null;
try {
stream = CommonUtils.class.getClassLoader()
.getResourceAsStream(path.replace("classpath:", ""));
content = IOUtils.toString(stream, "UTF-8");
} catch (IOException e) {
log.error("read file[{}] catch IOException. {}", path, e.getMessage());
} finally {
stream.close();
}
return content;
}
/**
* 布尔值判断 true-0 false-1
*
* @param param
* @return {@code Integer }
* @author zhaoyuanyuan
* @date 2024/06/24 17:21
*/
public static Integer booleanValueJudge(Boolean param) {
if (param == null) {
return null;
}
if (param) {
return Constants.IS_TRUE;
} else {
return Constants.IS_FALSE;
}
}
/**
* 获取IP地址
*
* @param ipAndMaskOffCode ip和屏蔽代码
* @return {@code String}
*/
public static String getIp(String ipAndMaskOffCode) {
String ip = "";
if(StringUtils.isEmpty(ipAndMaskOffCode)){
return ip;
}
String[] ipAndMaskOffCodes = ipAndMaskOffCode.split(IP_MASK_CODE_SEPARATOR);
if (ipAndMaskOffCodes.length > 1) {
ip = ipAndMaskOffCodes[0];
}else {
ip =ipAndMaskOffCode;
}
return ip;
}
/**
* 获取掩码
*
* @param ipAndMaskOffCode ip和屏蔽代码
* @return {@code String}
*/
public static String getMaskOffCode(String ipAndMaskOffCode) {
String maskOffCode = "";
if (StringUtils.isEmpty(ipAndMaskOffCode)){
return maskOffCode;
}
String[] ipAndMaskOffCodes = ipAndMaskOffCode.split(IP_MASK_CODE_SEPARATOR);
if (ipAndMaskOffCodes.length >= 2) {
maskOffCode = ipAndMaskOffCodes[1];
}
return maskOffCode;
}
/**
* 字符串形式的数组转换为List
*
* @param integerArrStr 数组字符串
* @return {@code List<Integer> }
* @author zhaoyuanyuan
* @date 2024/07/11 09:43
*/
public static List<Integer> string2IntegerList(String integerArrStr) {
List<String> tempList = Arrays.asList(integerArrStr.split(","));
List<Integer> resultList = tempList.stream().map(Integer::valueOf).collect(Collectors.toList());
return resultList;
}
/**
* 随机获取列表中的某一个元素
*
* @param list 列表
* @return {@code T }
* @author zhaoyuanyuan
* @date 2024/07/18 16:16
*/
public static <T> T getRandomElement(List<T> list) {
if (list == null || list.isEmpty()) {
throw new IllegalArgumentException("List cannot be null or empty");
}
SecureRandom random = new SecureRandom();
int index = random.nextInt(list.size());
return list.get(index);
}
/**
* Double类型数据保留两位小数
*
* @param value 值
* @return {@code Double }
* @author zhaoyuanyuan
* @date 2024/09/18 14:01
*/
public static Double formatDouble(Double value) {
//格式化字符串,表示两位小数
DecimalFormat df = new DecimalFormat("#.00");
//设置格式化器
df.setRoundingMode(RoundingMode.HALF_UP);
//转换并输出
String formattedValue = df.format(value);
return Double.valueOf(formattedValue);
}
/**
* 身份证号脱敏处理
*
* @param idCard 身份证号
* @return {@code String }
* @author zhaoyuanyuan
* @date 2024/11/21 13:14
*/
public static String desensitizedIdCard(String idCard) {
if (idCard == null || (idCard.length() != 18 && idCard.length() != 15)) {
return null;
}
return idCard.replaceAll("^(\\d{6})(\\d+)(\\d{4})$", "$1****$3");
}
}