001/*
002 * Copyright (C) 2009 The Guava Authors
003 *
004 * Licensed under the Apache License, Version 2.0 (the "License");
005 * you may not use this file except in compliance with the License.
006 * You may obtain a copy of the License at
007 *
008 * http://www.apache.org/licenses/LICENSE-2.0
009 *
010 * Unless required by applicable law or agreed to in writing, software
011 * distributed under the License is distributed on an "AS IS" BASIS,
012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013 * See the License for the specific language governing permissions and
014 * limitations under the License.
015 */
016
017package com.google.common.collect;
018
019import com.google.errorprone.annotations.CanIgnoreReturnValue;
020import com.google.gwt.user.client.rpc.SerializationException;
021import com.google.gwt.user.client.rpc.SerializationStreamReader;
022import com.google.gwt.user.client.rpc.SerializationStreamWriter;
023import java.util.Collection;
024import java.util.Map.Entry;
025
026/**
027 * This class contains static utility methods for writing {@code Multimap} GWT field serializers.
028 * Serializers should delegate to {@link #serialize(SerializationStreamWriter, Multimap)} and to
029 * either {@link #instantiate(SerializationStreamReader, ImmutableMultimap.Builder)} or {@link
030 * #populate(SerializationStreamReader, Multimap)}.
031 *
032 * @author Chris Povirk
033 */
034public final class Multimap_CustomFieldSerializerBase {
035
036  static ImmutableMultimap<Object, Object> instantiate(
037      SerializationStreamReader reader, ImmutableMultimap.Builder<Object, Object> builder)
038      throws SerializationException {
039    int keyCount = reader.readInt();
040    for (int i = 0; i < keyCount; ++i) {
041      Object key = reader.readObject();
042      int valueCount = reader.readInt();
043      for (int j = 0; j < valueCount; ++j) {
044        Object value = reader.readObject();
045        builder.put(key, value);
046      }
047    }
048    return builder.build();
049  }
050
051  @CanIgnoreReturnValue
052  public static Multimap<Object, Object> populate(
053      SerializationStreamReader reader, Multimap<Object, Object> multimap)
054      throws SerializationException {
055    int keyCount = reader.readInt();
056    for (int i = 0; i < keyCount; ++i) {
057      Object key = reader.readObject();
058      int valueCount = reader.readInt();
059      for (int j = 0; j < valueCount; ++j) {
060        Object value = reader.readObject();
061        multimap.put(key, value);
062      }
063    }
064    return multimap;
065  }
066
067  public static void serialize(SerializationStreamWriter writer, Multimap<?, ?> instance)
068      throws SerializationException {
069    writer.writeInt(instance.asMap().size());
070    for (Entry<?, ? extends Collection<?>> entry : instance.asMap().entrySet()) {
071      writer.writeObject(entry.getKey());
072      writer.writeInt(entry.getValue().size());
073      for (Object value : entry.getValue()) {
074        writer.writeObject(value);
075      }
076    }
077  }
078
079  private Multimap_CustomFieldSerializerBase() {}
080}