Details
-
Improvement
-
Status: Closed
-
Major
-
Resolution: Duplicate
-
None
-
None
-
None
Description
We have had long-standing discussions about the fact that adaptTo returning null can lead to many hard-to-trace bugs.
One option of evolving the API is to add a default method to the adaptable which returns an optional
default @NotNull <AdapterType> Optional<AdapterType> tryAdapt(@NotNull Class<AdapterType> type) { return Optional.ofNullable(adaptTo(type)); }
This can in turn be used by clients to check that the adaption succeeed in a more fluent way
MyAdapted resource = resource.tryAdapt(MyAdapted.class) .orElseThrow( () -> new RuntimeException("Adaption failed") ) .get();
or enable functional API usage patterns
MyAdapted resource = resource.tryAdapt(MyAdapted.class) .ifPresent( a -> consume(a) );
Attachments
Issue Links
- duplicates
-
SLING-3714 Allow for a caller to request a non-null response from adaptTo()
-
- Open
-
- relates to
-
SLING-3714 Allow for a caller to request a non-null response from adaptTo()
-
- Open
-
-
SLING-3709 Sling Models: Allow caller to deal with exceptions
-
- Closed
-