move common visitors and filters to NBIOWalker

This commit is contained in:
Jonathan Shook
2020-04-20 02:04:14 -05:00
parent 57a2867562
commit 3cbb114d5a
2 changed files with 54 additions and 53 deletions

View File

@@ -8,12 +8,9 @@ import java.io.IOException;
import java.io.InputStream; import java.io.InputStream;
import java.io.Reader; import java.io.Reader;
import java.nio.CharBuffer; import java.nio.CharBuffer;
import java.nio.file.DirectoryStream;
import java.nio.file.FileSystems; import java.nio.file.FileSystems;
import java.nio.file.Path; import java.nio.file.Path;
import java.util.*; import java.util.*;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import java.util.stream.Collectors; import java.util.stream.Collectors;
/** /**
@@ -339,14 +336,15 @@ public class NBIO implements NBPathsAPI.Facets {
for (String searchPath : prefixes) { for (String searchPath : prefixes) {
List<Path> founds = resolver.resolveDirectory(searchPath); List<Path> founds = resolver.resolveDirectory(searchPath);
FileCapture capture = new FileCapture(); NBIOWalker.CollectVisitor capture = new NBIOWalker.CollectVisitor(true,false);
for (Path path : founds) { for (Path path : founds) {
for (String searchPattern : searches) { for (String searchPattern : searches) {
RegexPathFilter filter = new RegexPathFilter(searchPattern, true); NBIOWalker.RegexFilter filter = new NBIOWalker.RegexFilter(searchPattern,true);
// RegexPathFilter filter = new RegexPathFilter(searchPattern, true);
NBIOWalker.walkFullPath(path, capture, filter); NBIOWalker.walkFullPath(path, capture, filter);
} }
} }
capture.found.stream().map(PathContent::new).forEach(foundFiles::add); capture.get().stream().map(PathContent::new).forEach(foundFiles::add);
} }
return new ArrayList<>(foundFiles); return new ArrayList<>(foundFiles);
@@ -380,49 +378,6 @@ public class NBIO implements NBPathsAPI.Facets {
// return expanded; // return expanded;
// } // }
private static class RegexPathFilter implements DirectoryStream.Filter<Path> {
private final Pattern regex;
public RegexPathFilter(String pattern, boolean rightglob) {
if (rightglob && !pattern.startsWith("^") && !pattern.startsWith(".")) {
this.regex = Pattern.compile(".*" + pattern);
} else {
this.regex = Pattern.compile(pattern);
}
}
@Override
public boolean accept(Path entry) throws IOException {
String input = entry.toString();
Matcher matcher = regex.matcher(input);
boolean matches = matcher.matches();
return matches;
}
public String toString() {
return regex.toString();
}
}
private static class FileCapture implements NBIOWalker.PathVisitor, Iterable<Path> {
List<Path> found = new ArrayList<>();
@Override
public void visit(Path foundPath) {
found.add(foundPath);
}
@Override
public Iterator<Path> iterator() {
return found.iterator();
}
public String toString() {
return "FileCapture{n=" + found.size() + (found.size()>0?"," +found.get(0).toString():"") +"}";
}
}
@Override @Override
public String toString() { public String toString() {

View File

@@ -5,12 +5,13 @@ import org.apache.logging.log4j.Logger;
import java.io.IOException; import java.io.IOException;
import java.nio.file.DirectoryStream; import java.nio.file.DirectoryStream;
import java.nio.file.Files;
import java.nio.file.Path; import java.nio.file.Path;
import java.nio.file.attribute.BasicFileAttributes; import java.nio.file.attribute.BasicFileAttributes;
import java.nio.file.spi.FileSystemProvider; import java.nio.file.spi.FileSystemProvider;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.List; import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class NBIOWalker { public class NBIOWalker {
private final static Logger logger = LogManager.getLogger(NBIOWalker.class); private final static Logger logger = LogManager.getLogger(NBIOWalker.class);
@@ -20,7 +21,7 @@ public class NBIOWalker {
} }
public static List<Path> findAll(Path p) { public static List<Path> findAll(Path p) {
Collect fileCollector = new Collect(true, false); CollectVisitor fileCollector = new CollectVisitor(true, false);
walk(p, fileCollector); walk(p, fileCollector);
return fileCollector.get(); return fileCollector.get();
@@ -104,12 +105,31 @@ public class NBIOWalker {
public static DirectoryStream.Filter<Path> WALK_ALL = entry -> true; public static DirectoryStream.Filter<Path> WALK_ALL = entry -> true;
public static class Collect implements PathVisitor { // private static class FileCapture implements NBIOWalker.PathVisitor, Iterable<Path> {
// List<Path> found = new ArrayList<>();
//
// @Override
// public void visit(Path foundPath) {
// found.add(foundPath);
// }
//
// @Override
// public Iterator<Path> iterator() {
// return found.iterator();
// }
//
// public String toString() {
// return "FileCapture{n=" + found.size() + (found.size()>0?"," +found.get(0).toString():"") +"}";
// }
//
// }
public static class CollectVisitor implements PathVisitor {
private final List<Path> listing = new ArrayList<>(); private final List<Path> listing = new ArrayList<>();
private final boolean collectFiles; private final boolean collectFiles;
private final boolean collectDirectories; private final boolean collectDirectories;
public Collect(boolean collectFiles, boolean collectDirectories) { public CollectVisitor(boolean collectFiles, boolean collectDirectories) {
this.collectFiles = collectFiles; this.collectFiles = collectFiles;
this.collectDirectories = collectDirectories; this.collectDirectories = collectDirectories;
@@ -138,4 +158,30 @@ public class NBIOWalker {
} }
} }
public static class RegexFilter implements DirectoryStream.Filter<Path> {
private final Pattern regex;
public RegexFilter(String pattern, boolean rightglob) {
if (rightglob && !pattern.startsWith("^") && !pattern.startsWith(".")) {
this.regex = Pattern.compile(".*" + pattern);
} else {
this.regex = Pattern.compile(pattern);
}
}
@Override
public boolean accept(Path entry) throws IOException {
String input = entry.toString();
Matcher matcher = regex.matcher(input);
boolean matches = matcher.matches();
return matches;
}
public String toString() {
return regex.toString();
}
}
} }