-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
c0027e6
commit f79f118
Showing
6 changed files
with
106 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,74 @@ | ||
package info.loenwind.compare.tools; | ||
|
||
import java.io.IOException; | ||
import java.nio.file.FileSystems; | ||
import java.nio.file.FileVisitResult; | ||
import java.nio.file.Files; | ||
import java.nio.file.Path; | ||
import java.nio.file.PathMatcher; | ||
import java.nio.file.SimpleFileVisitor; | ||
import java.nio.file.attribute.BasicFileAttributes; | ||
import java.util.ArrayList; | ||
import java.util.List; | ||
|
||
import static java.nio.file.FileVisitResult.CONTINUE; | ||
|
||
public class FileFinder extends SimpleFileVisitor<Path> { | ||
|
||
private final PathMatcher matcher; | ||
private final String ignore; | ||
private final List<Path> matches = new ArrayList<>(); | ||
|
||
@SuppressWarnings("resource") | ||
public FileFinder(Path startingDir, String pattern) { | ||
matcher = FileSystems.getDefault().getPathMatcher("glob:" + pattern); | ||
ignore = ""; | ||
try { | ||
Files.walkFileTree(startingDir, this); | ||
} catch (IOException e) { | ||
e.printStackTrace(); | ||
} | ||
} | ||
|
||
@SuppressWarnings("resource") | ||
public FileFinder(Path startingFile) { | ||
Path fileName = startingFile.getFileName(); | ||
Path startingDir = startingFile.getParent(); | ||
ignore = fileName.toString(); | ||
String pattern = ignore.replaceFirst("\\.[^.]+$", ".*"); | ||
matcher = FileSystems.getDefault().getPathMatcher("glob:" + pattern); | ||
try { | ||
Files.walkFileTree(startingDir, this); | ||
} catch (IOException e) { | ||
e.printStackTrace(); | ||
} | ||
} | ||
|
||
private void find(Path file) { | ||
Path name = file.getFileName(); | ||
if (name != null && matcher.matches(name) && !ignore.equals(name.toString())) { | ||
matches.add(file); | ||
} | ||
} | ||
|
||
@Override | ||
public FileVisitResult visitFile(Path file, BasicFileAttributes attrs) { | ||
find(file); | ||
return CONTINUE; | ||
} | ||
|
||
@Override | ||
public FileVisitResult preVisitDirectory(Path dir, BasicFileAttributes attrs) { | ||
return FileVisitResult.CONTINUE; | ||
} | ||
|
||
@Override | ||
public FileVisitResult visitFileFailed(Path file, IOException exc) { | ||
return CONTINUE; | ||
} | ||
|
||
public List<Path> getMatches() { | ||
return matches; | ||
} | ||
|
||
} |
21 changes: 21 additions & 0 deletions
21
src/test/java/info/loenwind/compare/tools/FileFinderTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
package info.loenwind.compare.tools; | ||
|
||
import java.nio.file.Paths; | ||
import java.util.Collections; | ||
|
||
import org.junit.Test; | ||
|
||
import static org.junit.Assert.assertEquals; | ||
|
||
public class FileFinderTest { | ||
|
||
@Test | ||
public final void testFileFinderPath() { | ||
FileFinder finder = new FileFinder(Paths.get("src/test/resources", "test.txt")); | ||
assertEquals("2 matches", 2, finder.getMatches().size()); | ||
Collections.sort(finder.getMatches()); | ||
assertEquals("Found xml neighbour", Paths.get("src/test/resources", "test.xml"), finder.getMatches().get(0)); | ||
assertEquals("Found yml neighbor", Paths.get("src/test/resources", "test.yaml"), finder.getMatches().get(1)); | ||
} | ||
|
||
} |
Empty file.
Empty file.
Empty file.