refactor: optimize Row.getIgnoreCase

This commit is contained in:
Michael Yang 2024-04-17 16:44:18 +08:00
parent 3ff68d027d
commit aeb8ae9ad4
2 changed files with 21 additions and 5 deletions

View File

@ -273,12 +273,14 @@ public class Row extends LinkedHashMap<String, Object> implements UpdateWrapper<
}
public Object getIgnoreCase(String key) {
String camelKey = null;
if (key.contains("_")) {
camelKey = StringUtil.deleteChar(key, '_');
Object result = super.get(key);
if (result != null) {
return result;
}
String newKey = StringUtil.deleteChar(key, '_', '-');
for (String innerKey : keySet()) {
if (innerKey.equalsIgnoreCase(key) || (camelKey != null && camelKey.equalsIgnoreCase(innerKey))) {
if (newKey.equalsIgnoreCase(StringUtil.deleteChar(innerKey, '_', '-'))) {
return super.get(innerKey);
}
}
@ -320,7 +322,7 @@ public class Row extends LinkedHashMap<String, Object> implements UpdateWrapper<
return defaultValue;
}
String r = s.toString();
return r.trim().length() == 0 ? defaultValue : r;
return r.trim().isEmpty() ? defaultValue : r;
}
public Integer getInt(String key) {

View File

@ -144,6 +144,20 @@ public class StringUtil {
return sb.toString();
}
public static String deleteChar(String string, char deleteChar1, char deleteChar2) {
if (isBlank(string)) {
return "";
}
char[] chars = string.toCharArray();
StringBuilder sb = new StringBuilder(string.length());
for (char aChar : chars) {
if (aChar != deleteChar1 && aChar != deleteChar2) {
sb.append(aChar);
}
}
return sb.toString();
}
/**
* 字符串为 null 或者内部字符全部为 ' ', '\t', '\n', '\r' 这四类字符时返回 true
*/