[
https://issues.apache.org/jira/browse/SLING-3714?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel&focusedCommentId=16943163#comment-16943163
]
Robert Munteanu commented on SLING-3714:
----------------------------------------
Another option of evolving the API is to add a default method to the adaptable
which returns an optional
{code:java}
default @NotNull <AdapterType> Optional<AdapterType> tryAdapt(@NotNull
Class<AdapterType> type) {
return Optional.ofNullable(adaptTo(type));
}
{code}
This can in turn be used by clients to check that the adaption succeeed in a
more fluent way
{code:java}
MyAdapted resource = resource.tryAdapt(MyAdapted.class)
.orElseThrow( () -> new RuntimeException("Adaption failed") )
.get();{code}
or enable functional API usage patterns
{code:java}
MyAdapted resource = resource.tryAdapt(MyAdapted.class)
.ifPresent( a -> consume(a) );
{code}
> Allow for a caller to request a non-null response from adaptTo()
> ----------------------------------------------------------------
>
> Key: SLING-3714
> URL: https://issues.apache.org/jira/browse/SLING-3714
> Project: Sling
> Issue Type: Improvement
> Reporter: Justin Edelson
> Priority: Major
>
> See SLING-3709 for a Sling Models-specific request. As I commented there, I
> think this makes more sense as a core change to the Adaptable interface.
> One option:
> resource.adaptTo(Result<Node>.class)
> Which returns a Result object, which has an API like:
> interface Result<T> {
> boolean isSuccess();
> T getObject();
> List<Error> getErrors();
> }
--
This message was sent by Atlassian Jira
(v8.3.4#803005)