title: Null Safe dereferencing in Java like ?. in Groovy using Maybe monad
body: <p>I'm working on a codebase ported from Objective C to Java. There are several usages of method chaining without nullchecks</p>
  
  <pre><code>dog.collar().tag().name()
  </code></pre>
  
  <p>I was looking for something similar to <a href="http://groovy.codehaus.org/Null+Object+Pattern" rel="nofollow noreferrer">safe-dereferencing operator</a> ?. in Groovy instead of having nullchecks</p>
  
  <pre><code>dog.collar?.tag?.name
  </code></pre>
  
  <p>This led to <a href="http://www.irekjozwiak.com/entry/2009/06/13/Monads-for-Java-C---programmers.html" rel="nofollow noreferrer">Maybe monad</a> to have the notion of Nothing instead of Null. But all the implementations of Nothing i came across throw exception when value is accessed which still doesn't solve the chaining problem. I made Nothing return a mock, which behaves like NullObject pattern. But it solves the chaining problem. </p>
  
  <p>Is there anything wrong with this implementation of Nothing? </p>
  
  <pre><code>public class Nothing&lt;T&gt; implements Maybe&lt;T&gt; {
  
      private Class&lt;T&gt; klass;
  
      public Nothing(Class&lt;T&gt; klass) {
          this.klass = klass;
      }
  
      @Override
      public T value() {
          return mock(klass, Mockito.RETURNS_DEEP_STUBS); 
      }
  }
  </code></pre>
  
  <p>As far as I can see </p>
  
  <ol>
  <li>It feels odd to use mocking library in code </li>
  <li>It doesn't stop at the first null. </li>
  <li>How do i distinguish between null result because of null reference or name actually being null? How is it distinguished in Groovy code?</li>
  </ol>