Re: FreeMarker and java.util.Optional

Eirik Lygre <[email protected]> Thu, 16 Apr 2015 11:07:10 +0000
Newsgroups gmane.comp.web.freemarker.user
Message-ID <CAHJmSV8hZW6URpfG8Zc8NqJvehv_9Hgt5gQ=Oq_b2bXN_x=AAg@mail.gmail.com>
--===============8969577791170399962==
Content-Type: multipart/alternative; boundary=001a1134188c2600390513d577d1

--001a1134188c2600390513d577d1
Content-Type: text/plain; charset=UTF-8

We ended up adding the following to the wrap() function of our custom
ObjectWrapper, which works for our use-cases. Looking at it now, it should
probably be simplified (and fixed!) to "this.wrap(optional.orElse(null))",
but that's for another day.

if (obj instanceof Optional) {
    Optional optional = (Optional)obj;
    if (optional.isPresent()) {
        return this.wrap(optional.get());
    } else {
        return null;
    }
}


On Tue, Dec 2, 2014 at 11:33 PM Daniel Dekany <[email protected]> wrote:

> Not yet AFAIK, but that's good point. I guess FreeMarker should call
> Optional.orElse(null) whenever it accesses an Optional. Thus ${user}
> and ${user!'unknown'} will work even if `user` is a String or a
> Optional<String>.
>
>
> Monday, December 1, 2014, 12:19:57 PM, Eirik Lygre wrote:
>
> > Hi,
> >
> > Starting with java 8, java supports the new "Optional" class
> > (https://docs.oracle.com/javase/8/docs/api/java/util/Optional.html).
> >
> > * Did anybody give any thought to how this integrates with
> > FreeMarker, for example through a (custom?) ObjectWrapper or the like?
> >
> > We will probably start exposing certain elements in our java layer
> > as Optional<SomeBusinessObject>. We haven't yet really thought about
> > how this integrates with FreeMarker, but I thought it better to ask
> > sooner rather than later, in case somebody has already been through this!
> >
> > Eirik
>
> --
> Thanks,
>  Daniel Dekany
>
>

--001a1134188c2600390513d577d1
Content-Type: text/html; charset=UTF-8
Content-Transfer-Encoding: quoted-printable

<div dir=3D"ltr">We ended up adding the following to the wrap() function of=
 our custom ObjectWrapper, which works for our use-cases. Looking at it now=
, it should probably be simplified (and fixed!) to &quot;this.wrap(optional=
.orElse(null))&quot;, but that&#39;s for another day.<br><br><pre style=3D"=
color:rgb(0,0,0);font-family:&#39;Courier New&#39;"><span style=3D"color:#0=
00080;font-weight:bold">if </span>(obj <span style=3D"color:#000080;font-we=
ight:bold">instanceof </span>Optional) {<br>    Optional optional =3D (Opti=
onal)obj;<br>    <span style=3D"color:#000080;font-weight:bold">if </span>(=
optional.isPresent()) {<br>        <span style=3D"color:#000080;font-weight=
:bold">return this</span>.wrap(optional.get());<br>    } <span style=3D"col=
or:#000080;font-weight:bold">else </span>{<br>        <span style=3D"color:=
#000080;font-weight:bold">return null</span>;<span style=3D"color:#808080;f=
ont-style:italic"><br></span><span style=3D"color:#808080;font-style:italic=
">    </span>}<br>}<br></pre></div><br><div class=3D"gmail_quote">On Tue, D=
ec 2, 2014 at 11:33 PM Daniel Dekany &lt;<a href=3D"mailto:ddekany@freemail=
.hu">[email protected]</a>&gt; wrote:<br><blockquote class=3D"gmail_quote=
" style=3D"margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">N=
ot yet AFAIK, but that&#39;s good point. I guess FreeMarker should call<br>
Optional.orElse(null) whenever it accesses an Optional. Thus ${user}<br>
and ${user!&#39;unknown&#39;} will work even if `user` is a String or a<br>
Optional&lt;String&gt;.<br>
<br>
<br>
Monday, December 1, 2014, 12:19:57 PM, Eirik Lygre wrote:<br>
<br>
&gt; Hi,<br>
&gt;<br>
&gt; Starting with java 8, java supports the new &quot;Optional&quot; class=
<br>
&gt; (<a href=3D"https://docs.oracle.com/javase/8/docs/api/java/util/Option=
al.html" target=3D"_blank">https://docs.oracle.com/javase/8/docs/api/java/u=
til/Optional.html</a>).<br>
&gt;<br>
&gt; * Did anybody give any thought to how this integrates with<br>
&gt; FreeMarker, for example through a (custom?) ObjectWrapper or the like?=
<br>
&gt;<br>
&gt; We will probably start exposing certain elements in our java layer<br>
&gt; as Optional&lt;SomeBusinessObject&gt;. We haven&#39;t yet really thoug=
ht about<br>
&gt; how this integrates with FreeMarker, but I thought it better to ask<br=
>
&gt; sooner rather than later, in case somebody has already been through th=
is!<br>
&gt;<br>
&gt; Eirik<br>
<br>
--<br>
Thanks,<br>
=C2=A0Daniel Dekany<br>
<br>
</blockquote></div>

--001a1134188c2600390513d577d1--


--===============8969577791170399962==
Content-Type: text/plain; charset="us-ascii"
MIME-Version: 1.0
Content-Transfer-Encoding: 7bit
Content-Disposition: inline

------------------------------------------------------------------------------
BPM Camp - Free Virtual Workshop May 6th at 10am PDT/1PM EDT
Develop your own process in accordance with the BPMN 2 standard
Learn Process modeling best practices with Bonita BPM through live exercises
http://www.bonitasoft.com/be-part-of-it/events/bpm-camp-virtual- event?utm_
source=Sourceforge_BPM_Camp_5_6_15&utm_medium=email&utm_campaign=VA_SF
--===============8969577791170399962==
Content-Type: text/plain; charset="us-ascii"
MIME-Version: 1.0
Content-Transfer-Encoding: 7bit
Content-Disposition: inline

_______________________________________________
FreeMarker-user mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/freemarker-user

--===============8969577791170399962==--