1/*
2 * Copyright (C) 2009 The Guava Authors
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17package com.google.common.collect;
18
19import com.google.gwt.user.client.rpc.SerializationException;
20import com.google.gwt.user.client.rpc.SerializationStreamReader;
21import com.google.gwt.user.client.rpc.SerializationStreamWriter;
22
23import java.util.Collection;
24import java.util.Map;
25
26/**
27 * This class contains static utility methods for writing {@code Multimap} GWT
28 * field serializers. Serializers should delegate to
29 * {@link #serialize(SerializationStreamWriter, Multimap)} and to either
30 * {@link #instantiate(SerializationStreamReader, ImmutableMultimap.Builder)} or
31 * {@link #populate(SerializationStreamReader, Multimap)}.
32 *
33 * @author Chris Povirk
34 */
35public final class Multimap_CustomFieldSerializerBase {
36
37  static ImmutableMultimap<Object, Object> instantiate(
38      SerializationStreamReader reader,
39      ImmutableMultimap.Builder<Object, Object> builder)
40      throws SerializationException {
41    int keyCount = reader.readInt();
42    for (int i = 0; i < keyCount; ++i) {
43      Object key = reader.readObject();
44      int valueCount = reader.readInt();
45      for (int j = 0; j < valueCount; ++j) {
46        Object value = reader.readObject();
47        builder.put(key, value);
48      }
49    }
50    return builder.build();
51  }
52
53  public static Multimap<Object, Object> populate(
54      SerializationStreamReader reader, Multimap<Object, Object> multimap)
55      throws SerializationException {
56    int keyCount = reader.readInt();
57    for (int i = 0; i < keyCount; ++i) {
58      Object key = reader.readObject();
59      int valueCount = reader.readInt();
60      for (int j = 0; j < valueCount; ++j) {
61        Object value = reader.readObject();
62        multimap.put(key, value);
63      }
64    }
65    return multimap;
66  }
67
68  public static void serialize(
69      SerializationStreamWriter writer, Multimap<?, ?> instance)
70      throws SerializationException {
71    writer.writeInt(instance.asMap().size());
72    for (Map.Entry<?, ? extends Collection<?>> entry
73        : instance.asMap().entrySet()) {
74      writer.writeObject(entry.getKey());
75      writer.writeInt(entry.getValue().size());
76      for (Object value : entry.getValue()) {
77        writer.writeObject(value);
78      }
79    }
80  }
81
82  private Multimap_CustomFieldSerializerBase() {}
83}
84