title: Idiomatic way of converting from Long object to long primitive safely in java
body: <p>I was trying to pass a Long object value to a method that expects long primitive, passing directly works except for the case when the Long object is null. In this case I get a Null Pointer Exception.</p>
  
  <pre><code>Long foo=null;
  bar.methodExpects_long_primitive(foo);
  </code></pre>
  
  <p>I can create a check if foo is null and skip calling the method, like</p>
  
  <pre><code>Long foo=null;
  if(foo!=null){
  bar.methodExpects_long_primitive(foo);
  }
  </code></pre>
  
  <p>or if I want to provide a default value</p>
  
  <pre><code>Long foo=null;
  bar.methodExpects_long_primitive(foo==null?defaultValue:foo);
  </code></pre>
  
  <p>Is there a elegant/better way to do this? I have to repeat this multiple times in the codebase and doing it like this seems to add a lot of conditionals.</p>
  
  <p>I could create my own method to do this, but I would like to know if there is already any such library method.</p>