Skip to content

Commit

Permalink
updated java.7 home property for new java 7 SDK, pushed common test s…
Browse files Browse the repository at this point in the history
…etup into new BaseFileTest class, added tests to DirectoryStreamTest, added FilterBuilder class added method to DirUtils created AsynchronousRecursiveDirectoryStream and test
  • Loading branch information
bbejeck committed Feb 10, 2012
1 parent 062667a commit 36d881c
Show file tree
Hide file tree
Showing 9 changed files with 467 additions and 56 deletions.
5 changes: 4 additions & 1 deletion README.txt
@@ -1 +1,4 @@
Source code for the Java 7 java.nio.file blog series. Please note that the pom file needs to have the path for Java 7 set as I'm still using Java 6 as my default and needed to specify in the pom where Java 7 lives for compilation and tests. Source code for the Java 7 java.nio.file blog series.
Please note that the pom file needs to have the path for Java 7
set as I'm still using Java 6 as my default JDK and needed to specify
in the pom where Java 7 lives for compilation and tests.
2 changes: 1 addition & 1 deletion pom.xml
Expand Up @@ -67,6 +67,6 @@
</build> </build>


<properties> <properties>
<java.7.home>/usr/share/java/java7</java.7.home> <java.7.home>/usr/share/java/java7/Contents/Home</java.7.home>
</properties> </properties>
</project> </project>
@@ -0,0 +1,117 @@
package bbejeck.nio.files.directory;

import bbejeck.nio.files.visitor.FunctionVisitor;
import bbejeck.nio.util.FilterBuilder;
import com.google.common.base.Function;

import java.io.IOException;
import java.nio.file.DirectoryStream;
import java.nio.file.FileVisitResult;
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.Iterator;
import java.util.Objects;
import java.util.concurrent.*;

/**
* Created by IntelliJ IDEA.
* User: bbejeck
* Date: 2/5/12
* Time: 1:05 PM
*/
public class AsynchronousRecursiveDirectoryStream implements DirectoryStream<Path> {

private LinkedBlockingQueue<Path> pathsBlockingQueue = new LinkedBlockingQueue<>();
private boolean closed = false;
private FutureTask<Void> pathTask;
private Path startPath;
private Filter filter;

public AsynchronousRecursiveDirectoryStream(Path startPath, String pattern) throws IOException {
this.filter = FilterBuilder.buildGlobFilter(Objects.requireNonNull(pattern));
this.startPath = Objects.requireNonNull(startPath);
}

@Override
public Iterator<Path> iterator() {
confirmNotClosed();
findFiles(startPath, filter);
return new Iterator<Path>() {
Path path;
@Override
public boolean hasNext() {
try {
path = pathsBlockingQueue.poll();
while (!pathTask.isDone() && path == null) {
path = pathsBlockingQueue.poll(5, TimeUnit.MILLISECONDS);
}
return (path != null) ? true : false;
} catch (InterruptedException e) {
Thread.currentThread().interrupt();
}
return false;
}

@Override
public Path next() {
return path;
}

@Override
public void remove() {
throw new UnsupportedOperationException("Removal not supported");
}
};
}

private void findFiles(final Path startPath, final Filter filter) {
pathTask = new FutureTask<Void>(new Callable<Void>() {
@Override
public Void call() throws Exception {
Files.walkFileTree(startPath, new FunctionVisitor(getFunction(filter)));
return null;
}
});
start(pathTask);
}

private Function<Path, FileVisitResult> getFunction(final Filter<Path> filter) {
return new Function<Path, FileVisitResult>() {
@Override
public FileVisitResult apply(Path input) {
try {
if (filter.accept(input.getFileName())) {
pathsBlockingQueue.offer(input);
}
} catch (IOException e) {
throw new RuntimeException(e.getMessage());
}
return (pathTask.isCancelled()) ? FileVisitResult.TERMINATE : FileVisitResult.CONTINUE;
}
};
}


@Override
public void close() throws IOException {
if(pathTask !=null){
pathTask.cancel(true);
}
pathsBlockingQueue.clear();
pathsBlockingQueue = null;
pathTask = null;
filter = null;
closed = true;
}

private void start(FutureTask<Void> futureTask) {
new Thread(futureTask).start();
}

private void confirmNotClosed() {
if (closed) {
throw new IllegalStateException("DirectoryStream has already been closed");
}
}

}
18 changes: 14 additions & 4 deletions src/main/java/bbejeck/nio/util/DirUtils.java
@@ -1,14 +1,12 @@
package bbejeck.nio.util; package bbejeck.nio.util;


import bbejeck.nio.files.directory.AsynchronousRecursiveDirectoryStream;
import bbejeck.nio.files.visitor.*; import bbejeck.nio.files.visitor.*;
import com.google.common.base.Function; import com.google.common.base.Function;
import com.google.common.base.Predicate; import com.google.common.base.Predicate;


import java.io.IOException; import java.io.IOException;
import java.nio.file.FileVisitOption; import java.nio.file.*;
import java.nio.file.FileVisitResult;
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.EnumSet; import java.util.EnumSet;
import java.util.Objects; import java.util.Objects;


Expand Down Expand Up @@ -126,6 +124,18 @@ public static void copyWithPredicate(Path from, Path to, Predicate<Path> predica
validate(from); validate(from);
Files.walkFileTree(from, new CopyPredicateVisitor(from,to,predicate)); Files.walkFileTree(from, new CopyPredicateVisitor(from,to,predicate));
} }

/**
* Returns a DirectoryStream that can iterate over files found recursively based on the pattern provided
* @param startPath the Directory to start from
* @param pattern the glob to match against files
* @return DirectoryStream
* @throws IOException
*/
public static DirectoryStream<Path> glob(Path startPath, String pattern) throws IOException{
validate(startPath);
return new AsynchronousRecursiveDirectoryStream(startPath,pattern);
}






Expand Down
45 changes: 45 additions & 0 deletions src/main/java/bbejeck/nio/util/FilterBuilder.java
@@ -0,0 +1,45 @@
package bbejeck.nio.util;

import java.io.IOException;
import java.nio.file.DirectoryStream;
import java.nio.file.FileSystems;
import java.nio.file.Path;
import java.nio.file.PathMatcher;

/**
* Created by IntelliJ IDEA.
* User: bbejeck
* Date: 2/2/12
* Time: 11:46 PM
*/

public class FilterBuilder {

private FilterBuilder() {}

public static DirectoryStream.Filter<Path> buildGlobFilter(String pattern) {
final PathMatcher pathMatcher = getPathMatcher("glob:"+pattern);
return new DirectoryStream.Filter<Path>() {
@Override
public boolean accept(Path entry) throws IOException {
return pathMatcher.matches(entry);
}
};
}

public static DirectoryStream.Filter<Path> buildRegexFilter(String pattern) {
final PathMatcher pathMatcher = getPathMatcher("regex:"+pattern);
return new DirectoryStream.Filter<Path>() {
@Override
public boolean accept(Path entry) throws IOException {
return pathMatcher.matches(entry);
}
};
}


private static PathMatcher getPathMatcher(String pattern) {
return FileSystems.getDefault().getPathMatcher(pattern);
}

}
79 changes: 79 additions & 0 deletions src/test/java/bbejeck/nio/files/BaseFileTest.java
@@ -0,0 +1,79 @@
package bbejeck.nio.files;

import bbejeck.nio.util.DirUtils;
import bbejeck.nio.util.FileGenerator;
import org.junit.Before;

import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;

/**
* Created by IntelliJ IDEA.
* User: bbejeck
* Date: 1/31/12
* Time: 9:31 PM
*/

public class BaseFileTest {
protected String baseDir = "test-files";
protected String dir1 = "dir1";
protected String dir2 = "dir2";
protected String fileName;
protected String fooDir = "foo";
protected String copyDir = "copy";
protected String tempDir = "temp";
protected Path basePath;
protected Path dir1Path;
protected Path dir2Path;
protected Path fooPath;
protected Path sourcePath;
protected Path copyPath;
protected String[] fakeJavaFiles = new String[]{"Code.java", "Foo.java", "Bar.java", "Baz.java"};
protected String[] textFileNames = new String[]{"persons.csv", "counts.csv", "CountyTaxes.csv"};


@Before
public void setUp() throws Exception {
basePath = Paths.get(baseDir);
dir1Path = basePath.resolve(dir1);
dir2Path = basePath.resolve(dir2);
fileName = "test.txt";
sourcePath = basePath.resolve(fileName);
copyPath = basePath.resolve(copyDir);
fooPath = basePath.resolve(fooDir);
cleanUp();
createDirectories();
generateFile(Paths.get(baseDir, fileName), 500);
generateFiles(basePath, fakeJavaFiles, textFileNames);
generateFiles(dir1Path, fakeJavaFiles);
generateFiles(dir2Path, fakeJavaFiles);
}

protected void cleanUp() throws Exception {
DirUtils.deleteIfExists(basePath);
Files.deleteIfExists(basePath.resolve(Paths.get(copyDir, fooDir)));
Files.deleteIfExists(basePath.resolve(Paths.get(copyDir, tempDir)));
Files.deleteIfExists(basePath.resolve(tempDir));
Files.deleteIfExists(basePath.resolve("tempII"));
}

protected void createDirectories() throws Exception {
Files.createDirectories(dir1Path);
Files.createDirectories(dir2Path);
Files.createDirectories(copyPath);
Files.createDirectories(fooPath);
}

protected void generateFile(Path path, int numberLines) throws Exception {
FileGenerator.generate(path, numberLines);
}

protected void generateFiles(Path path, String[]... fileNames) throws Exception {
for (String[] fileNamesArray : fileNames) {
for (String fileName : fileNamesArray) {
generateFile(path.resolve(fileName), 10);
}
}
}
}

0 comments on commit 36d881c

Please sign in to comment.