Skip to content

OAK-11571: commons: add Closer class (similar to Guava Closer)#2181

Merged
reschke merged 16 commits intotrunkfrom
OAK-11571
Mar 17, 2025
Merged

OAK-11571: commons: add Closer class (similar to Guava Closer)#2181
reschke merged 16 commits intotrunkfrom
OAK-11571

Conversation

@reschke
Copy link
Contributor

@reschke reschke commented Mar 13, 2025

Note

1b60437

which added tests for Guava Closer. The Oak implementation only was written after tests were there.

@reschke reschke self-assigned this Mar 13, 2025
@github-actions
Copy link

github-actions bot commented Mar 13, 2025

Commit-Check ✔️

@reschke reschke marked this pull request as draft March 13, 2025 15:38
@reschke reschke marked this pull request as ready for review March 13, 2025 15:40
@reschke
Copy link
Contributor Author

reschke commented Mar 14, 2025

I believe all comments were based on misunderstandings about what Guava does; see again d254e8f which has the same tests running with Guava.

@thomasmueller - every time we instantiate a collection with a non-default size, readers of the code will ask themselves: why? and does it really matter here?

@reschke
Copy link
Contributor Author

reschke commented Mar 14, 2025

Things IMHO left to do:

  • address some Sonar warnings
  • improve Javadoc
  • rename the completely confusingly named method rethrow() to rethrowOnClose() - not done, because I misunterstood the semantics
  • bikeshed about the package name - any opinions here???

Note that once this is in (and used throughout); we can get rid of the entire Guava common.io package.

fail("should not throw but got: " + e);
}
}
}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

    public void compareClosers() {
        // when rethrow was called, Exceptions that happened upon close will be swallowed

        com.google.common.io.Closer guavaCloser = com.google.common.io.Closer.create();
        Closer oakCloser = Closer.create();

        try {
            throw oakCloser.rethrow(new InterruptedException());
        } catch (Exception e) {}

        try {
            throw guavaCloser.rethrow(new InterruptedException());
        } catch (Exception e) {}

        try {
            oakCloser.register(() -> { throw new RuntimeException(); });
            oakCloser.close();
        } catch (Exception e) {
            fail("should not throw but got: " + e);
        }

        try {
            guavaCloser.register(() -> { throw new RuntimeException(); });
            guavaCloser.close();
        } catch (Exception e) {
            fail("should not throw but got: " + e);
        }
    }

would currently fail.

Copy link
Contributor

@mbaedke mbaedke left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

RuntimeException are not yet treated correctly, see inline comments.

…e unchecked exceptions thrown by Closeables when rethrow was called
@reschke reschke requested a review from mbaedke March 14, 2025 18:42
@sonarqubecloud
Copy link

@reschke reschke merged commit 01179a5 into trunk Mar 17, 2025
5 checks passed
@reschke reschke deleted the OAK-11571 branch March 26, 2025 08:17
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants