-
Notifications
You must be signed in to change notification settings - Fork 23
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: Support filtered DirectoryStream (#381)
* Add support for filtered DirectoryStreams. * Add unit test --------- Co-authored-by: Marcel Hekman <[email protected]>
- Loading branch information
1 parent
71640bd
commit f6668ba
Showing
3 changed files
with
131 additions
and
12 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
84 changes: 84 additions & 0 deletions
84
src/main/java/org/carlspring/cloud/storage/s3fs/S3FilteredIterator.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,84 @@ | ||
package org.carlspring.cloud.storage.s3fs; | ||
|
||
import java.io.IOException; | ||
import java.nio.file.DirectoryStream.Filter; | ||
import java.nio.file.Path; | ||
import java.util.Iterator; | ||
import java.util.NoSuchElementException; | ||
|
||
public class S3FilteredIterator implements Iterator<Path> | ||
{ | ||
|
||
private final S3Iterator s3iterator; | ||
|
||
private final Filter<? super Path> filter; | ||
|
||
private Path cursor; | ||
|
||
private boolean cursorIsCurrent; | ||
|
||
public S3FilteredIterator(S3Path s3Path, | ||
Filter<? super Path> filter) | ||
{ | ||
this.filter = filter; | ||
this.s3iterator = new S3Iterator(s3Path); | ||
clearCursor(); | ||
} | ||
|
||
@Override | ||
public boolean hasNext() | ||
{ | ||
if (!cursorIsCurrent) | ||
{ | ||
findNextFiltered(); | ||
} | ||
return cursorIsCurrent; | ||
} | ||
|
||
@Override | ||
public Path next() | ||
{ | ||
if (!hasNext()) | ||
{ | ||
throw new NoSuchElementException(); | ||
} | ||
Path next = cursor; | ||
clearCursor(); | ||
return next; | ||
} | ||
|
||
private void findNextFiltered() | ||
{ | ||
try | ||
{ | ||
while (s3iterator.hasNext()) | ||
{ | ||
S3Path next = s3iterator.next(); | ||
if (filter.accept(next)) | ||
{ | ||
cursor = next; | ||
cursorIsCurrent = true; | ||
return; | ||
} | ||
} | ||
clearCursor(); | ||
} | ||
catch (IOException e) | ||
{ | ||
throw new RuntimeException(e); | ||
} | ||
} | ||
|
||
private void clearCursor() | ||
{ | ||
cursor = null; | ||
cursorIsCurrent = false; | ||
} | ||
|
||
@Override | ||
public void remove() | ||
{ | ||
throw new UnsupportedOperationException(); | ||
} | ||
|
||
} |
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