GWT Serialize Ordering/Comparator of TreeMap

I hava a serializable object with a TreeMap.

Map<String, Dogs> dogsByNames = Maps.newTreeMap(); // guava style

So far, everything is fine. Now it's nessassary to ignore the case of the keys:

Map<String, Dogs> dogsByNames = Maps.newTreeMap(String.CASE_INSENSITIVE_ORDER);

This do not work because String.CASE_INSENSITIVE_ORDER isn't serialzable:

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

So I create my own one:

private static abstract class MyComparator<T>
    implements
        Comparator<T>, // extends Ordering<T> do not work
        IsSerializable { // Serializable do not work
}

public static final MyComparator<String> CASE_INSENSITIVE_ORDER = new MyComparator<String>() {
    @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);
    }
};

But this still do not work.

Whats wrong?

Solution:

protected static class MyOrdering // no-private!!!
    extends
        Ordering<String>
    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<String> CASE_INSENSITIVE_ORDER = new MyOrdering();

Answers


The CASE_INSENSITIVE_ORDER you created is an anonymous class, which cannot be serialized in GWT. To make it GWT serializable, you need to

  1. Create a custom top-level class (like

    CaseInsensitiveOrder implements Comparator<..>, Serializable {
        ...
    }
    
  2. Create a constant CASE_INSENSITIVE_ORDER like you did.

  3. Create a custom field serializer for that class (CaseInsensitiveOrder_CustomFieldSerializer), which returns the CASE_INSENSITIVE_ORDER instance on deserialize().

Need Your Help

Session variables not getting unset on subdomain after unset on main domain

php session

I have it setup so I can share session data across the main domain plus a subdomain, on the subdomain script I am using the code:

jQuery Draggable Helper stop automatic removal

jquery jquery-ui draggable helper

I've having a problem where I am using the jQuery ui draggable functionality with the helper option( helper: 'clone' ) - it all works great but when you stop dragging the helper is being removed an...

About UNIX Resources Network

Original, collect and organize Developers related documents, information and materials, contains jQuery, Html, CSS, MySQL, .NET, ASP.NET, SQL, objective-c, iPhone, Ruby on Rails, C, SQL Server, Ruby, Arrays, Regex, ASP.NET MVC, WPF, XML, Ajax, DataBase, and so on.