title: How to simplify a null-safe compareTo() implementation?
body: <p>I'm implementing <code>compareTo()</code> method for a simple class such as this (to be able to use <code>Collections.sort()</code> and other goodies offered by the Java platform):</p>
  
  <pre><code>public class Metadata implements Comparable&lt;Metadata&gt; {
      private String name;
      private String value;
  
  // Imagine basic constructor and accessors here
  // Irrelevant parts omitted
  }
  </code></pre>
  
  <p>I want the <em>natural ordering</em> for these objects to be: 1) sorted by name and 2) sorted by value if name is the same; both comparisons should be case-insensitive. For both fields null values are perfectly acceptable, so <code>compareTo</code> must not break in these cases. </p>
  
  <p>The solution that springs to mind is along the lines of the following (I'm using "guard clauses" here while others might prefer a single return point, but that's beside the point):</p>
  
  <pre><code>// primarily by name, secondarily by value; null-safe; case-insensitive
  public int compareTo(Metadata other) {
      if (this.name == null &amp;&amp; other.name != null){
          return -1;
      }
      else if (this.name != null &amp;&amp; other.name == null){
          return 1;
      }
      else if (this.name != null &amp;&amp; other.name != null) {
          int result = this.name.compareToIgnoreCase(other.name);
          if (result != 0){
              return result;
          }
      }
  
      if (this.value == null) {
          return other.value == null ? 0 : -1;
      }
      if (other.value == null){
          return 1;
      }
  
      return this.value.compareToIgnoreCase(other.value);
  }
  </code></pre>
  
  <p>This does the job, but I'm not perfectly happy with this code. Admittedly it isn't <em>very</em> complex, but is quite verbose and tedious.</p>
  
  <p>The question is, <strong>how would you make this less verbose</strong> (while retaining the functionality)? Feel free to refer to Java standard libraries or Apache Commons if they help. Would the only option to make this (a little) simpler be to implement my own "NullSafeStringComparator", and apply it for comparing both fields?</p>
  
  <p><strong>Edits 1-3</strong>: Eddie's right; fixed the "both names are null" case above</p>
  
  <h2>About the accepted answer</h2>
  
  <p>I asked this question back in 2009, on Java 1.6 of course, and at the time <strong><a href="https://stackoverflow.com/a/481836/56285">the pure JDK solution by Eddie</a></strong> was my preferred accepted answer. I never got round to changing that until now (2017).</p>
  
  <p>There are also <a href="https://stackoverflow.com/a/500643/56285">3rd party library solutions</a>—a 2009 Apache Commons Collections one and a 2013 Guava one, both posted by me—that I did prefer at some point in time.</p>
  
  <p>I now made the clean <strong><a href="https://stackoverflow.com/a/23908426/56285">Java 8 solution by Lukasz Wiktor</a></strong> the accepted answer. That should definitely be preferred if on Java 8, and these days Java 8 should be available to nearly all projects.</p>