Browse Source

重构getFileNameIndex

release/11.0
Destiny.Lin 2 years ago
parent
commit
7a07ff6fbd
  1. 47
      designer-base/src/main/java/com/fr/design/mainframe/JTemplateNameHelper.java
  2. 23
      designer-base/src/test/java/com/fr/design/mainframe/JTemplateNameHelperTest.java

47
designer-base/src/main/java/com/fr/design/mainframe/JTemplateNameHelper.java

@ -39,7 +39,6 @@ public class JTemplateNameHelper {
}
Collections.sort(reportNum);
BigInteger idx = reportNum.size() > 0 ? reportNum.get(reportNum.size() - 1).add(BigInteger.valueOf(1)) : BigInteger.valueOf(1);
idx = idx.add(BigInteger.valueOf(currentIndex));
currentIndex++;
return prefix + idx;
@ -54,35 +53,39 @@ public class JTemplateNameHelper {
* @Date 2021/4/9 11:13
**/
private static BigInteger getFileNameIndex(String prefix, String fileName) {
if (fileName.length() <= prefix.length()) {
//如果文件名长度小于等于前缀长度或者匹配前缀失败,直接返回就可以了
if ((prefix.length() >= fileName.length()) || (!StringUtils.equals(prefix, fileName.substring(0, prefix.length())))) {
return null;
}
char[] chars = new char[fileName.length()];
int i = 0;
for (; i < fileName.length(); i++) {
BigInteger integer = null;
integer = matchFileNameIndex(prefix, fileName);
return integer;
}
private static BigInteger matchFileNameIndex(String prefix, String fileName) {
StringBuilder result = new StringBuilder();
for (int i = prefix.length(); i < fileName.length(); i++) {
char c = fileName.charAt(i);
//匹配前缀
if (i < prefix.length()) {
if (c != prefix.charAt(i)) {
return null;
}
if (isDot(c)) {
break;
} else {
if (c == '.') {
break;
} else {
//匹配0~9
if (c < 48 || c > 57) {
return null;
}
chars[i - prefix.length()] = c;
if (isNotNumber(c)) {
return null;
}
result.append(c);
}
}
String s = new String(chars).substring(0, i - prefix.length());
BigInteger integer = new BigInteger(s, 10);
if (StringUtils.isBlank(s)) {
if (StringUtils.isBlank(result.toString())) {
return null;
}
return integer;
return new BigInteger(result.toString(), 10);
}
private static boolean isNotNumber(char c) {
return c < 48 || c > 57;
}
private static boolean isDot(char c) {
return c == '.';
}
}

23
designer-base/src/test/java/com/fr/design/mainframe/JTemplateNameHelperTest.java

@ -2,6 +2,8 @@ package com.fr.design.mainframe;
import junit.framework.TestCase;
import com.fr.invoke.Reflect;
/**
* @author shine
* @version 10.0
@ -18,6 +20,27 @@ public class JTemplateNameHelperTest extends TestCase {
String name1 = JTemplateNameHelper.newTemplateNameByIndex("TEST");
assertEquals("TEST2", name1);
}
public void testGetFileNameIndex() {
//正常情况
assertEquals("1", Reflect.on(JTemplateNameHelper.class).call("getFileNameIndex", "WorkBook", "WorkBook1").toString());
//正常情况
assertEquals("8888888888", Reflect.on(JTemplateNameHelper.class).call("getFileNameIndex", "WorkBook", "WorkBook8888888888").toString());
//正常情况
assertEquals("88812214128888881231238123123", Reflect.on(JTemplateNameHelper.class).call("getFileNameIndex", "WorkBook", "WorkBook88812214128888881231238123123").toString());
//前缀不匹配
assertNull(Reflect.on(JTemplateNameHelper.class).call("getFileNameIndex", "Work123", "WorkBook8888888888").get());
//前缀为空
assertNull(Reflect.on(JTemplateNameHelper.class).call("getFileNameIndex", "", "WorkBook8888888888").get());
//文件长度小于前缀
assertNull(Reflect.on(JTemplateNameHelper.class).call("getFileNameIndex", "WorkBook", "").get());
}
}

Loading…
Cancel
Save