diff options
Diffstat (limited to 'guava-gwt/src/com/google/common/collect/ImmutableSortedMap_CustomFieldSerializer.java')
-rw-r--r-- | guava-gwt/src/com/google/common/collect/ImmutableSortedMap_CustomFieldSerializer.java | 41 |
1 files changed, 37 insertions, 4 deletions
diff --git a/guava-gwt/src/com/google/common/collect/ImmutableSortedMap_CustomFieldSerializer.java b/guava-gwt/src/com/google/common/collect/ImmutableSortedMap_CustomFieldSerializer.java index 70cf908..6293686 100644 --- a/guava-gwt/src/com/google/common/collect/ImmutableSortedMap_CustomFieldSerializer.java +++ b/guava-gwt/src/com/google/common/collect/ImmutableSortedMap_CustomFieldSerializer.java @@ -16,11 +16,44 @@ package com.google.common.collect; +import com.google.gwt.user.client.rpc.SerializationException; +import com.google.gwt.user.client.rpc.SerializationStreamReader; +import com.google.gwt.user.client.rpc.SerializationStreamWriter; +import com.google.gwt.user.client.rpc.core.java.util.Map_CustomFieldSerializerBase; + +import java.util.Comparator; +import java.util.SortedMap; +import java.util.TreeMap; + /** - * Even though {@link ImmutableSortedMap} cannot be instantiated, we still need a custom field - * serializer. TODO(cpovirk): why? Does it help if ensure that the GWT and non-GWT classes have the - * same fields? Is that worth the trouble? + * This class implements the GWT serialization of {@link ImmutableSortedMap}. * * @author Chris Povirk */ -public final class ImmutableSortedMap_CustomFieldSerializer {} +public class ImmutableSortedMap_CustomFieldSerializer { + public static void deserialize(SerializationStreamReader reader, + ImmutableSortedMap<?, ?> instance) { + } + + public static ImmutableSortedMap<?, ?> instantiate( + SerializationStreamReader reader) throws SerializationException { + /* + * Nothing we can do, but we're already assuming the serialized form is + * correctly typed, anyway. + */ + @SuppressWarnings("unchecked") + Comparator<Object> comparator = (Comparator<Object>) reader.readObject(); + + SortedMap<Object, Object> entries = new TreeMap<Object, Object>(comparator); + Map_CustomFieldSerializerBase.deserialize(reader, entries); + + return ImmutableSortedMap.orderedBy(comparator).putAll(entries).build(); + } + + public static void serialize(SerializationStreamWriter writer, + ImmutableSortedMap<?, ?> instance) throws SerializationException { + writer.writeObject(instance.comparator()); + + Map_CustomFieldSerializerBase.serialize(writer, instance); + } +} |