[ScopedValue] best way to conditionally bind a value ?

Benoit LEFEVRE -CAMPUS- benoit.lefevre at decathlon.com
Thu Jul 31 08:51:00 UTC 2025


Hi Davor

Thank you for your reply

Indeed, I had a thought about doing such a thing

But with this solution, involving the direct usage of a Carrier, a trouble
would arise :

What if I only had to manage a single optional ACCEPT_LANGUAGE scope value ?

I wouldn't have other choice to either write a cumbersome code on the
caller side :

if( acceptLanguage == null ) {
    ScopedValue
        .where( ACCEPT_LANGUAGE, acceptLanguage )
        .call( MyClass::myStaticCallableMethod );
} else {
    MyClass.myStaticCallableMethod();
}


or to write something more straightforward :


 ScopedValue
    .where( ACCEPT_LANGUAGE, acceptLanguage ) // null binding is allowed
here
    .call( MyClass::myStaticCallableMethod );


... but in that later case, it would rule out the use of .isBound() or the
.orElse() method on the callee side :

private String myStaticCallableMethod() {
    final Local localToUse = ACCEPT_LANGUAGE.get() == null
        ? A_DEFAULT_LOCAL
        : ACCEPT_LANGUAGE.get();

    ...
}


I feel like this is because there is no possibility to get an "emtpy"
carrier from the ScopedValue API, but on the other hand, I guess it's a
conception decision that has been made on purpose, else it would have
enabled writing useless code such as :

ScopedValue

    .getEmptyCarrier()

    .call( MyClass::myStaticCallableMethod );


best regards


Benoit

Le mer. 30 juil. 2025 à 23:31, Davor Hrg <hrgdavor at gmail.com> a écrit :

> I personally would try this variant,
>
> Carrier carrier = ScopedValue.where( USER_ID, uid );
> if( acceptLanguage == null ) {
>     carrier = carrier.where( ACCEPT_LANGUAGE, acceptLanguage );
> }
> carrier.call( MyClass::myStaticCallableMethod );
>
> br,
> Davor Hrg
>
>
> On Wed, Jul 30, 2025 at 8:18 PM Benoit LEFEVRE -CAMPUS- <
> benoit.lefevre at decathlon.com> wrote:
>
>> Hello
>>
>> After using the ScopedValue for a few days, I'm wondering if there is a
>> better way to conditionally bind a value than doing the following :
>>
>>
>> public static final ScopedValue< Locale > ACCEPT_LANGUAGE =
>> ScopedValue.newInstance();
>> public static final ScopedValue< String > USER_ID =
>> ScopedValue.newInstance();
>>
>> final Locale acceptLanguage = getAcceptLanguageFromHeader( httpRequest );
>>
>>
>> if( acceptLanguage == null ) {
>>     ScopedValue
>>         .where( USER_ID, uid )
>>         .call( MyClass::myStaticCallableMethod );
>> } else {
>>     ScopedValue
>>         .where( USER_ID, uid )
>>         .where( ACCEPT_LANGUAGE, acceptLanguage )
>>         .call( MyClass::myStaticCallableMethod );
>> }
>>
>>
>> The point for me of doing so, instead of simply setting a null
>> acceptLanguage, is to have a more readable code afterward :
>>
>>
>> private String myStaticCallableMethod() {
>>     final Local localToUse = ACCEPT_LANGUAGE.orElse( A_DEFAULT_LOCAL );
>>
>>
>>     ...
>> }
>>
>>
>> What is your advise about it ?
>>
>>
>> Best regards
>>
>> --
>> Benoit Lefèvre
>>
>

-- 
Benoit Lefèvre
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://mail.openjdk.org/pipermail/loom-dev/attachments/20250731/2740f6e4/attachment.htm>


More information about the loom-dev mailing list