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.gwt.user.client.rpc.SerializationException;
20  import com.google.gwt.user.client.rpc.SerializationStreamReader;
21  import com.google.gwt.user.client.rpc.SerializationStreamWriter;
22  import java.util.Collection;
23  import java.util.LinkedHashMap;
24  import java.util.Map;
25  
26  /**
27   * This class implements the GWT serialization of {@link LinkedHashMultimap}.
28   *
29   * @author Chris Povirk
30   */
31  public class LinkedHashMultimap_CustomFieldSerializer {
32  
33    public static void deserialize(SerializationStreamReader in, LinkedHashMultimap<?, ?> out) {}
34  
35    public static LinkedHashMultimap<Object, Object> instantiate(SerializationStreamReader stream)
36        throws SerializationException {
37      LinkedHashMultimap<Object, Object> multimap = LinkedHashMultimap.create();
38  
39      int distinctKeys = stream.readInt();
40      Map<Object, Collection<Object>> map = new LinkedHashMap<>();
41      for (int i = 0; i < distinctKeys; i++) {
42        Object key = stream.readObject();
43        map.put(key, multimap.createCollection(key));
44      }
45      int entries = stream.readInt();
46      for (int i = 0; i < entries; i++) {
47        Object key = stream.readObject();
48        Object value = stream.readObject();
49        map.get(key).add(value);
50      }
51      multimap.setMap(map);
52  
53      return multimap;
54    }
55  
56    public static void serialize(SerializationStreamWriter stream, LinkedHashMultimap<?, ?> multimap)
57        throws SerializationException {
58      stream.writeInt(multimap.keySet().size());
59      for (Object key : multimap.keySet()) {
60        stream.writeObject(key);
61      }
62      stream.writeInt(multimap.size());
63      for (Map.Entry<?, ?> entry : multimap.entries()) {
64        stream.writeObject(entry.getKey());
65        stream.writeObject(entry.getValue());
66      }
67    }
68  }