Skip to content

Commit

Permalink
Make FilteredIterator::hasNext idempotent
Browse files Browse the repository at this point in the history
  • Loading branch information
tdrwenski authored and haileyajohnson committed May 2, 2023
1 parent 98258e9 commit e61f325
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 2 deletions.
7 changes: 5 additions & 2 deletions cdm/s3/src/main/java/thredds/filesystem/s3/ControllerS3.java
Original file line number Diff line number Diff line change
Expand Up @@ -141,24 +141,27 @@ private static class FilteredIterator implements Iterator<MFile> {
this.mc = mc;
this.wantDirs = wantDirs;
this.fullInventory = false;
this.next = nextFilteredFile();
}

FilteredIterator(CollectionConfig mc, Iterator<MFile> iter, boolean wantDirs, boolean fullInventory) {
this.orgIter = iter;
this.mc = mc;
this.wantDirs = wantDirs;
this.fullInventory = fullInventory;
this.next = nextFilteredFile();
}

public boolean hasNext() {
next = nextFilteredFile();
return (next != null);
}

public MFile next() {
if (next == null)
throw new NoSuchElementException();
return next;
final MFile mFile = next;
next = nextFilteredFile();
return mFile;
}

public void remove() {
Expand Down
15 changes: 15 additions & 0 deletions cdm/s3/src/test/java/thredds/filesystem/s3/TestControllerS3.java
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,21 @@ public static void setup() {

}

@Test
public void shouldReturnSameValueFromHasNext() throws URISyntaxException {
final CdmS3Uri uri = new CdmS3Uri("cdms3:thredds-test-data");
final MFileFilter filter = new WildcardMatchOnName("testData.nc");
final CollectionConfig collectionConfig = new CollectionConfig(uri.getBucket(), uri.toString(), true, filter, null);
final ControllerS3 controller = new ControllerS3();
final Iterator<MFile> iterator = controller.getInventoryTop(collectionConfig, false);

assertThat(iterator.hasNext()).isTrue();
assertThat(iterator.hasNext()).isTrue();
iterator.next();
assertThat(iterator.hasNext()).isFalse();
assertThat(iterator.hasNext()).isFalse();
}

//////////////////////
// getInventoryTop() tests
//
Expand Down

0 comments on commit e61f325

Please sign in to comment.