Skip to content

Commit 24f3332

Browse files
committed
add methods with String directory in ls and find
1 parent 9a65dbd commit 24f3332

File tree

3 files changed

+37
-0
lines changed

3 files changed

+37
-0
lines changed

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

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -142,6 +142,17 @@ static UnixStream<Path> ls(final Path directory) throws IOException {
142142
return new UnixStreamImpl<>(Files.list(directory));
143143
}
144144

145+
/**
146+
* Create an UnixStream with file names of the given directory.
147+
*
148+
* @param directory the directory from which to list files
149+
* @return an UnixStream with file names of the given directory.
150+
* @throws IOException thrown if an error occurs during reading the directory
151+
*/
152+
static UnixStream<Path> ls(final String directory) throws IOException {
153+
return ls(Paths.get(directory));
154+
}
155+
145156
/**
146157
* Create a new UnixStream with the absolute path of the current directory.
147158
*
@@ -177,6 +188,18 @@ static UnixStream<Path> find(final Path directory, final String pattern) throws
177188
.filter(path -> pathMatcher.matches(path.getFileName())));
178189
}
179190

191+
/**
192+
* Find files by name (recursively) in a given directory.
193+
*
194+
* @param directory the root directory
195+
* @param pattern the file name pattern with <a href="https://docs.oracle.com/javase/tutorial/essential/io/fileOps.html#glob">glob syntax</a>.
196+
* @return a new UnixStream with found files
197+
* @throws IOException thrown if an error occurs during reading the file
198+
*/
199+
static UnixStream<Path> find(final String directory, final String pattern) throws IOException {
200+
return find(Paths.get(directory), pattern);
201+
}
202+
180203
/**
181204
* Create a new UnixStream from the given stream.
182205
*

src/test/java/io/github/benas/unixstream/components/FindTest.java

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,4 +26,11 @@ public void find_whenNoFileExist() throws Exception {
2626
assertThat(stream).isEmpty();
2727
}
2828

29+
@Test
30+
public void findWithStringDirectory_whenFilesExist() throws Exception {
31+
UnixStream<Path> stream = UnixStream.find(TEST_RESOURCES_DIRECTORY, "*.txt");
32+
33+
assertThat(stream).containsExactly(Paths.get(TEST_RESOURCES_DIRECTORY, "input.txt"));
34+
}
35+
2936
}

src/test/java/io/github/benas/unixstream/components/LsTest.java

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,4 +26,11 @@ public void ls() throws IOException {
2626
Paths.get("unix-stream.jpeg")
2727
);
2828
}
29+
30+
@Test
31+
public void ls_directory() throws IOException {
32+
UnixStream<Path> stream = UnixStream.ls("src/test/resources");
33+
34+
assertThat(stream).contains(Paths.get("src/test/resources","input.txt"));
35+
}
2936
}

0 commit comments

Comments
 (0)