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