<?xml version="1.0" encoding="utf-8"?>
<!-- generator="FeedCreator 1.7.2-ppt DokuWiki" -->
<?xml-stylesheet href="http://www.coconut-palm-software.com/the_new_visual_editor/lib/exe/css.php?s=feed" type="text/css"?>
<rdf:RDF
    xmlns="http://purl.org/rss/1.0/"
    xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#"
    xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
    xmlns:dc="http://purl.org/dc/elements/1.1/">
    <channel rdf:about="http://www.coconut-palm-software.com/the_new_visual_editor/feed.php">
        <title>The Visual Editor blog</title>
        <description></description>
        <link>http://www.coconut-palm-software.com/the_new_visual_editor/</link>
        <image rdf:resource="http://www.coconut-palm-software.com/the_new_visual_editor/lib/tpl/monobook/images/favicon.ico" />
       <dc:date>2013-05-18T17:45:57-04:00</dc:date>
        <items>
            <rdf:Seq>
                <rdf:li rdf:resource="http://www.coconut-palm-software.com/the_new_visual_editor/doku.php?id=blog:improvements_on_null_safety_for_java_and_eclipse"/>
            </rdf:Seq>
        </items>
    </channel>
    <image rdf:about="http://www.coconut-palm-software.com/the_new_visual_editor/lib/tpl/monobook/images/favicon.ico">
        <title>The Visual Editor</title>
        <link>http://www.coconut-palm-software.com/the_new_visual_editor/</link>
        <url>http://www.coconut-palm-software.com/the_new_visual_editor/lib/tpl/monobook/images/favicon.ico</url>
    </image>
    <item rdf:about="http://www.coconut-palm-software.com/the_new_visual_editor/doku.php?id=blog:improvements_on_null_safety_for_java_and_eclipse">
        <dc:format>text/html</dc:format>
        <dc:date>2013-02-08T13:52:02-04:00</dc:date>
        <dc:creator>djo</dc:creator>
        <title>blog:improvements_on_null_safety_for_java_and_eclipse - [Improving our Option implementation] </title>
        <link>http://www.coconut-palm-software.com/the_new_visual_editor/doku.php?id=blog:improvements_on_null_safety_for_java_and_eclipse</link>
        <description>
&lt;h1 class=&quot;sectionedit1&quot;&gt;&lt;a name=&quot;improvements_on_null_safety_for_java_and_eclipse&quot; id=&quot;improvements_on_null_safety_for_java_and_eclipse&quot;&gt;Improvements on Null Safety for Java and Eclipse&lt;/a&gt;&lt;/h1&gt;
&lt;div class=&quot;level1&quot;&gt;

&lt;p&gt;
In their introduction to their implementation of the Optional&amp;lt;T&amp;gt; type, the Guava developers observe: &lt;sup&gt;&lt;a href=&quot;#fn__1&quot; name=&quot;fnt__1&quot; id=&quot;fnt__1&quot; class=&quot;fn_top&quot;&gt;1)&lt;/a&gt;&lt;/sup&gt;
&lt;/p&gt;
&lt;ul&gt;
&lt;li class=&quot;level1&quot;&gt;&lt;div class=&quot;li&quot;&gt; In the Google code base, approximately 95% of collections weren&amp;#039;t supposed to have any null values in them.&lt;/div&gt;
&lt;/li&gt;
&lt;li class=&quot;level1&quot;&gt;&lt;div class=&quot;li&quot;&gt; It&amp;#039;s rarely obvious what a null return value is supposed to mean.&lt;/div&gt;
&lt;ul&gt;
&lt;li class=&quot;level2&quot;&gt;&lt;div class=&quot;li&quot;&gt; For example, Map.get(key) can return null either because the value in the map is null, or the value is not in the map. &lt;/div&gt;
&lt;/li&gt;
&lt;li class=&quot;level2&quot;&gt;&lt;div class=&quot;li&quot;&gt; Null can mean failure, can mean success, can mean almost anything.  Worst, a null value by itself gives no indication of what it means.&lt;/div&gt;
&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;
Further, I have observed that:
&lt;/p&gt;
&lt;ul&gt;
&lt;li class=&quot;level1&quot;&gt;&lt;div class=&quot;li&quot;&gt; Failing to check for the presence of null sometimes results in the null value propagating through collections, eventually to an unrelated &lt;acronym title=&quot;Application Programming Interface&quot;&gt;API&lt;/acronym&gt; call, which then causes a NullPointerException.  &lt;/div&gt;
&lt;ul&gt;
&lt;li class=&quot;level2&quot;&gt;&lt;div class=&quot;li&quot;&gt; These bugs can be painful to fix because by the time the NullPointerException occurs, the original place where the null check was supposed to happen may no longer even be on the stack.&lt;/div&gt;
&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;li class=&quot;level1&quot;&gt;&lt;div class=&quot;li&quot;&gt; Using a more concrete type, rather than null, makes one&amp;#039;s intensions clearer.&lt;/div&gt;
&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;
All of this (and more) could be summarized as:
&lt;/p&gt;
&lt;blockquote&gt;&lt;div class=&quot;no&quot;&gt;
 “I call [null] my billion-dollar mistake.” - Sir C. A. R. Hoare, inventor of the null reference&lt;/div&gt;&lt;/blockquote&gt;

&lt;/div&gt;
&lt;!-- EDIT1 SECTION &quot;Improvements on Null Safety for Java and Eclipse&quot; [1-1351] --&gt;
&lt;h2 class=&quot;sectionedit2&quot;&gt;&lt;a name=&quot;alternatives_to_null&quot; id=&quot;alternatives_to_null&quot;&gt;Alternatives to null&lt;/a&gt;&lt;/h2&gt;
&lt;div class=&quot;level2&quot;&gt;

&lt;p&gt;
In Java, there appear to be three main alternatives to null references:
&lt;/p&gt;
&lt;ol&gt;
&lt;li class=&quot;level1&quot;&gt;&lt;div class=&quot;li&quot;&gt; Throw an exception; never return an “empty” object&lt;/div&gt;
&lt;/li&gt;
&lt;li class=&quot;level1&quot;&gt;&lt;div class=&quot;li&quot;&gt; The Null Object Pattern&lt;/div&gt;
&lt;/li&gt;
&lt;li class=&quot;level1&quot;&gt;&lt;div class=&quot;li&quot;&gt; Some variation on the Option pattern, sometimes also known as “Maybe” or “Optional”&lt;/div&gt;
&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;
Here is why I think the Java community should consider adopting some form of Option:
&lt;/p&gt;

&lt;p&gt;
1) The Java community has been moving away from throwing checked exceptions, and to this blogger, unchecked exceptions do not seem appropriate as a replacement for something that may not represent an actual failure.
&lt;/p&gt;

&lt;p&gt;
2) I expect you&amp;#039;ve probably heard of the Null Object Pattern before, and it&amp;#039;s been well-described elsewhere.  It&amp;#039;s a good idea when you can use it, but there isn&amp;#039;t always a suitable null object for a given type.
&lt;/p&gt;

&lt;p&gt;
3) A lot of Java programmers who don&amp;#039;t have a background in functional programming haven&amp;#039;t heard of the Option pattern or the Maybe pattern.  I think this is a pity because by itself, Option has nothing to do with functional programming.  It&amp;#039;s just a good object-oriented design pattern that happens to have been discovered by the functional programming people.
&lt;/p&gt;

&lt;p&gt;
So for the balance of this blog, I&amp;#039;d like to describe what Option is and how I&amp;#039;ve adapted Option to Java&amp;#039;s idioms and coding patterns.  I think that with a little consideration, you might grow to really like using this style.
&lt;/p&gt;

&lt;/div&gt;
&lt;!-- EDIT2 SECTION &quot;Alternatives to null&quot; [1352-2736] --&gt;
&lt;h2 class=&quot;sectionedit3&quot;&gt;&lt;a name=&quot;monad_schmonad&quot; id=&quot;monad_schmonad&quot;&gt;Monad, Schmonad...&lt;/a&gt;&lt;/h2&gt;
&lt;div class=&quot;level2&quot;&gt;

&lt;p&gt;
The first thing I&amp;#039;d like to get out of the way is this: Some of you have heard that Option is a Monad.  Maybe you didn&amp;#039;t listen past that or maybe that scared you if you don&amp;#039;t already understand monads.  
&lt;/p&gt;

&lt;p&gt;
If this was true of you, no problem, because we&amp;#039;re going to entirely ignore monads for the moment and demonstrate that our version of Option, which we will call Possible&amp;lt;T&amp;gt;, is plenty useful without them.  (If you&amp;#039;re interested, I&amp;#039;ll blog about monads at some later date.)
&lt;/p&gt;

&lt;/div&gt;
&lt;!-- EDIT3 SECTION &quot;Monad, Schmonad...&quot; [2737-3248] --&gt;
&lt;h2 class=&quot;sectionedit4&quot;&gt;&lt;a name=&quot;a_motivating_example&quot; id=&quot;a_motivating_example&quot;&gt;A motivating example...&lt;/a&gt;&lt;/h2&gt;
&lt;div class=&quot;level2&quot;&gt;

&lt;p&gt;
Back to the problem at hand:  Most of the time when a null object is not possible, the purpose for returning or passing null is to indicate the “lack of an object”.
&lt;/p&gt;

&lt;p&gt;
For the sake of our discussion, let&amp;#039;s pretend that we are designing a data access object (DAO) layer and need to create a 
&lt;/p&gt;
&lt;pre class=&quot;code java&quot;&gt;findPerson&lt;span class=&quot;br0&quot;&gt;&amp;#40;&lt;/span&gt;&lt;span class=&quot;kw4&quot;&gt;int&lt;/span&gt; id&lt;span class=&quot;br0&quot;&gt;&amp;#41;&lt;/span&gt; &lt;/pre&gt;

&lt;p&gt;
&lt;acronym title=&quot;Application Programming Interface&quot;&gt;API&lt;/acronym&gt;.  The question is this:
&lt;/p&gt;
&lt;ul&gt;
&lt;li class=&quot;level1&quot;&gt;&lt;div class=&quot;li&quot;&gt; If we are not going to return null or throw an exception, and if we feel that there is no reasonable “null Person object”, what do we return?&lt;/div&gt;
&lt;/li&gt;
&lt;/ul&gt;

&lt;/div&gt;
&lt;!-- EDIT4 SECTION &quot;A motivating example...&quot; [3249-3792] --&gt;
&lt;h2 class=&quot;sectionedit5&quot;&gt;&lt;a name=&quot;return_a_collection&quot; id=&quot;return_a_collection&quot;&gt;Return a collection?&lt;/a&gt;&lt;/h2&gt;
&lt;div class=&quot;level2&quot;&gt;

&lt;p&gt;
Rather than return a Person object directly, one option might be to return a collection of Person objects instead, where the collection always has either 0 or 1 element(s).  Something like the following could work:
&lt;/p&gt;
&lt;pre class=&quot;code java&quot;&gt;&lt;span class=&quot;kw1&quot;&gt;public&lt;/span&gt; List&lt;span class=&quot;sy0&quot;&gt;&amp;lt;&lt;/span&gt;Person&lt;span class=&quot;sy0&quot;&gt;&amp;gt;&lt;/span&gt; findPerson&lt;span class=&quot;br0&quot;&gt;&amp;#40;&lt;/span&gt;&lt;span class=&quot;kw4&quot;&gt;int&lt;/span&gt; id&lt;span class=&quot;br0&quot;&gt;&amp;#41;&lt;/span&gt; &lt;span class=&quot;br0&quot;&gt;&amp;#123;&lt;/span&gt; ... &lt;span class=&quot;br0&quot;&gt;&amp;#125;&lt;/span&gt;
&amp;nbsp;
&amp;nbsp;
List&lt;span class=&quot;sy0&quot;&gt;&amp;lt;&lt;/span&gt;Person&lt;span class=&quot;sy0&quot;&gt;&amp;gt;&lt;/span&gt; foundPerson &lt;span class=&quot;sy0&quot;&gt;=&lt;/span&gt; findPerson&lt;span class=&quot;br0&quot;&gt;&amp;#40;&lt;/span&gt;&lt;span class=&quot;nu0&quot;&gt;42&lt;/span&gt;&lt;span class=&quot;br0&quot;&gt;&amp;#41;&lt;/span&gt;&lt;span class=&quot;sy0&quot;&gt;;&lt;/span&gt;
&lt;span class=&quot;kw1&quot;&gt;if&lt;/span&gt; &lt;span class=&quot;br0&quot;&gt;&amp;#40;&lt;/span&gt;&lt;span class=&quot;sy0&quot;&gt;!&lt;/span&gt;foundPerson.&lt;span class=&quot;me1&quot;&gt;isEmpty&lt;/span&gt;&lt;span class=&quot;br0&quot;&gt;&amp;#40;&lt;/span&gt;&lt;span class=&quot;br0&quot;&gt;&amp;#41;&lt;/span&gt;&lt;span class=&quot;br0&quot;&gt;&amp;#41;&lt;/span&gt; &lt;span class=&quot;br0&quot;&gt;&amp;#123;&lt;/span&gt;
   Person person &lt;span class=&quot;sy0&quot;&gt;=&lt;/span&gt; foundPerson.&lt;span class=&quot;me1&quot;&gt;get&lt;/span&gt;&lt;span class=&quot;br0&quot;&gt;&amp;#40;&lt;/span&gt;&lt;span class=&quot;nu0&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;br0&quot;&gt;&amp;#41;&lt;/span&gt;&lt;span class=&quot;sy0&quot;&gt;;&lt;/span&gt;
&lt;span class=&quot;br0&quot;&gt;&amp;#125;&lt;/span&gt; &lt;span class=&quot;kw1&quot;&gt;else&lt;/span&gt; &lt;span class=&quot;br0&quot;&gt;&amp;#123;&lt;/span&gt;
   &lt;span class=&quot;co1&quot;&gt;// handle the case where we didn't find the person if needed&lt;/span&gt;
&lt;span class=&quot;br0&quot;&gt;&amp;#125;&lt;/span&gt;&lt;/pre&gt;

&lt;p&gt;
Further, since the result is a Collection, if we do not care about the case where we didn&amp;#039;t find a Person, we can &amp;#039;foreach&amp;#039; over the results:
&lt;/p&gt;
&lt;pre class=&quot;code java&quot;&gt;&lt;span class=&quot;kw1&quot;&gt;public&lt;/span&gt; List&lt;span class=&quot;sy0&quot;&gt;&amp;lt;&lt;/span&gt;Person&lt;span class=&quot;sy0&quot;&gt;&amp;gt;&lt;/span&gt; findPerson&lt;span class=&quot;br0&quot;&gt;&amp;#40;&lt;/span&gt;&lt;span class=&quot;kw4&quot;&gt;int&lt;/span&gt; id&lt;span class=&quot;br0&quot;&gt;&amp;#41;&lt;/span&gt; &lt;span class=&quot;br0&quot;&gt;&amp;#123;&lt;/span&gt; ... &lt;span class=&quot;br0&quot;&gt;&amp;#125;&lt;/span&gt;
&amp;nbsp;
&amp;nbsp;
List&lt;span class=&quot;sy0&quot;&gt;&amp;lt;&lt;/span&gt;Person&lt;span class=&quot;sy0&quot;&gt;&amp;gt;&lt;/span&gt; foundPerson &lt;span class=&quot;sy0&quot;&gt;=&lt;/span&gt; findPerson&lt;span class=&quot;br0&quot;&gt;&amp;#40;&lt;/span&gt;&lt;span class=&quot;nu0&quot;&gt;42&lt;/span&gt;&lt;span class=&quot;br0&quot;&gt;&amp;#41;&lt;/span&gt;&lt;span class=&quot;sy0&quot;&gt;;&lt;/span&gt;
&lt;span class=&quot;kw1&quot;&gt;for&lt;/span&gt; &lt;span class=&quot;br0&quot;&gt;&amp;#40;&lt;/span&gt;Person person &lt;span class=&quot;sy0&quot;&gt;:&lt;/span&gt; foundPerson&lt;span class=&quot;br0&quot;&gt;&amp;#41;&lt;/span&gt; &lt;span class=&quot;br0&quot;&gt;&amp;#123;&lt;/span&gt;
   &lt;span class=&quot;co1&quot;&gt;// ...&lt;/span&gt;
&lt;span class=&quot;br0&quot;&gt;&amp;#125;&lt;/span&gt;&lt;/pre&gt;

&lt;/div&gt;
&lt;!-- EDIT5 SECTION &quot;Return a collection?&quot; [3793-4603] --&gt;
&lt;h3 class=&quot;sectionedit6&quot;&gt;&lt;a name=&quot;benefits_and_drawbacks_of_using_list_t&quot; id=&quot;benefits_and_drawbacks_of_using_list_t&quot;&gt;Benefits and drawbacks of using List&amp;lt;T&amp;gt;&lt;/a&gt;&lt;/h3&gt;
&lt;div class=&quot;level3&quot;&gt;

&lt;p&gt;
The benefits of using List&amp;lt;T&amp;gt; to contain 0 or 1 results include:
&lt;/p&gt;
&lt;ul&gt;
&lt;li class=&quot;level1&quot;&gt;&lt;div class=&quot;li&quot;&gt; It&amp;#039;s clear from the method signature that the &lt;acronym title=&quot;Application Programming Interface&quot;&gt;API&lt;/acronym&gt; might return a List of 0 length.&lt;/div&gt;
&lt;/li&gt;
&lt;li class=&quot;level1&quot;&gt;&lt;div class=&quot;li&quot;&gt; Testing for the presence of a value is simple, straightforward, and familiar.  After all, we&amp;#039;re just returning and evaluating a collection.&lt;/div&gt;
&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;
The drawbacks include:
&lt;/p&gt;
&lt;ul&gt;
&lt;li class=&quot;level1&quot;&gt;&lt;div class=&quot;li&quot;&gt; It&amp;#039;s not clear from the method signature that the &lt;acronym title=&quot;Application Programming Interface&quot;&gt;API&lt;/acronym&gt; will never return a foundPerson.size() &amp;gt; 1&lt;/div&gt;
&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;
How might we avoid this drawback?  
&lt;/p&gt;

&lt;p&gt;
Easy: Let&amp;#039;s build a Java collection type that can only contain 0 or 1 elements.  Additionally, we could add a few special-purpose methods for handling the kinds of cases for which we normally would use null.
&lt;/p&gt;

&lt;/div&gt;
&lt;!-- EDIT6 SECTION &quot;Benefits and drawbacks of using List&lt;T&gt;&quot; [4604-5324] --&gt;
&lt;h2 class=&quot;sectionedit7&quot;&gt;&lt;a name=&quot;implementing_option_t_for_eclipse&quot; id=&quot;implementing_option_t_for_eclipse&quot;&gt;Implementing Option&amp;lt;T&amp;gt; for Eclipse&lt;/a&gt;&lt;/h2&gt;
&lt;div class=&quot;level2&quot;&gt;

&lt;p&gt;
Traditionally, Option or Maybe is implemented using pattern matching (and in Scala, using case classes).  In Java, this translates to something like the following:
&lt;/p&gt;
&lt;pre class=&quot;code java&quot;&gt;&lt;span class=&quot;kw1&quot;&gt;public&lt;/span&gt; &lt;span class=&quot;kw1&quot;&gt;interface&lt;/span&gt; Option&lt;span class=&quot;sy0&quot;&gt;&amp;lt;&lt;/span&gt;T&lt;span class=&quot;sy0&quot;&gt;&amp;gt;&lt;/span&gt; &lt;span class=&quot;br0&quot;&gt;&amp;#123;&lt;/span&gt;
   &lt;span class=&quot;kw4&quot;&gt;boolean&lt;/span&gt; isEmpty&lt;span class=&quot;br0&quot;&gt;&amp;#40;&lt;/span&gt;&lt;span class=&quot;br0&quot;&gt;&amp;#41;&lt;/span&gt;&lt;span class=&quot;sy0&quot;&gt;;&lt;/span&gt;
   T get&lt;span class=&quot;br0&quot;&gt;&amp;#40;&lt;/span&gt;&lt;span class=&quot;br0&quot;&gt;&amp;#41;&lt;/span&gt;&lt;span class=&quot;sy0&quot;&gt;;&lt;/span&gt;
&lt;span class=&quot;br0&quot;&gt;&amp;#125;&lt;/span&gt;
&amp;nbsp;
&lt;span class=&quot;kw1&quot;&gt;public&lt;/span&gt; &lt;span class=&quot;kw1&quot;&gt;class&lt;/span&gt; Some&lt;span class=&quot;sy0&quot;&gt;&amp;lt;&lt;/span&gt;T&lt;span class=&quot;sy0&quot;&gt;&amp;gt;&lt;/span&gt; &lt;span class=&quot;kw1&quot;&gt;implements&lt;/span&gt; Option&lt;span class=&quot;sy0&quot;&gt;&amp;lt;&lt;/span&gt;T&lt;span class=&quot;sy0&quot;&gt;&amp;gt;&lt;/span&gt; &lt;span class=&quot;br0&quot;&gt;&amp;#123;&lt;/span&gt; &lt;span class=&quot;co1&quot;&gt;// ...&lt;/span&gt;
   &lt;span class=&quot;kw1&quot;&gt;public&lt;/span&gt; &lt;span class=&quot;kw1&quot;&gt;static&lt;/span&gt; &lt;span class=&quot;sy0&quot;&gt;&amp;lt;&lt;/span&gt;T&lt;span class=&quot;sy0&quot;&gt;&amp;gt;&lt;/span&gt; Some&lt;span class=&quot;sy0&quot;&gt;&amp;lt;&lt;/span&gt;T&lt;span class=&quot;sy0&quot;&gt;&amp;gt;&lt;/span&gt; some&lt;span class=&quot;br0&quot;&gt;&amp;#40;&lt;/span&gt;T value&lt;span class=&quot;br0&quot;&gt;&amp;#41;&lt;/span&gt; &lt;span class=&quot;br0&quot;&gt;&amp;#123;&lt;/span&gt; &lt;span class=&quot;kw1&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;kw1&quot;&gt;new&lt;/span&gt; Some&lt;span class=&quot;sy0&quot;&gt;&amp;lt;&lt;/span&gt;T&lt;span class=&quot;sy0&quot;&gt;&amp;gt;&lt;/span&gt;&lt;span class=&quot;br0&quot;&gt;&amp;#40;&lt;/span&gt;value&lt;span class=&quot;br0&quot;&gt;&amp;#41;&lt;/span&gt;&lt;span class=&quot;sy0&quot;&gt;;&lt;/span&gt; &lt;span class=&quot;br0&quot;&gt;&amp;#125;&lt;/span&gt; 
   &lt;span class=&quot;co1&quot;&gt;// ...&lt;/span&gt;
&lt;span class=&quot;br0&quot;&gt;&amp;#125;&lt;/span&gt;
&amp;nbsp;
&lt;span class=&quot;kw1&quot;&gt;public&lt;/span&gt; &lt;span class=&quot;kw1&quot;&gt;class&lt;/span&gt; None&lt;span class=&quot;sy0&quot;&gt;&amp;lt;&lt;/span&gt;T&lt;span class=&quot;sy0&quot;&gt;&amp;gt;&lt;/span&gt; &lt;span class=&quot;kw1&quot;&gt;implements&lt;/span&gt; Option&lt;span class=&quot;sy0&quot;&gt;&amp;lt;&lt;/span&gt;T&lt;span class=&quot;sy0&quot;&gt;&amp;gt;&lt;/span&gt; &lt;span class=&quot;br0&quot;&gt;&amp;#123;&lt;/span&gt; &lt;span class=&quot;co1&quot;&gt;// ... &lt;/span&gt;
   &lt;span class=&quot;kw1&quot;&gt;public&lt;/span&gt; &lt;span class=&quot;kw1&quot;&gt;static&lt;/span&gt; &lt;span class=&quot;sy0&quot;&gt;&amp;lt;&lt;/span&gt;T&lt;span class=&quot;sy0&quot;&gt;&amp;gt;&lt;/span&gt; None&lt;span class=&quot;sy0&quot;&gt;&amp;lt;&lt;/span&gt;T&lt;span class=&quot;sy0&quot;&gt;&amp;gt;&lt;/span&gt; none&lt;span class=&quot;br0&quot;&gt;&amp;#40;&lt;/span&gt;&lt;span class=&quot;br0&quot;&gt;&amp;#41;&lt;/span&gt; &lt;span class=&quot;br0&quot;&gt;&amp;#123;&lt;/span&gt; &lt;span class=&quot;kw1&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;kw1&quot;&gt;new&lt;/span&gt; None&lt;span class=&quot;sy0&quot;&gt;&amp;lt;&lt;/span&gt;T&lt;span class=&quot;sy0&quot;&gt;&amp;gt;&lt;/span&gt;&lt;span class=&quot;br0&quot;&gt;&amp;#40;&lt;/span&gt;&lt;span class=&quot;br0&quot;&gt;&amp;#41;&lt;/span&gt;&lt;span class=&quot;sy0&quot;&gt;;&lt;/span&gt; &lt;span class=&quot;br0&quot;&gt;&amp;#125;&lt;/span&gt;
   &lt;span class=&quot;co1&quot;&gt;// ...&lt;/span&gt;
&lt;span class=&quot;br0&quot;&gt;&amp;#125;&lt;/span&gt;
&amp;nbsp;
&amp;nbsp;
&lt;span class=&quot;kw1&quot;&gt;public&lt;/span&gt; Option&lt;span class=&quot;sy0&quot;&gt;&amp;lt;&lt;/span&gt;Person&lt;span class=&quot;sy0&quot;&gt;&amp;gt;&lt;/span&gt; findPerson&lt;span class=&quot;br0&quot;&gt;&amp;#40;&lt;/span&gt;&lt;span class=&quot;kw4&quot;&gt;int&lt;/span&gt; id&lt;span class=&quot;br0&quot;&gt;&amp;#41;&lt;/span&gt; &lt;span class=&quot;br0&quot;&gt;&amp;#123;&lt;/span&gt;
   &lt;span class=&quot;kw4&quot;&gt;boolean&lt;/span&gt; foundPerson &lt;span class=&quot;sy0&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;kw2&quot;&gt;false&lt;/span&gt;&lt;span class=&quot;sy0&quot;&gt;;&lt;/span&gt;
   &lt;span class=&quot;co1&quot;&gt;// look up person by ID&lt;/span&gt;
   &lt;span class=&quot;kw1&quot;&gt;if&lt;/span&gt; &lt;span class=&quot;br0&quot;&gt;&amp;#40;&lt;/span&gt;foundPerson&lt;span class=&quot;br0&quot;&gt;&amp;#41;&lt;/span&gt; &lt;span class=&quot;br0&quot;&gt;&amp;#123;&lt;/span&gt;
      Person person &lt;span class=&quot;sy0&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;kw1&quot;&gt;new&lt;/span&gt; Person&lt;span class=&quot;br0&quot;&gt;&amp;#40;&lt;/span&gt;parameters&lt;span class=&quot;br0&quot;&gt;&amp;#41;&lt;/span&gt;&lt;span class=&quot;sy0&quot;&gt;;&lt;/span&gt;
      &lt;span class=&quot;kw1&quot;&gt;return&lt;/span&gt; some&lt;span class=&quot;br0&quot;&gt;&amp;#40;&lt;/span&gt;person&lt;span class=&quot;br0&quot;&gt;&amp;#41;&lt;/span&gt;&lt;span class=&quot;sy0&quot;&gt;;&lt;/span&gt;
   &lt;span class=&quot;br0&quot;&gt;&amp;#125;&lt;/span&gt; &lt;span class=&quot;kw1&quot;&gt;else&lt;/span&gt; &lt;span class=&quot;br0&quot;&gt;&amp;#123;&lt;/span&gt;
      &lt;span class=&quot;kw1&quot;&gt;return&lt;/span&gt; none&lt;span class=&quot;br0&quot;&gt;&amp;#40;&lt;/span&gt;&lt;span class=&quot;br0&quot;&gt;&amp;#41;&lt;/span&gt;&lt;span class=&quot;sy0&quot;&gt;;&lt;/span&gt;
   &lt;span class=&quot;br0&quot;&gt;&amp;#125;&lt;/span&gt;
&lt;span class=&quot;br0&quot;&gt;&amp;#125;&lt;/span&gt;
&amp;nbsp;
&amp;nbsp;
&lt;span class=&quot;kw1&quot;&gt;public&lt;/span&gt; &lt;span class=&quot;kw4&quot;&gt;void&lt;/span&gt; whenWeNeedToFetchThePerson&lt;span class=&quot;br0&quot;&gt;&amp;#40;&lt;/span&gt;&lt;span class=&quot;br0&quot;&gt;&amp;#41;&lt;/span&gt; &lt;span class=&quot;br0&quot;&gt;&amp;#123;&lt;/span&gt;
   Option&lt;span class=&quot;sy0&quot;&gt;&amp;lt;&lt;/span&gt;Person&lt;span class=&quot;sy0&quot;&gt;&amp;gt;&lt;/span&gt; optionalPerson &lt;span class=&quot;sy0&quot;&gt;=&lt;/span&gt; findPerson&lt;span class=&quot;br0&quot;&gt;&amp;#40;&lt;/span&gt;&lt;span class=&quot;nu0&quot;&gt;42&lt;/span&gt;&lt;span class=&quot;br0&quot;&gt;&amp;#41;&lt;/span&gt;&lt;span class=&quot;sy0&quot;&gt;;&lt;/span&gt;
   &lt;span class=&quot;kw1&quot;&gt;if&lt;/span&gt; &lt;span class=&quot;br0&quot;&gt;&amp;#40;&lt;/span&gt;&lt;span class=&quot;sy0&quot;&gt;!&lt;/span&gt;optionalPerson.&lt;span class=&quot;me1&quot;&gt;isEmpty&lt;/span&gt;&lt;span class=&quot;br0&quot;&gt;&amp;#40;&lt;/span&gt;&lt;span class=&quot;br0&quot;&gt;&amp;#41;&lt;/span&gt;&lt;span class=&quot;br0&quot;&gt;&amp;#41;&lt;/span&gt; &lt;span class=&quot;br0&quot;&gt;&amp;#123;&lt;/span&gt;
      Person person &lt;span class=&quot;sy0&quot;&gt;=&lt;/span&gt; optionalPerson.&lt;span class=&quot;me1&quot;&gt;get&lt;/span&gt;&lt;span class=&quot;br0&quot;&gt;&amp;#40;&lt;/span&gt;&lt;span class=&quot;br0&quot;&gt;&amp;#41;&lt;/span&gt;&lt;span class=&quot;sy0&quot;&gt;;&lt;/span&gt;
      &lt;span class=&quot;co1&quot;&gt;// process the case where we found the person&lt;/span&gt;
   &lt;span class=&quot;br0&quot;&gt;&amp;#125;&lt;/span&gt; &lt;span class=&quot;kw1&quot;&gt;else&lt;/span&gt; &lt;span class=&quot;br0&quot;&gt;&amp;#123;&lt;/span&gt;
      &lt;span class=&quot;co1&quot;&gt;// process the case where we did not find the person&lt;/span&gt;
   &lt;span class=&quot;br0&quot;&gt;&amp;#125;&lt;/span&gt;
&lt;span class=&quot;br0&quot;&gt;&amp;#125;&lt;/span&gt;&lt;/pre&gt;

&lt;p&gt;
I have used an Option implementation similar to this for approximately the last year-and-a-half.  My observation in team usage is that programmers who are familiar with pattern matching and case classes in other languages are comfortable enough with it, but that programmers who are only familiar with Java find the static #some and #none factories counter-intuitive and problematic.
&lt;/p&gt;

&lt;p&gt;
In case you like this style, some of the E4 code that was never adopted, and also some of the P2 Director integration tests, have Option implementations after this style:
&lt;/p&gt;
&lt;ul&gt;
&lt;li class=&quot;level1&quot;&gt;&lt;div class=&quot;li&quot;&gt; &lt;a href=&quot;http://git.eclipse.org/c/e4/org.eclipse.e4.utils.git/tree/bundles/org.eclipse.e4.core.functionalprog/src/org/eclipse/e4/core/functionalprog/optionmonad/Option.java&quot; class=&quot;urlextern&quot; title=&quot;http://git.eclipse.org/c/e4/org.eclipse.e4.utils.git/tree/bundles/org.eclipse.e4.core.functionalprog/src/org/eclipse/e4/core/functionalprog/optionmonad/Option.java&quot;  rel=&quot;nofollow&quot;&gt;Originally we contributed this to E4&lt;/a&gt;.&lt;/div&gt;
&lt;/li&gt;
&lt;li class=&quot;level1&quot;&gt;&lt;div class=&quot;li&quot;&gt; Again later, as we assisted the P2 Director effort, we used an improved version in our &lt;a href=&quot;https://github.com/pieceoftheloaf/SynchronizeOperation/tree/master/webapps/root/test.app/src/test/app/optionmonad&quot; class=&quot;urlextern&quot; title=&quot;https://github.com/pieceoftheloaf/SynchronizeOperation/tree/master/webapps/root/test.app/src/test/app/optionmonad&quot;  rel=&quot;nofollow&quot;&gt;P2 Director Integration Test&lt;/a&gt;.&lt;/div&gt;
&lt;/li&gt;
&lt;/ul&gt;

&lt;/div&gt;
&lt;!-- EDIT7 SECTION &quot;Implementing Option&lt;T&gt; for Eclipse&quot; [5325-7441] --&gt;
&lt;h2 class=&quot;sectionedit8&quot;&gt;&lt;a name=&quot;improving_our_option_implementation&quot; id=&quot;improving_our_option_implementation&quot;&gt;Improving our Option implementation&lt;/a&gt;&lt;/h2&gt;
&lt;div class=&quot;level2&quot;&gt;

&lt;p&gt;
After time with these implementations, the question returned to my mind, “Can I find a way to implement Option&amp;lt;T&amp;gt; that is more idiomatic to Java?”
&lt;/p&gt;

&lt;p&gt;
And the thought that occurred to me was, “What if I implemented something like Option&amp;lt;T&amp;gt;, but as a standard Java collection containing 0 or 1 element, nothing more.”
&lt;/p&gt;

&lt;p&gt;
I&amp;#039;ve now done that.  Since the implementation isn&amp;#039;t really derived from Option any longer, I&amp;#039;ve chosen to call the new version Possible&amp;lt;T&amp;gt;.  (That name also makes the factory methods read more nicely, &lt;acronym title=&quot;In my opinion&quot;&gt;IMO&lt;/acronym&gt;.)
&lt;/p&gt;

&lt;p&gt;
Here is the previous example, rewritten to use Possible&amp;lt;T&amp;gt;:
&lt;/p&gt;
&lt;pre class=&quot;code java&quot;&gt;&lt;span class=&quot;kw1&quot;&gt;public&lt;/span&gt; &lt;span class=&quot;kw1&quot;&gt;abstract&lt;/span&gt; &lt;span class=&quot;kw1&quot;&gt;class&lt;/span&gt; Possible&lt;span class=&quot;sy0&quot;&gt;&amp;lt;&lt;/span&gt;T&lt;span class=&quot;sy0&quot;&gt;&amp;gt;&lt;/span&gt; &lt;span class=&quot;kw1&quot;&gt;implements&lt;/span&gt; Iterable&lt;span class=&quot;sy0&quot;&gt;&amp;lt;&lt;/span&gt;T&lt;span class=&quot;sy0&quot;&gt;&amp;gt;&lt;/span&gt; &lt;span class=&quot;br0&quot;&gt;&amp;#123;&lt;/span&gt;
   &lt;span class=&quot;kw1&quot;&gt;public&lt;/span&gt; &lt;span class=&quot;kw1&quot;&gt;static&lt;/span&gt; &lt;span class=&quot;sy0&quot;&gt;&amp;lt;&lt;/span&gt;T&lt;span class=&quot;sy0&quot;&gt;&amp;gt;&lt;/span&gt; Possible&lt;span class=&quot;sy0&quot;&gt;&amp;lt;&lt;/span&gt;T&lt;span class=&quot;sy0&quot;&gt;&amp;gt;&lt;/span&gt; value&lt;span class=&quot;br0&quot;&gt;&amp;#40;&lt;/span&gt;T value&lt;span class=&quot;br0&quot;&gt;&amp;#41;&lt;/span&gt; &lt;span class=&quot;br0&quot;&gt;&amp;#123;&lt;/span&gt; ... &lt;span class=&quot;br0&quot;&gt;&amp;#125;&lt;/span&gt;
   &lt;span class=&quot;kw1&quot;&gt;public&lt;/span&gt; &lt;span class=&quot;kw1&quot;&gt;static&lt;/span&gt; &lt;span class=&quot;sy0&quot;&gt;&amp;lt;&lt;/span&gt;T&lt;span class=&quot;sy0&quot;&gt;&amp;gt;&lt;/span&gt; Possible&lt;span class=&quot;sy0&quot;&gt;&amp;lt;&lt;/span&gt;T&lt;span class=&quot;sy0&quot;&gt;&amp;gt;&lt;/span&gt; value&lt;span class=&quot;br0&quot;&gt;&amp;#40;&lt;/span&gt;T value, IStatus status&lt;span class=&quot;br0&quot;&gt;&amp;#41;&lt;/span&gt; &lt;span class=&quot;br0&quot;&gt;&amp;#123;&lt;/span&gt; ... &lt;span class=&quot;br0&quot;&gt;&amp;#125;&lt;/span&gt;
   &lt;span class=&quot;kw1&quot;&gt;public&lt;/span&gt; &lt;span class=&quot;kw1&quot;&gt;static&lt;/span&gt; &lt;span class=&quot;sy0&quot;&gt;&amp;lt;&lt;/span&gt;T&lt;span class=&quot;sy0&quot;&gt;&amp;gt;&lt;/span&gt; Possible&lt;span class=&quot;sy0&quot;&gt;&amp;lt;&lt;/span&gt;T&lt;span class=&quot;sy0&quot;&gt;&amp;gt;&lt;/span&gt; emptyValue&lt;span class=&quot;br0&quot;&gt;&amp;#40;&lt;/span&gt;&lt;span class=&quot;br0&quot;&gt;&amp;#41;&lt;/span&gt; &lt;span class=&quot;br0&quot;&gt;&amp;#123;&lt;/span&gt; ... &lt;span class=&quot;br0&quot;&gt;&amp;#125;&lt;/span&gt;
   &lt;span class=&quot;kw1&quot;&gt;public&lt;/span&gt; &lt;span class=&quot;kw1&quot;&gt;static&lt;/span&gt; &lt;span class=&quot;sy0&quot;&gt;&amp;lt;&lt;/span&gt;T&lt;span class=&quot;sy0&quot;&gt;&amp;gt;&lt;/span&gt; Possible&lt;span class=&quot;sy0&quot;&gt;&amp;lt;&lt;/span&gt;T&lt;span class=&quot;sy0&quot;&gt;&amp;gt;&lt;/span&gt; emptyValue&lt;span class=&quot;br0&quot;&gt;&amp;#40;&lt;/span&gt;IStatus reason&lt;span class=&quot;br0&quot;&gt;&amp;#41;&lt;/span&gt; &lt;span class=&quot;br0&quot;&gt;&amp;#123;&lt;/span&gt; ... &lt;span class=&quot;br0&quot;&gt;&amp;#125;&lt;/span&gt;
&amp;nbsp;
   &lt;span class=&quot;kw1&quot;&gt;abstract&lt;/span&gt; &lt;span class=&quot;kw4&quot;&gt;boolean&lt;/span&gt; contains&lt;span class=&quot;br0&quot;&gt;&amp;#40;&lt;/span&gt;&lt;a href=&quot;http://www.google.com/search?hl=en&amp;amp;q=allinurl%3Aobject+java.sun.com&amp;amp;btnI=I%27m%20Feeling%20Lucky&quot;&gt;&lt;span class=&quot;kw3&quot;&gt;Object&lt;/span&gt;&lt;/a&gt; o&lt;span class=&quot;br0&quot;&gt;&amp;#41;&lt;/span&gt;&lt;span class=&quot;sy0&quot;&gt;;&lt;/span&gt;
   &lt;span class=&quot;kw1&quot;&gt;abstract&lt;/span&gt; &lt;span class=&quot;kw4&quot;&gt;boolean&lt;/span&gt; isEmpty&lt;span class=&quot;br0&quot;&gt;&amp;#40;&lt;/span&gt;&lt;span class=&quot;br0&quot;&gt;&amp;#41;&lt;/span&gt;&lt;span class=&quot;sy0&quot;&gt;;&lt;/span&gt;
   &lt;span class=&quot;kw1&quot;&gt;abstract&lt;/span&gt; &lt;span class=&quot;kw4&quot;&gt;boolean&lt;/span&gt; hasValue&lt;span class=&quot;br0&quot;&gt;&amp;#40;&lt;/span&gt;&lt;span class=&quot;br0&quot;&gt;&amp;#41;&lt;/span&gt;&lt;span class=&quot;sy0&quot;&gt;;&lt;/span&gt;
   &lt;span class=&quot;kw1&quot;&gt;abstract&lt;/span&gt; &lt;span class=&quot;kw4&quot;&gt;int&lt;/span&gt; size&lt;span class=&quot;br0&quot;&gt;&amp;#40;&lt;/span&gt;&lt;span class=&quot;br0&quot;&gt;&amp;#41;&lt;/span&gt;&lt;span class=&quot;sy0&quot;&gt;;&lt;/span&gt;
   &lt;span class=&quot;kw1&quot;&gt;abstract&lt;/span&gt; &lt;a href=&quot;http://www.google.com/search?hl=en&amp;amp;q=allinurl%3Aobject+java.sun.com&amp;amp;btnI=I%27m%20Feeling%20Lucky&quot;&gt;&lt;span class=&quot;kw3&quot;&gt;Object&lt;/span&gt;&lt;/a&gt;&lt;span class=&quot;br0&quot;&gt;&amp;#91;&lt;/span&gt;&lt;span class=&quot;br0&quot;&gt;&amp;#93;&lt;/span&gt; toArray&lt;span class=&quot;br0&quot;&gt;&amp;#40;&lt;/span&gt;&lt;span class=&quot;br0&quot;&gt;&amp;#41;&lt;/span&gt;&lt;span class=&quot;sy0&quot;&gt;;&lt;/span&gt;
   &lt;span class=&quot;kw1&quot;&gt;abstract&lt;/span&gt; &lt;span class=&quot;sy0&quot;&gt;&amp;lt;&lt;/span&gt;A&lt;span class=&quot;sy0&quot;&gt;&amp;gt;&lt;/span&gt; A&lt;span class=&quot;br0&quot;&gt;&amp;#91;&lt;/span&gt;&lt;span class=&quot;br0&quot;&gt;&amp;#93;&lt;/span&gt; toArray&lt;span class=&quot;br0&quot;&gt;&amp;#40;&lt;/span&gt;A&lt;span class=&quot;br0&quot;&gt;&amp;#91;&lt;/span&gt;&lt;span class=&quot;br0&quot;&gt;&amp;#93;&lt;/span&gt; a&lt;span class=&quot;br0&quot;&gt;&amp;#41;&lt;/span&gt;&lt;span class=&quot;sy0&quot;&gt;;&lt;/span&gt;
   &lt;span class=&quot;kw1&quot;&gt;abstract&lt;/span&gt; T get&lt;span class=&quot;br0&quot;&gt;&amp;#40;&lt;/span&gt;&lt;span class=&quot;br0&quot;&gt;&amp;#41;&lt;/span&gt;&lt;span class=&quot;sy0&quot;&gt;;&lt;/span&gt;
   &lt;span class=&quot;kw1&quot;&gt;abstract&lt;/span&gt; T getOrSubstitute&lt;span class=&quot;br0&quot;&gt;&amp;#40;&lt;/span&gt;T defaultValue&lt;span class=&quot;br0&quot;&gt;&amp;#41;&lt;/span&gt;&lt;span class=&quot;sy0&quot;&gt;;&lt;/span&gt;
   &lt;span class=&quot;kw1&quot;&gt;abstract&lt;/span&gt; &lt;span class=&quot;sy0&quot;&gt;&amp;lt;&lt;/span&gt;E &lt;span class=&quot;kw1&quot;&gt;extends&lt;/span&gt; Throwable&lt;span class=&quot;sy0&quot;&gt;&amp;gt;&lt;/span&gt; T getOrThrow&lt;span class=&quot;br0&quot;&gt;&amp;#40;&lt;/span&gt;E exception&lt;span class=&quot;br0&quot;&gt;&amp;#41;&lt;/span&gt; &lt;span class=&quot;kw1&quot;&gt;throws&lt;/span&gt; E&lt;span class=&quot;sy0&quot;&gt;;&lt;/span&gt;
   &lt;span class=&quot;kw1&quot;&gt;abstract&lt;/span&gt; IStatus getStatus&lt;span class=&quot;br0&quot;&gt;&amp;#40;&lt;/span&gt;&lt;span class=&quot;br0&quot;&gt;&amp;#41;&lt;/span&gt;&lt;span class=&quot;sy0&quot;&gt;;&lt;/span&gt;
&lt;span class=&quot;br0&quot;&gt;&amp;#125;&lt;/span&gt;
&amp;nbsp;
&lt;span class=&quot;kw1&quot;&gt;public&lt;/span&gt; Option&lt;span class=&quot;sy0&quot;&gt;&amp;lt;&lt;/span&gt;Person&lt;span class=&quot;sy0&quot;&gt;&amp;gt;&lt;/span&gt; findPerson&lt;span class=&quot;br0&quot;&gt;&amp;#40;&lt;/span&gt;&lt;span class=&quot;kw4&quot;&gt;int&lt;/span&gt; id&lt;span class=&quot;br0&quot;&gt;&amp;#41;&lt;/span&gt; &lt;span class=&quot;br0&quot;&gt;&amp;#123;&lt;/span&gt;
   &lt;span class=&quot;kw4&quot;&gt;boolean&lt;/span&gt; foundPerson &lt;span class=&quot;sy0&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;kw2&quot;&gt;false&lt;/span&gt;&lt;span class=&quot;sy0&quot;&gt;;&lt;/span&gt;
   &lt;span class=&quot;co1&quot;&gt;// look up person by ID&lt;/span&gt;
   &lt;span class=&quot;kw1&quot;&gt;if&lt;/span&gt; &lt;span class=&quot;br0&quot;&gt;&amp;#40;&lt;/span&gt;foundPerson&lt;span class=&quot;br0&quot;&gt;&amp;#41;&lt;/span&gt; &lt;span class=&quot;br0&quot;&gt;&amp;#123;&lt;/span&gt;
      Person person &lt;span class=&quot;sy0&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;kw1&quot;&gt;new&lt;/span&gt; Person&lt;span class=&quot;br0&quot;&gt;&amp;#40;&lt;/span&gt;parameters&lt;span class=&quot;br0&quot;&gt;&amp;#41;&lt;/span&gt;&lt;span class=&quot;sy0&quot;&gt;;&lt;/span&gt;
      &lt;span class=&quot;kw1&quot;&gt;return&lt;/span&gt; Possible.&lt;span class=&quot;me1&quot;&gt;value&lt;/span&gt;&lt;span class=&quot;br0&quot;&gt;&amp;#40;&lt;/span&gt;person&lt;span class=&quot;br0&quot;&gt;&amp;#41;&lt;/span&gt;&lt;span class=&quot;sy0&quot;&gt;;&lt;/span&gt;
   &lt;span class=&quot;br0&quot;&gt;&amp;#125;&lt;/span&gt; &lt;span class=&quot;kw1&quot;&gt;else&lt;/span&gt; &lt;span class=&quot;br0&quot;&gt;&amp;#123;&lt;/span&gt;
      &lt;span class=&quot;kw1&quot;&gt;return&lt;/span&gt; Possible.&lt;span class=&quot;me1&quot;&gt;emptyValue&lt;/span&gt;&lt;span class=&quot;br0&quot;&gt;&amp;#40;&lt;/span&gt;&lt;span class=&quot;br0&quot;&gt;&amp;#41;&lt;/span&gt;&lt;span class=&quot;sy0&quot;&gt;;&lt;/span&gt;
   &lt;span class=&quot;br0&quot;&gt;&amp;#125;&lt;/span&gt;
&lt;span class=&quot;br0&quot;&gt;&amp;#125;&lt;/span&gt;
&amp;nbsp;
&lt;span class=&quot;kw1&quot;&gt;public&lt;/span&gt; &lt;span class=&quot;kw4&quot;&gt;void&lt;/span&gt; whenWeNeedToFetchThePerson&lt;span class=&quot;br0&quot;&gt;&amp;#40;&lt;/span&gt;&lt;span class=&quot;br0&quot;&gt;&amp;#41;&lt;/span&gt; &lt;span class=&quot;br0&quot;&gt;&amp;#123;&lt;/span&gt;
   Possible&lt;span class=&quot;sy0&quot;&gt;&amp;lt;&lt;/span&gt;Person&lt;span class=&quot;sy0&quot;&gt;&amp;gt;&lt;/span&gt; optionalPerson &lt;span class=&quot;sy0&quot;&gt;=&lt;/span&gt; findPerson&lt;span class=&quot;br0&quot;&gt;&amp;#40;&lt;/span&gt;&lt;span class=&quot;nu0&quot;&gt;42&lt;/span&gt;&lt;span class=&quot;br0&quot;&gt;&amp;#41;&lt;/span&gt;&lt;span class=&quot;sy0&quot;&gt;;&lt;/span&gt;
   &lt;span class=&quot;kw1&quot;&gt;if&lt;/span&gt; &lt;span class=&quot;br0&quot;&gt;&amp;#40;&lt;/span&gt;&lt;span class=&quot;sy0&quot;&gt;!&lt;/span&gt;optionalPerson.&lt;span class=&quot;me1&quot;&gt;isEmpty&lt;/span&gt;&lt;span class=&quot;br0&quot;&gt;&amp;#40;&lt;/span&gt;&lt;span class=&quot;br0&quot;&gt;&amp;#41;&lt;/span&gt;&lt;span class=&quot;br0&quot;&gt;&amp;#41;&lt;/span&gt; &lt;span class=&quot;br0&quot;&gt;&amp;#123;&lt;/span&gt;
      Person person &lt;span class=&quot;sy0&quot;&gt;=&lt;/span&gt; optionalPerson.&lt;span class=&quot;me1&quot;&gt;get&lt;/span&gt;&lt;span class=&quot;br0&quot;&gt;&amp;#40;&lt;/span&gt;&lt;span class=&quot;br0&quot;&gt;&amp;#41;&lt;/span&gt;&lt;span class=&quot;sy0&quot;&gt;;&lt;/span&gt;
      &lt;span class=&quot;co1&quot;&gt;// process the case where we found the person&lt;/span&gt;
   &lt;span class=&quot;br0&quot;&gt;&amp;#125;&lt;/span&gt; &lt;span class=&quot;kw1&quot;&gt;else&lt;/span&gt; &lt;span class=&quot;br0&quot;&gt;&amp;#123;&lt;/span&gt;
      &lt;span class=&quot;co1&quot;&gt;// process the case where we did not find the person&lt;/span&gt;
   &lt;span class=&quot;br0&quot;&gt;&amp;#125;&lt;/span&gt;
&lt;span class=&quot;br0&quot;&gt;&amp;#125;&lt;/span&gt;&lt;/pre&gt;

&lt;p&gt;
From this code snippet, I would like to highlight a few things:
&lt;/p&gt;
&lt;ul&gt;
&lt;li class=&quot;level1&quot;&gt;&lt;div class=&quot;li&quot;&gt; Possible&amp;lt;T&amp;gt; only makes you deal with its own class as &lt;acronym title=&quot;Application Programming Interface&quot;&gt;API&lt;/acronym&gt;.  Implementation classes are not &lt;acronym title=&quot;Application Programming Interface&quot;&gt;API&lt;/acronym&gt; and are not exposed.&lt;/div&gt;
&lt;/li&gt;
&lt;li class=&quot;level1&quot;&gt;&lt;div class=&quot;li&quot;&gt; The &lt;acronym title=&quot;Application Programming Interface&quot;&gt;API&lt;/acronym&gt; is largely familiar.  With only a few minor additions, it&amp;#039;s just Java&amp;#039;s existing Collections &lt;acronym title=&quot;Application Programming Interface&quot;&gt;API&lt;/acronym&gt;, with &lt;strong&gt;zero&lt;/strong&gt; surprises.&lt;/div&gt;
&lt;/li&gt;
&lt;li class=&quot;level1&quot;&gt;&lt;div class=&quot;li&quot;&gt; The static factories are there, but in the tradition of more idiomatic Java, names are chosen so that the fully-qualified forms make sense as well as the statically-imported versions:&lt;/div&gt;
&lt;ul&gt;
&lt;li class=&quot;level2&quot;&gt;&lt;div class=&quot;li&quot;&gt; Possible&amp;lt;Integer&amp;gt; meaning = Possible.value(42);&lt;/div&gt;
&lt;/li&gt;
&lt;li class=&quot;level2&quot;&gt;&lt;div class=&quot;li&quot;&gt; Possible&amp;lt;Integer&amp;gt; meaning = Possible.emptyValue();&lt;/div&gt;
&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;
Up to this point, you might be saying, “This is nice, but why would I chose this over, say, the fine Optional class in Google&amp;#039;s Guava project?”
&lt;/p&gt;

&lt;p&gt;
First, I would heartily recommend Guava.  It has a lot of really nice ideas and adaptations from the functional programming world to Java and if that&amp;#039;s the ultimate direction you want to choose, it&amp;#039;s a great choice&lt;sup&gt;&lt;a href=&quot;#fn__2&quot; name=&quot;fnt__2&quot; id=&quot;fnt__2&quot; class=&quot;fn_top&quot;&gt;2)&lt;/a&gt;&lt;/sup&gt;.
&lt;/p&gt;

&lt;p&gt;
There are a few reasons you might consider this version:
&lt;/p&gt;
&lt;ul&gt;
&lt;li class=&quot;level1&quot;&gt;&lt;div class=&quot;li&quot;&gt; It has no dependencies other than the &lt;acronym title=&quot;Java Development Kit&quot;&gt;JDK&lt;/acronym&gt; and org.eclipse.runtime; it is small and simple enough you can “own” it yourself.&lt;/div&gt;
&lt;/li&gt;
&lt;li class=&quot;level1&quot;&gt;&lt;div class=&quot;li&quot;&gt; Possible&amp;lt;T&amp;gt;, in addition to containing a value or nothing, encapsulates an IStatus value which is useful in Eclipse development.  (If no IStatus is specified, the value defaults to Status.OK_STATUS.)&lt;/div&gt;
&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;
For library authors who do not want to be opinionated about the logging framework choice, encapsulating an IStatus allows the library to return information in a form suitable for logging to any of several loggers, including Eclipse&amp;#039;s.
&lt;/p&gt;

&lt;p&gt;
Possible&amp;lt;T&amp;gt; may be &lt;a href=&quot;https://github.com/pieceoftheloaf/possible-monad&quot; class=&quot;urlextern&quot; title=&quot;https://github.com/pieceoftheloaf/possible-monad&quot;  rel=&quot;nofollow&quot;&gt;found on GitHub&lt;/a&gt;.
&lt;/p&gt;

&lt;/div&gt;
&lt;!-- EDIT8 SECTION &quot;Improving our Option implementation&quot; [7442-] --&gt;&lt;div class=&quot;footnotes&quot;&gt;
&lt;div class=&quot;fn&quot;&gt;&lt;sup&gt;&lt;a href=&quot;#fnt__1&quot; id=&quot;fn__1&quot; name=&quot;fn__1&quot; class=&quot;fn_bot&quot;&gt;1)&lt;/a&gt;&lt;/sup&gt; 
&lt;a href=&quot;https://code.google.com/p/guava-libraries/wiki/UsingAndAvoidingNullExplained&quot; class=&quot;urlextern&quot; title=&quot;https://code.google.com/p/guava-libraries/wiki/UsingAndAvoidingNullExplained&quot;  rel=&quot;nofollow&quot;&gt;https://code.google.com/p/guava-libraries/wiki/UsingAndAvoidingNullExplained&lt;/a&gt;&lt;/div&gt;
&lt;div class=&quot;fn&quot;&gt;&lt;sup&gt;&lt;a href=&quot;#fnt__2&quot; id=&quot;fn__2&quot; name=&quot;fn__2&quot; class=&quot;fn_bot&quot;&gt;2)&lt;/a&gt;&lt;/sup&gt; 
&lt;a href=&quot;http://gdg-krakow.github.com/google-io-ext-2012-guava/&quot; class=&quot;urlextern&quot; title=&quot;http://gdg-krakow.github.com/google-io-ext-2012-guava/&quot;  rel=&quot;nofollow&quot;&gt;Google Guava -- Google I/O talk&lt;/a&gt;&lt;/div&gt;
&lt;/div&gt;
</description>
    </item>
</rdf:RDF>
