Skip to content

Add SafeCastSnippet Utility - #267

Open
ayshgoyl wants to merge 1 commit into
iluwatar:masterfrom
ayshgoyl:master
Open

Add SafeCastSnippet Utility#267
ayshgoyl wants to merge 1 commit into
iluwatar:masterfrom
ayshgoyl:master

Conversation

@ayshgoyl

Copy link
Copy Markdown

Summary

Fixes #246
Implements the SafeCastSnippet utility for safely casting objects using generics and returning the result as an Optional, avoiding ClassCastException.

Changes

  • Added SafeCastSnippet with a generic safeCast(Object obj, Class<T> clazz) method.
  • Returns Optional.empty() when the object cannot be cast to the requested type.
  • Added unit tests covering both successful and unsuccessful cast scenarios.
  • Updated the README to include the new snippet.

Testing

  • Verified the project builds successfully with Java 21.
  • Ran the test suite successfully using:
./gradlew clean test

Signed-off-by: Ayush Goyal <ayush.s1@somaiya.edu>
@stale

stale Bot commented Jul 26, 2026

Copy link
Copy Markdown

This pull request has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs.

@stale stale Bot added the status: stale label Jul 26, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Add safe cast snippet

1 participant