Skip to content

Commit 7eaeaf1

Browse files
committed
issue #2: add find command
1 parent 4dbbeca commit 7eaeaf1

File tree

2 files changed

+54
-1
lines changed

2 files changed

+54
-1
lines changed

src/main/java/io/github/benas/unixstream/UnixStream.java

Lines changed: 25 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,15 +3,17 @@
33
import io.github.benas.unixstream.components.WordCount;
44

55
import java.io.*;
6-
import java.nio.file.Paths;
6+
import java.nio.file.*;
77
import java.util.Arrays;
88
import java.util.Comparator;
99
import java.util.Date;
1010
import java.util.Objects;
1111
import java.util.function.Predicate;
1212
import java.util.stream.Stream;
1313

14+
import static java.nio.file.Files.isDirectory;
1415
import static java.nio.file.Files.lines;
16+
import static java.nio.file.Files.walk;
1517

1618
/**
1719
* This interface is the main entry point to use UnixStream.
@@ -129,6 +131,28 @@ static UnixStream<String> pwd() {
129131
return new UnixStreamImpl<>(Stream.of(Paths.get("").toFile().getAbsolutePath()));
130132
}
131133

134+
135+
/**
136+
* Find files by name (recursively) in a given directory.
137+
*
138+
* @param directory the root directory
139+
* @param pattern the file name pattern with <a href="https://docs.oracle.com/javase/tutorial/essential/io/fileOps.html#glob">glob syntax</a>.
140+
* @return a new UnixStream with found files
141+
*/
142+
static UnixStream<Path> find(final Path directory, final String pattern) {
143+
Objects.requireNonNull(directory, "The root directory must not be null");
144+
Objects.requireNonNull(pattern, "The file pattern must not be null");
145+
PathMatcher pathMatcher = FileSystems.getDefault().getPathMatcher("glob:" + pattern);
146+
try {
147+
return new UnixStreamImpl<>(walk(directory)
148+
.filter(path -> !isDirectory(path))
149+
.filter(path -> pathMatcher.matches(path.getFileName())));
150+
151+
} catch (IOException e) {
152+
throw new RuntimeException("Unable to find files in directory " + directory);
153+
}
154+
}
155+
132156
/**
133157
* Create a new UnixStream fro the given stream.
134158
*
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
package io.github.benas.unixstream.components;
2+
3+
import io.github.benas.unixstream.UnixStream;
4+
import org.junit.Test;
5+
6+
import java.nio.file.Path;
7+
import java.nio.file.Paths;
8+
9+
import static org.assertj.core.api.Assertions.assertThat;
10+
11+
public class FindTest {
12+
13+
private static final String TEST_RESOURCES_DIRECTORY = "src/test/resources";
14+
15+
@Test
16+
public void find_whenFilesExist() throws Exception {
17+
UnixStream<Path> stream = UnixStream.find(Paths.get(TEST_RESOURCES_DIRECTORY), "*.txt");
18+
19+
assertThat(stream).containsExactly(Paths.get(TEST_RESOURCES_DIRECTORY, "input.txt"));
20+
}
21+
22+
@Test
23+
public void find_whenNoFileExist() throws Exception {
24+
UnixStream<Path> stream = UnixStream.find(Paths.get(TEST_RESOURCES_DIRECTORY), "*.blah");
25+
26+
assertThat(stream).isEmpty();
27+
}
28+
29+
}

0 commit comments

Comments
 (0)