【fix sonar】oConvertUtils文件

This commit is contained in:
梁琦涛
2023-03-03 17:58:49 +08:00
parent 220182e072
commit 84ed5657f7
@@ -15,18 +15,24 @@ import java.net.InetAddress;
import java.net.NetworkInterface;
import java.net.SocketException;
import java.net.UnknownHostException;
import java.security.NoSuchAlgorithmException;
import java.security.SecureRandom;
import java.sql.Date;
import java.util.*;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
/**
*
*
* @Author 张代浩
*
*/
@Slf4j
public class oConvertUtils {
private oConvertUtils(){
}
public static boolean isEmpty(Object object) {
if (object == null) {
return (true);
@@ -39,7 +45,7 @@ public class oConvertUtils {
}
return (false);
}
public static boolean isNotEmpty(Object object) {
if (object != null && !object.equals("") && !object.equals("null")) {
return (true);
@@ -52,8 +58,8 @@ public class oConvertUtils {
return temp;
}
public static String StrToUTF(String strIn, String sourceCode, String targetCode) {
strIn = "";
public static String StrToUTF() {
String strIn = "";
try {
strIn = new String(strIn.getBytes("ISO-8859-1"), "GBK");
} catch (UnsupportedEncodingException e) {
@@ -72,7 +78,7 @@ public class oConvertUtils {
try {
byte[] b = strIn.getBytes(sourceCode);
for (int i = 0; i < b.length; i++) {
System.out.print(b[i] + " ");
log.info(b[i] + " ");
}
strOut = new String(b, targetCode);
} catch (Exception e) {
@@ -83,7 +89,7 @@ public class oConvertUtils {
}
public static int getInt(String s, int defval) {
if (s == null || s == "") {
if (s == null || "".equals(s)) {
return (defval);
}
try {
@@ -94,7 +100,7 @@ public class oConvertUtils {
}
public static int getInt(String s) {
if (s == null || s == "") {
if (s == null || "".equals(s)) {
return 0;
}
try {
@@ -105,7 +111,7 @@ public class oConvertUtils {
}
public static int getInt(String s, Integer df) {
if (s == null || s == "") {
if (s == null || "".equals(s)) {
return df;
}
try {
@@ -117,9 +123,6 @@ public class oConvertUtils {
public static Integer[] getInts(String[] s) {
Integer[] integer = new Integer[s.length];
if (s == null) {
return null;
}
for (int i = 0; i < s.length; i++) {
integer[i] = Integer.parseInt(s[i]);
}
@@ -128,7 +131,7 @@ public class oConvertUtils {
}
public static double getDouble(String s, double defval) {
if (s == null || s == "") {
if (s == null || "".equals(s)) {
return (defval);
}
try {
@@ -163,7 +166,7 @@ public class oConvertUtils {
return (defval);
}
}
public static Integer getInt(Object object) {
if (isEmpty(object)) {
return null;
@@ -207,7 +210,7 @@ public class oConvertUtils {
/*public static String escapeJava(Object s) {
return StringEscapeUtils.escapeJava(getString(s));
}*/
public static String getString(Object object) {
if (isEmpty(object)) {
return "";
@@ -263,12 +266,12 @@ public class oConvertUtils {
/**
* 判断一个类是否为基本数据类型。
*
*
* @param clazz
* 要判断的类。
* @return true 表示为基本数据类型。
*/
private static boolean isBaseDataType(Class clazz) throws Exception {
private static boolean isBaseDataType(Class clazz) {
return (clazz.equals(String.class) || clazz.equals(Integer.class) || clazz.equals(Byte.class) || clazz.equals(Long.class) || clazz.equals(Double.class) || clazz.equals(Float.class) || clazz.equals(Character.class) || clazz.equals(Short.class) || clazz.equals(BigDecimal.class) || clazz.equals(BigInteger.class) || clazz.equals(Boolean.class) || clazz.equals(Date.class) || clazz.isPrimitive());
}
@@ -278,14 +281,15 @@ public class oConvertUtils {
* @return IP Address
*/
public static String getIpAddrByRequest(HttpServletRequest request) {
String unknown = "unknown";
String ip = request.getHeader("x-forwarded-for");
if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
if (ip == null || ip.length() == 0 || unknown.equalsIgnoreCase(ip)) {
ip = request.getHeader("Proxy-Client-IP");
}
if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
if (ip == null || ip.length() == 0 || unknown.equalsIgnoreCase(ip)) {
ip = request.getHeader("WL-Proxy-Client-IP");
}
if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
if (ip == null || ip.length() == 0 || unknown.equalsIgnoreCase(ip)) {
ip = request.getRemoteAddr();
}
return ip;
@@ -326,7 +330,7 @@ public class oConvertUtils {
/**
* java去除字符串中的空格、回车、换行符、制表符
*
*
* @param str
* @return
*/
@@ -343,7 +347,7 @@ public class oConvertUtils {
/**
* 判断元素是否在数组内
*
*
* @param substring
* @param source
* @return
@@ -370,7 +374,7 @@ public class oConvertUtils {
/**
* SET转换MAP
*
*
* @param str
* @return
*/
@@ -414,12 +418,12 @@ public class oConvertUtils {
private static boolean isInner(long userIp, long begin, long end) {
return (userIp >= begin) && (userIp <= end);
}
/**
* 将下划线大写方式命名的字符串转换为驼峰式。
* 如果转换前的下划线大写方式命名的字符串为空,则返回空字符串。</br>
* 例如:hello_world->helloWorld
*
*
* @param name
* 转换前的下划线大写方式命名的字符串
* @return 转换后的驼峰式命名的字符串
@@ -456,12 +460,12 @@ public class oConvertUtils {
}
return result.toString();
}
/**
* 将下划线大写方式命名的字符串转换为驼峰式。
* 如果转换前的下划线大写方式命名的字符串为空,则返回空字符串。</br>
* 例如:hello_world,test_id->helloWorld,testId
*
*
* @param name
* 转换前的下划线大写方式命名的字符串
* @return 转换后的驼峰式命名的字符串
@@ -470,7 +474,7 @@ public class oConvertUtils {
if(names==null||names.equals("")){
return null;
}
StringBuffer sf = new StringBuffer();
StringBuilder sf = new StringBuilder();
String[] fs = names.split(",");
for (String field : fs) {
field = camelName(field);
@@ -479,13 +483,13 @@ public class oConvertUtils {
String result = sf.toString();
return result.substring(0, result.length() - 1);
}
//update-begin--Author:zhoujf Date:20180503 forTASK #2500 【代码生成器】代码生成器开发一通用模板生成功能
/**
* 将下划线大写方式命名的字符串转换为驼峰式。(首字母写)
* 如果转换前的下划线大写方式命名的字符串为空,则返回空字符串。</br>
* 例如:hello_world->HelloWorld
*
*
* @param name
* 转换前的下划线大写方式命名的字符串
* @return 转换后的驼峰式命名的字符串
@@ -514,7 +518,7 @@ public class oConvertUtils {
return result.toString();
}
//update-end--Author:zhoujf Date:20180503 forTASK #2500 【代码生成器】代码生成器开发一通用模板生成功能
/**
* 将驼峰命名转化成下划线
* @param para
@@ -522,18 +526,18 @@ public class oConvertUtils {
*/
public static String camelToUnderline(String para){
if(para.length()<3){
return para.toLowerCase();
return para.toLowerCase();
}
StringBuilder sb=new StringBuilder(para);
int temp=0;//定位
//从第三个字符开始 避免命名不规范
//从第三个字符开始 避免命名不规范
for(int i=2;i<para.length();i++){
if(Character.isUpperCase(para.charAt(i))){
sb.insert(i+temp, "_");
temp+=1;
}
}
return sb.toString().toLowerCase();
return sb.toString().toLowerCase();
}
/**
@@ -542,17 +546,25 @@ public class oConvertUtils {
*/
public static String randomGen(int place) {
String base = "qwertyuioplkjhgfdsazxcvbnmQAZWSXEDCRFVTGBYHNUJMIKLOP0123456789";
StringBuffer sb = new StringBuffer();
Random rd = new Random();
StringBuilder sb = new StringBuilder();
Random rd = null;
try {
rd = SecureRandom.getInstanceStrong();
} catch (NoSuchAlgorithmException e) {
e.printStackTrace();
}
if(Objects.isNull(rd)){
return null;
}
for(int i=0;i<place;i++) {
sb.append(base.charAt(rd.nextInt(base.length())));
}
return sb.toString();
}
/**
* 获取类的所有属性,包括父类
*
*
* @param object
* @return
*/
@@ -567,7 +579,7 @@ public class oConvertUtils {
fieldList.toArray(fields);
return fields;
}
/**
* 将map的key全部转成小写
* @param list
@@ -577,10 +589,10 @@ public class oConvertUtils {
List<Map<String, Object>> select = new ArrayList<>();
for (Map<String, Object> row : list) {
Map<String, Object> resultMap = new HashMap<>();
Set<String> keySet = row.keySet();
for (String key : keySet) {
String newKey = key.toLowerCase();
resultMap.put(newKey, row.get(key));
Set<String> keySet = row.keySet();
for (String key : keySet) {
String newKey = key.toLowerCase();
resultMap.put(newKey, row.get(key));
}
select.add(resultMap);
}