title: GWT Serialize Ordering/Comparator of TreeMap
body: <p>I have a serializable object with a TreeMap.</p>
  
  <pre><code>Map&lt;String, Dogs&gt; dogsByNames = Maps.newTreeMap(); // guava style
  </code></pre>
  
  <p>So far, everything is fine.
  Now it's nessassary to ignore the case of the keys:</p>
  
  <pre><code>Map&lt;String, Dogs&gt; dogsByNames = Maps.newTreeMap(String.CASE_INSENSITIVE_ORDER);
  </code></pre>
  
  <p>This do not work because String.CASE_INSENSITIVE_ORDER isn't serialzable:</p>
  
  <blockquote>
    <p>com.google.gwt.user.client.rpc.SerializationException: Type
    'java.lang.String$CaseInsensitiveComparator' was not included in the
    set of types which can be serialized by this SerializationPolicy or
    its Class object could not be loaded. For security purposes, this type
    will not be serialized.: instance =
    java.lang.String$CaseInsensitiveComparator@f26f68</p>
  </blockquote>
  
  <p>So I create my own one:</p>
  
  <pre><code>private static abstract class MyComparator&lt;T&gt;
      implements
          Comparator&lt;T&gt;, // extends Ordering&lt;T&gt; do not work
          IsSerializable { // Serializable do not work
  }
  
  public static final MyComparator&lt;String&gt; CASE_INSENSITIVE_ORDER = new MyComparator&lt;String&gt;() {
      @Override
      public int compare(final String left, final String right) {
          if (null == left) {
              return (null == right) ? 0 : -1;
          } else if (null == right) {
              return 1;
          }
          return left.compareToIgnoreCase(right);
      }
  };
  </code></pre>
  
  <p>But this still do not work.</p>
  
  <p>Whats wrong?</p>
  
  <p><strong>Solution:</strong></p>
  
  <pre><code>protected static class MyOrdering // no-private!!!
      extends
          Ordering&lt;String&gt;
      implements
          IsSerializable {
      @Override
      public int compare(final String left, final String right) {
          if (null == left) {
              return (null == right) ? 0 : -1;
          } else if (null == right) {
              return 1;
          }
          return left.compareToIgnoreCase(right);
      }
  }
  
  public static final Ordering&lt;String&gt; CASE_INSENSITIVE_ORDER = new MyOrdering();
  </code></pre>