<!DOCTYPE html><html><head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
</head>
<body>
<br>
<br>
<div class="moz-cite-prefix">On 30/07/2025 19:16, Benoit LEFEVRE
-CAMPUS- wrote:<br>
</div>
<blockquote type="cite" cite="mid:CAL6Huih1j8BqnpA=9ScPTBigdeyVAvn-L+F6UdeKftcS0LWWSQ@mail.gmail.com">
<div dir="ltr">
<div>Hello</div>
<div><br>
</div>
<blockquote style="margin:0 0 0 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:0 0 0 40px;border:none;padding:0px">
<div>
<blockquote style="margin:0 0 0 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:0 0 0 40px;border:none;padding:0px">
<div>
<blockquote style="margin:0 0 0 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:0 0 0 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:0 0 0 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:0 0 0 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>
</body>
</html>