<div dir="ltr">Hi Alan<div><br></div><div><blockquote style="margin:0 0 0 40px;border:none;padding:0px"><div>Thanks for your advice !</div></blockquote><blockquote style="margin:0 0 0 40px;border:none;padding:0px"><div><br></div><div>As mentioned in my latest reply to Davor, the goal of this badly chosen example of mine is to point out the edge case where there is only a single, optional, ScopedValue to manage :p</div><div><br></div></blockquote></div><blockquote style="margin:0 0 0 40px;border:none;padding:0px"><div><blockquote style="margin:0 0 0 40px;border:none;padding:0px"><div><i>Is there any reason why the caller can't bind ACCEPT_LANGUAGE to A_DEFAULT_LOCALE so that the callee doesn't need to handle it? :</i></div></blockquote></div></blockquote><div><blockquote style="margin:0 0 0 40px;border:none;padding:0px"><div><br></div></blockquote><blockquote style="margin:0 0 0 40px;border:none;padding:0px"><div>Yes, there is one :</div><div><br></div></blockquote><blockquote style="margin:0 0 0 40px;border:none;padding:0px"><div>This code sample is inspired from an actual Tomcat's Filter, which is general purpose.</div><div><br></div></blockquote><blockquote style="margin:0 0 0 40px;border:none;padding:0px"><div>I.E. it's only there to extract certain generic header parameters from calls to a REST API server, and it's used in each use cases</div><div><br></div></blockquote><blockquote style="margin:0 0 0 40px;border:none;padding:0px">So it's a very technical oriented piece of code, and that's why from my point of view its purpose is not to choose which is the default accept language to be used.<br><br>Sounds better to me to hand over this functional responsibility to each endpoints.<br><br>Best regards<br><br></blockquote>Benoit</div></div><br><div class="gmail_quote gmail_quote_container"><div dir="ltr" class="gmail_attr">Le jeu. 31 juil. 2025 à 08:16, Alan Bateman <<a href="mailto:alan.bateman@oracle.com">alan.bateman@oracle.com</a>> a écrit :<br></div><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left:1px solid rgb(204,204,204);padding-left:1ex"><u></u>
<div>
<br>
<br>
<div>On 30/07/2025 19:16, Benoit LEFEVRE
-CAMPUS- wrote:<br>
</div>
<blockquote type="cite">
<div dir="ltr">
<div>Hello</div>
<div><br>
</div>
<blockquote style="margin:0px 0px 0px 40px;border:none;padding:0px">
<div>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 :<br>
</div>
</blockquote>
<blockquote style="margin:0px 0px 0px 40px;border:none;padding:0px">
<div>
<blockquote style="margin:0px 0px 0px 40px;border:none;padding:0px"><br>
<font face="monospace">public static final ScopedValue<
Locale > ACCEPT_LANGUAGE = ScopedValue.newInstance();<br>
public static final ScopedValue< String > USER_ID
= ScopedValue.newInstance();<br>
<br>
final Locale acceptLanguage =
getAcceptLanguageFromHeader( httpRequest );</font></blockquote>
</div>
</blockquote>
<blockquote style="margin:0px 0px 0px 40px;border:none;padding:0px">
<div>
<blockquote style="margin:0px 0px 0px 40px;border:none;padding:0px"><font face="monospace"><br>
if( acceptLanguage == null ) {<br>
ScopedValue<br>
.where( USER_ID, uid )<br>
.call( MyClass::myStaticCallableMethod );<br>
} else {<br>
ScopedValue<br>
.where( USER_ID, uid )<br>
.where( ACCEPT_LANGUAGE, acceptLanguage )<br>
.call( MyClass::myStaticCallableMethod );<br>
}<br>
</font></blockquote>
</div>
</blockquote>
<blockquote style="margin:0px 0px 0px 40px;border:none;padding:0px">
<div>
<blockquote style="margin:0px 0px 0px 40px;border:none;padding:0px">
<div><br>
</div>
</blockquote>
</div>
</blockquote>
<blockquote style="margin:0px 0px 0px 40px;border:none;padding:0px">
<div>The point for me of doing so, instead of simply setting a
null acceptLanguage, is to have a more readable code
afterward :<br>
<blockquote style="margin:0px 0px 0px 40px;border:none;padding:0px">
<div><br>
</div>
</blockquote>
<blockquote style="margin:0px 0px 0px 40px;border:none;padding:0px"><span style="font-family:monospace">private String
myStaticCallableMethod() {</span><br>
<span style="font-family:monospace"> final Local
localToUse = ACCEPT_LANGUAGE.orElse( A_</span><span style="font-family:monospace">DEFAULT_LOCAL</span><span style="font-family:monospace"> );</span></blockquote>
<blockquote style="margin:0px 0px 0px 40px;border:none;padding:0px"><br>
<span style="font-family:monospace"> ...</span><br>
<font face="monospace">}</font></blockquote>
</div>
</blockquote>
<blockquote style="margin:0px 0px 0px 40px;border:none;padding:0px">
<div>
<div>
<blockquote style="margin:0px 0px 0px 40px;border:none;padding:0px"><br>
</blockquote>
</div>
What is your advise about it ?<br>
</div>
</blockquote>
</div>
</blockquote>
<br>
If the user ID and accept language were (explicit) method parameters
then you might write:<br>
<br>
if (acceptLanguage == null) {<br>
MyClass.myStaticCallableMethod(uid);<br>
} else {<br>
MyClass.myStaticCallableMethod(uid, acceptLanguage);<br>
}<br>
<br>
With implicit method parameters you've got the same if-then-else so
I don't think it is terrible.<br>
<br>
Is there any reason why the caller can't bind ACCEPT_LANGUAGE to
A_DEFAULT_LOCALE so that the callee doesn't need to handle it? That
would change the usage to:<br>
<br>
ScopedValue<br>
.where(USER_ID, uid)<br>
.where(ACCEPT_LANGUAGE, (acceptLanguage != null) ?
acceptLanguage : A_DEFAULT_LOCALE)<br>
.call(...);<br>
<br>
It would then be a bug f myStaticCallableMethod were called without
bindings for either.<br>
<br>
-Alan<br>
<br>
<br>
<span style="font-family:monospace"></span>
</div>
</blockquote></div><div><br clear="all"></div><div><br></div><span class="gmail_signature_prefix">-- </span><br><div dir="ltr" class="gmail_signature"><div dir="ltr">Benoit Lefèvre</div></div>