Browse Source

重构getFileNameIndex

newui
Destiny.Lin 3 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); Collections.sort(reportNum);
BigInteger idx = reportNum.size() > 0 ? reportNum.get(reportNum.size() - 1).add(BigInteger.valueOf(1)) : BigInteger.valueOf(1); BigInteger idx = reportNum.size() > 0 ? reportNum.get(reportNum.size() - 1).add(BigInteger.valueOf(1)) : BigInteger.valueOf(1);
idx = idx.add(BigInteger.valueOf(currentIndex)); idx = idx.add(BigInteger.valueOf(currentIndex));
currentIndex++; currentIndex++;
return prefix + idx; return prefix + idx;
@ -54,35 +53,39 @@ public class JTemplateNameHelper {
* @Date 2021/4/9 11:13 * @Date 2021/4/9 11:13
**/ **/
private static BigInteger getFileNameIndex(String prefix, String fileName) { 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; return null;
} }
char[] chars = new char[fileName.length()]; BigInteger integer = null;
int i = 0; integer = matchFileNameIndex(prefix, fileName);
for (; i < fileName.length(); i++) { 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); char c = fileName.charAt(i);
//匹配前缀 if (isDot(c)) {
if (i < prefix.length()) { break;
if (c != prefix.charAt(i)) {
return null;
}
} else { } else {
if (c == '.') { if (isNotNumber(c)) {
break; return null;
} else {
//匹配0~9
if (c < 48 || c > 57) {
return null;
}
chars[i - prefix.length()] = c;
} }
result.append(c);
} }
} }
String s = new String(chars).substring(0, i - prefix.length()); if (StringUtils.isBlank(result.toString())) {
BigInteger integer = new BigInteger(s, 10);
if (StringUtils.isBlank(s)) {
return null; 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 junit.framework.TestCase;
import com.fr.invoke.Reflect;
/** /**
* @author shine * @author shine
* @version 10.0 * @version 10.0
@ -18,6 +20,27 @@ public class JTemplateNameHelperTest extends TestCase {
String name1 = JTemplateNameHelper.newTemplateNameByIndex("TEST"); String name1 = JTemplateNameHelper.newTemplateNameByIndex("TEST");
assertEquals("TEST2", name1); 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