refactor: optimize Table.java and StringUtil.java

This commit is contained in:
Michael Yang 2024-03-04 18:07:07 +08:00
parent 1d0192e665
commit 9356905a73
2 changed files with 31 additions and 36 deletions

View File

@ -131,7 +131,7 @@ public class Table {
public boolean containsColumn(String columnName) { public boolean containsColumn(String columnName) {
if (columns == null || columns.isEmpty()) { if (columns == null || columns.isEmpty() || StringUtil.isBlank(columnName)) {
return false; return false;
} }
for (Column column : columns) { for (Column column : columns) {

View File

@ -34,9 +34,9 @@ public class StringUtil {
public static String firstCharToLowerCase(String string) { public static String firstCharToLowerCase(String string) {
char firstChar = string.charAt(0); char firstChar = string.charAt(0);
if (firstChar >= 'A' && firstChar <= 'Z') { if (firstChar >= 'A' && firstChar <= 'Z') {
char[] arr = string.toCharArray(); char[] chars = string.toCharArray();
arr[0] += ('a' - 'A'); chars[0] += ('a' - 'A');
return new String(arr); return new String(chars);
} }
return string; return string;
} }
@ -50,9 +50,9 @@ public class StringUtil {
public static String firstCharToUpperCase(String string) { public static String firstCharToUpperCase(String string) {
char firstChar = string.charAt(0); char firstChar = string.charAt(0);
if (firstChar >= 'a' && firstChar <= 'z') { if (firstChar >= 'a' && firstChar <= 'z') {
char[] arr = string.toCharArray(); char[] chars = string.toCharArray();
arr[0] -= ('a' - 'A'); chars[0] -= ('a' - 'A');
return new String(arr); return new String(chars);
} }
return string; return string;
} }
@ -88,7 +88,7 @@ public class StringUtil {
if (isBlank(string)) { if (isBlank(string)) {
return ""; return "";
} }
if(Character.isUpperCase(string.charAt(0))){ if (Character.isUpperCase(string.charAt(0))) {
string = string.toLowerCase(); string = string.toLowerCase();
} }
int strLen = string.length(); int strLen = string.length();
@ -110,15 +110,15 @@ public class StringUtil {
/** /**
* 删除字符串中的字符 * 删除字符串中的字符
*/ */
public static String deleteChar(String string,char deleteChar) { public static String deleteChar(String string, char deleteChar) {
if (isBlank(string)) { if (isBlank(string)) {
return ""; return "";
} }
char[] chars = string.toCharArray(); char[] chars = string.toCharArray();
StringBuilder sb = new StringBuilder(string.length()); StringBuilder sb = new StringBuilder(string.length());
for (char aChar : chars) { for (char aChar : chars) {
if (aChar != deleteChar){ if (aChar != deleteChar) {
sb.append(aChar); sb.append(aChar);
} }
} }
return sb.toString(); return sb.toString();
@ -127,13 +127,13 @@ public class StringUtil {
/** /**
* 字符串为 null 或者内部字符全部为 ' ', '\t', '\n', '\r' 这四类字符时返回 true * 字符串为 null 或者内部字符全部为 ' ', '\t', '\n', '\r' 这四类字符时返回 true
*/ */
public static boolean isBlank(String str) { public static boolean isBlank(String string) {
if (str == null) { if (string == null) {
return true; return true;
} }
for (int i = 0, len = str.length(); i < len; i++) { for (int i = 0, len = string.length(); i < len; i++) {
if (str.charAt(i) > ' ') { if (string.charAt(i) > ' ') {
return false; return false;
} }
} }
@ -143,11 +143,11 @@ public class StringUtil {
public static boolean isAnyBlank(String... strings) { public static boolean isAnyBlank(String... strings) {
if (strings == null || strings.length == 0) { if (strings == null || strings.length == 0) {
throw new IllegalArgumentException("args is empty."); throw new IllegalArgumentException("strings is null or empty.");
} }
for (String str : strings) { for (String string : strings) {
if (isBlank(str)) { if (isBlank(string)) {
return true; return true;
} }
} }
@ -168,15 +168,15 @@ public class StringUtil {
/** /**
* 这个字符串是否是全是数字 * 这个字符串是否是全是数字
* *
* @param str * @param string
* @return * @return 全部数数值时返回 true否则返回 false
*/ */
public static boolean isNumeric(String str) { public static boolean isNumeric(String string) {
if (isBlank(str)) { if (isBlank(string)) {
return false; return false;
} }
for (int i = str.length(); --i >= 0; ) { for (int i = string.length(); --i >= 0; ) {
int chr = str.charAt(i); int chr = string.charAt(i);
if (chr < 48 || chr > 57) { if (chr < 48 || chr > 57) {
return false; return false;
} }
@ -185,13 +185,13 @@ public class StringUtil {
} }
public static boolean startsWithAny(String str, String... prefixes) { public static boolean startsWithAny(String string, String... prefixes) {
if (isBlank(str) || prefixes == null || prefixes.length == 0) { if (isBlank(string) || prefixes == null) {
return false; return false;
} }
for (String prefix : prefixes) { for (String prefix : prefixes) {
if (str.startsWith(prefix)) { if (string.startsWith(prefix)) {
return true; return true;
} }
} }
@ -200,7 +200,7 @@ public class StringUtil {
public static boolean endsWithAny(String str, String... suffixes) { public static boolean endsWithAny(String str, String... suffixes) {
if (isBlank(str) || suffixes == null || suffixes.length == 0) { if (isBlank(str) || suffixes == null) {
return false; return false;
} }
@ -213,11 +213,6 @@ public class StringUtil {
} }
public static String trimOrNull(String string) {
return string != null ? string.trim() : null;
}
/** /**
* 正则匹配 * 正则匹配
* *
@ -313,14 +308,14 @@ public class StringUtil {
return string != null ? string.trim() : null; return string != null ? string.trim() : null;
} }
public static String substringAfterLast(String text, String str) { public static String substringAfterLast(String text, String prefix) {
if (text == null) { if (text == null) {
return null; return null;
} }
if (str == null) { if (prefix == null) {
return text; return text;
} }
return text.substring(text.lastIndexOf(str) + 1); return text.substring(text.lastIndexOf(prefix) + 1);
} }