Merge pull request #37 from Yuao-github/main

perf[storage] 优化了字符串处理函数
This commit is contained in:
godotg
2022-12-12 19:17:12 +08:00
committed by GitHub
2 changed files with 17 additions and 44 deletions
@@ -485,38 +485,5 @@ public abstract class StringUtils {
return Character.isWhitespace(ch) || STOP_WORD.contains(ch);
}
/**
*
* @param str 待匹配字符串
* @param matchStr 匹配的后缀
* @return 是否成功匹配后缀
*/
public static boolean suffixMatch(final String str,final String matchStr){
if(matchStr.length()>str.length())
return false;
for(int i=0;i<matchStr.length();i++){
if(str.charAt(str.length()-1-i)!=matchStr.charAt(matchStr.length()-1-i))
return false;
}
return true;
}
/**
*把String数组展开成一个大的字符串
* @param strings
* @return
*/
public static String stringArrayToString(final String[] strings){
StringBuilder stringBuilder=new StringBuilder();
stringBuilder.append('[');
for(var i=0;i<strings.length;i++){
if(i!=0){
stringBuilder.append(',');
}
stringBuilder.append(StringUtils.format("[{}]",i+1));
stringBuilder.append(strings[i]);
}
stringBuilder.append(']');
return stringBuilder.toString();
}
}
@@ -260,11 +260,14 @@ public class StorageManager implements IStorageManager {
packageSearchPath = packageSearchPath.replaceAll("//", "/");
try {
for(var resource :resourcePatternResolver.getResources(packageSearchPath)){
if(StringUtils.suffixMatch(resource.getFilename(), ResourceEnum.CSV.getType())
||StringUtils.suffixMatch(resource.getFilename(), ResourceEnum.EXCEL_XLS.getType())
||StringUtils.suffixMatch(resource.getFilename(), ResourceEnum.EXCEL_XLSX.getType())
||StringUtils.suffixMatch(resource.getFilename(), ResourceEnum.JSON.getType()))
var resourceFilename=resource.getFilename();
if(resourceFilename.endsWith(ResourceEnum.CSV.getType())
|| resourceFilename.endsWith(ResourceEnum.EXCEL_XLS.getType())
|| resourceFilename.endsWith(ResourceEnum.EXCEL_XLSX.getType())
|| resourceFilename.endsWith(ResourceEnum.JSON.getType())){
resourceDefSet.add(new ResourceDef(clazz,resource));
}
}
} catch (Exception e) {
// do nothing
@@ -275,11 +278,13 @@ public class StorageManager implements IStorageManager {
packageSearchPath = StringUtils.format("{}/{}.*", resourceLocation, clazz.getSimpleName());
packageSearchPath = packageSearchPath.replaceAll("//", "/");
for(var resource :resourcePatternResolver.getResources(packageSearchPath)){
if(StringUtils.suffixMatch(resource.getFilename(), ResourceEnum.CSV.getType())
||StringUtils.suffixMatch(resource.getFilename(), ResourceEnum.EXCEL_XLS.getType())
||StringUtils.suffixMatch(resource.getFilename(), ResourceEnum.EXCEL_XLSX.getType())
||StringUtils.suffixMatch(resource.getFilename(), ResourceEnum.JSON.getType()))
var resourceFilename=resource.getFilename();
if(resourceFilename.endsWith(ResourceEnum.CSV.getType())
|| resourceFilename.endsWith(ResourceEnum.EXCEL_XLS.getType())
|| resourceFilename.endsWith(ResourceEnum.EXCEL_XLSX.getType())
|| resourceFilename.endsWith(ResourceEnum.JSON.getType())){
resourceDefSet.add(new ResourceDef(clazz,resource));
}
}
}
}
@@ -290,10 +295,11 @@ public class StorageManager implements IStorageManager {
var resourceNames=new String[resourceDefSet.size()];
var index=0;
for(var resourceDef:resourceDefSet){
resourceNames[index++]=resourceDef.getResource().getFile().getAbsolutePath();
resourceNames[index++]=StringUtils.format("[{}]{}",index,resourceDef.getResource().getFile().getAbsolutePath());
}
throw new RuntimeException(StringUtils.format("资源类[class:{}]找到重复的配置文件{}", clazz.getSimpleName(),StringUtils.stringArrayToString(resourceNames)));
StringJoiner stringJoiner=new StringJoiner(",","[","]");
Arrays.stream(resourceNames).forEach(stringJoiner::add);
throw new RuntimeException(StringUtils.format("资源类[class:{}]找到重复的配置文件{}", clazz.getSimpleName(),stringJoiner.toString()));
} else {
return ((ResourceDef)(resourceDefSet.toArray()[0])).getResource();
}