feature: #85 | Added try with resources capability#111
feature: #85 | Added try with resources capability#111ShivamNagpal merged 1 commit intoZeplinko:developfrom
Conversation
📝 WalkthroughWalkthroughAdds new Try helpers for executing throwables and managing one- or two AutoCloseable resources (including a consumer variant), plus comprehensive unit tests for these helpers. Also updates the macOS section of Changes
Sequence Diagram(s)sequenceDiagram
%%{init: {"themeVariables": {"actorBackground":"#E8F6EF","actorBorder":"#2E8B57","noteBackground":"#FFF8E1"}}}%%
participant Caller as Caller
participant TryUtil as Try
participant R1 as Resource1
participant R2 as Resource2
participant Action as Action
Caller->>TryUtil: withResources(resourceSupplier1, resourceSupplier2, action)
alt open resources
TryUtil->>R1: resourceSupplier1.get()
Note right of R1: R1 opened
TryUtil->>R2: resourceSupplier2.get()
Note right of R2: R2 opened
TryUtil->>Action: action.apply(R1, R2)
Action-->>TryUtil: result / throws
end
alt success
TryUtil-->>Caller: Try.success(result)
else exception
TryUtil-->>Caller: Try.failure(exception)
end
par close resources
R2-->>TryUtil: close()
R1-->>TryUtil: close()
end
Estimated code review effort🎯 3 (Moderate) | ⏱️ ~25 minutes Poem
Pre-merge checks and finishing touches❌ Failed checks (1 warning)
✅ Passed checks (2 passed)
✨ Finishing touches
📜 Recent review detailsConfiguration used: defaults Review profile: CHILL Plan: Pro 📒 Files selected for processing (3)
🚧 Files skipped from review as they are similar to previous changes (1)
🧰 Additional context used🧬 Code graph analysis (1)src/test/java/org/zeplinko/commons/lang/ext/core/TryTest.java (1)
🔇 Additional comments (10)
Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out. Comment |
Codecov Report✅ All modified and coverable lines are covered by tests. 📢 Thoughts on this report? Let us know! |
There was a problem hiding this comment.
Actionable comments posted: 0
🧹 Nitpick comments (1)
src/main/java/org/zeplinko/commons/lang/ext/core/Try.java (1)
179-213: Consider documenting resource closure order.The implementation correctly manages two resources using try-with-resources. The resources will be closed in reverse order of their acquisition (r2, then r1), which is the standard Java behavior.
Consider adding a note in the JavaDoc about the closure order to help users understand cleanup semantics, especially if they have resources with dependencies.
📝 Optional JavaDoc enhancement
/** * Executes a block of code with two resources that are automatically closed, * capturing any exceptions into a {@code Try}. + * <p> + * Resources are closed in reverse order of acquisition: the second resource is + * closed first, followed by the first resource. If closing a resource throws an + * exception, it is added as a suppressed exception if the action also threw. + * </p> * * @param resourceSupplier1 A supplier that creates the first resource.
📜 Review details
Configuration used: defaults
Review profile: CHILL
Plan: Pro
📒 Files selected for processing (3)
.gitignoresrc/main/java/org/zeplinko/commons/lang/ext/core/Try.javasrc/test/java/org/zeplinko/commons/lang/ext/core/TryTest.java
🧰 Additional context used
🧬 Code graph analysis (1)
src/test/java/org/zeplinko/commons/lang/ext/core/TryTest.java (1)
src/main/java/org/zeplinko/commons/lang/ext/core/Try.java (1)
Try(80-748)
🔇 Additional comments (10)
.gitignore (1)
39-40: LGTM!The macOS-specific ignores are appropriate.
.DS_Storeis standard for macOS directory metadata, and.agentappears to be a project-specific file.src/test/java/org/zeplinko/commons/lang/ext/core/TryTest.java (5)
1191-1234: LGTM! Comprehensive test coverage forTry.run().The tests properly verify:
- Null parameter validation
- Successful execution returning
Empty- Exception propagation
InterruptedExceptionhandling with thread interrupt flag restoration and cleanup
1256-1316: LGTM! Thorough test coverage for single-resourcewithResources.The tests verify all critical paths:
- Resource closure on success
- Exception handling when supplier fails (resource not created)
- Exception handling when action fails (resource properly closed)
InterruptedExceptionhandling with proper thread state management
1348-1439: LGTM! Excellent test coverage for two-resourcewithResources.The tests verify proper resource management:
- Both resources closed on success
- First resource closed when second supplier fails
- Both resources closed when action fails
- Proper
InterruptedExceptionhandlingThe resource closure order testing is particularly valuable for ensuring try-with-resources semantics are maintained.
1462-1528: LGTM! Complete test coverage forconsumeResource.The tests verify:
- Successful side-effect execution with resource closure
- Exception handling in both supplier and action
- Proper
InterruptedExceptionhandling with thread state restoration
1530-1541: LGTM! Simple and effective test helper.The
TestResourceclass provides a clean way to verify resource closure semantics without unnecessary complexity.src/main/java/org/zeplinko/commons/lang/ext/core/Try.java (4)
5-9: LGTM! Necessary imports for the new API.The throwing functional interface imports support the new resource management methods.
132-150: LGTM! Clean implementation ofrun()method.The implementation correctly:
- Validates the runnable parameter
- Executes the runnable and captures exceptions
- Restores the thread interrupt flag on
InterruptedException- Returns
Try<Empty>for void operationsThe pattern is consistent with the existing
to()method.
152-177: LGTM! Proper single-resource management implementation.The method correctly uses try-with-resources to ensure the resource is closed regardless of whether the action succeeds or fails. The
InterruptedExceptionhandling follows best practices by restoring the thread's interrupt status.
215-240: LGTM! Proper void-operation resource management.The
consumeResourcemethod appropriately handles side-effect operations that don't return a value, following the same error handling and resource cleanup patterns as the other methods.
|
@codex review |
1 similar comment
|
@codex review |
|
To use Codex here, create a Codex account and connect to github. |
|
@codex review |
|
To use Codex here, create a Codex account and connect to github. |
|
@codex review |
There was a problem hiding this comment.
💡 Codex Review
Here are some automated review suggestions for this pull request.
Reviewed commit: a10519e0b8
ℹ️ About Codex in GitHub
Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you
- Open a pull request for review
- Mark a draft as ready
- Comment "@codex review".
If Codex has suggestions, it will comment; otherwise it will react with 👍.
Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".
There was a problem hiding this comment.
Pull request overview
Adds new @Preview APIs to Try to support void execution and try-with-resources-style resource management while capturing exceptions into Try, along with unit tests and ignore-list updates.
Changes:
- Added
Try.run(...),Try.withResources(...)(1- and 2-resource variants), andTry.consumeResource(...)helper methods. - Added comprehensive unit tests for the new APIs, including interruption handling and resource closing behavior.
- Updated
.gitignoreto ignore.DS_Storeand.agent.
Reviewed changes
Copilot reviewed 2 out of 3 changed files in this pull request and generated 4 comments.
| File | Description |
|---|---|
src/main/java/org/zeplinko/commons/lang/ext/core/Try.java |
Introduces new @Preview helper methods for throwing runnables and try-with-resources patterns returning Try. |
src/test/java/org/zeplinko/commons/lang/ext/core/TryTest.java |
Adds test coverage for success/failure paths, null-argument behavior, resource closing, and InterruptedException handling. |
.gitignore |
Adds .agent and normalizes .DS_Store entry. |
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
New Methods (all marked @Preview)
Try.run(ThrowingRunnable)Try.withResources(ThrowingSupplier<R>, ThrowingFunction<R,T>)Try.withResources(ThrowingSupplier<R1>, ThrowingSupplier<R2>, ThrowingBiFunction<R1,R2,T>)Try.consumeResource(ThrowingSupplier<R>, ThrowingConsumer<R>)Usage Examples
Summary by CodeRabbit
New Features
Tests
Chores
✏️ Tip: You can customize this high-level summary in your review settings.