View Javadoc
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  
17  package com.google.common.collect;
18  
19  import com.google.errorprone.annotations.CanIgnoreReturnValue;
20  import com.google.gwt.user.client.rpc.SerializationException;
21  import com.google.gwt.user.client.rpc.SerializationStreamReader;
22  import com.google.gwt.user.client.rpc.SerializationStreamWriter;
23  import java.util.Collection;
24  import 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   */
35  public final class Multimap_CustomFieldSerializerBase {
36  
37    static ImmutableMultimap<Object, Object> instantiate(
38        SerializationStreamReader reader, ImmutableMultimap.Builder<Object, Object> builder)
39        throws SerializationException {
40      int keyCount = reader.readInt();
41      for (int i = 0; i < keyCount; ++i) {
42        Object key = reader.readObject();
43        int valueCount = reader.readInt();
44        for (int j = 0; j < valueCount; ++j) {
45          Object value = reader.readObject();
46          builder.put(key, value);
47        }
48      }
49      return builder.build();
50    }
51  
52    @CanIgnoreReturnValue
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(SerializationStreamWriter writer, Multimap<?, ?> instance)
69        throws SerializationException {
70      writer.writeInt(instance.asMap().size());
71      for (Map.Entry<?, ? extends Collection<?>> entry : instance.asMap().entrySet()) {
72        writer.writeObject(entry.getKey());
73        writer.writeInt(entry.getValue().size());
74        for (Object value : entry.getValue()) {
75          writer.writeObject(value);
76        }
77      }
78    }
79  
80    private Multimap_CustomFieldSerializerBase() {}
81  }