Skip to content

Commit a1643e0

Browse files
authored
Merge pull request #1 from Nordstrom/pr/fix-next-path-bug
Fix bug in getNextPath
2 parents 81ec9e0 + c204fbb commit a1643e0

File tree

2 files changed

+18
-14
lines changed

2 files changed

+18
-14
lines changed

src/main/java/com/nordstrom/common/file/PathUtils.java

Lines changed: 7 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,6 @@
1010
import java.util.Comparator;
1111
import java.util.Objects;
1212
import java.util.Optional;
13-
import java.util.regex.Matcher;
14-
import java.util.regex.Pattern;
1513
import java.util.stream.Stream;
1614

1715
/**
@@ -173,25 +171,23 @@ public static Path getNextPath(Path targetPath, String baseName, String extensio
173171
PathMatcher pathMatcher = FileSystems.getDefault().getPathMatcher("regex:" + baseName + "(-\\d+)?\\." + extension);
174172

175173
try (Stream<Path> stream = Files.walk(targetPath, 1)) {
174+
int base = baseName.length();
176175
int ext = extension.length() + 1;
177176

178-
Optional<String> optional =
177+
Optional<Integer> optional =
179178
stream
180179
.map(Path::getFileName)
181180
.filter(pathMatcher::matches)
182181
.map(String::valueOf)
183-
.map(s -> s.substring(0, s.length() - ext))
182+
.map(s -> "0" + s.substring(base, s.length() - ext))
183+
.map(s -> s.replace("0-", ""))
184+
.map(Integer::valueOf)
185+
.map(i -> i + 1)
184186
.sorted(Comparator.reverseOrder())
185187
.findFirst();
186188

187189
if (optional.isPresent()) {
188-
int index = 1;
189-
Pattern pattern = Pattern.compile(baseName + "-(\\d+)");
190-
Matcher matcher = pattern.matcher(optional.get());
191-
if (matcher.matches()) {
192-
index = Integer.parseInt(matcher.group(1));
193-
}
194-
newName = baseName + "-" + Integer.toString(index + 1) + "." + extension;
190+
newName = baseName + "-" + optional.get() + "." + extension;
195191
} else {
196192
newName = baseName + "." + extension;
197193
}

src/test/java/com/nordstrom/common/file/PathUtilsTest.java

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -36,9 +36,17 @@ public void testNextPath() throws IOException {
3636
path1.toFile().createNewFile();
3737

3838
Path path2 = PathUtils.getNextPath(targetPath, "testNextPath", "txt");
39-
assertEquals(path2.getFileName().toString(), "testNextPath-2.txt");
40-
Path path3 = PathUtils.getNextPath(targetPath, "test", "txt");
41-
assertEquals(path3.getFileName().toString(), "test.txt");
39+
assertEquals(path2.getFileName().toString(), "testNextPath-1.txt");
40+
41+
path2.toFile().createNewFile();
42+
targetPath.resolve("testNextPath-9.txt").toFile().createNewFile();
43+
targetPath.resolve("testNextPath-10.txt").toFile().createNewFile();
44+
45+
Path path3 = PathUtils.getNextPath(targetPath, "testNextPath", "txt");
46+
assertEquals(path3.getFileName().toString(), "testNextPath-11.txt");
47+
48+
Path path4 = PathUtils.getNextPath(targetPath, "test", "txt");
49+
assertEquals(path4.getFileName().toString(), "test.txt");
4250
}
4351

4452
private Path getOutputPath() {

0 commit comments

Comments
 (0)