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.gwt.user.client.rpc.SerializationException;
020import com.google.gwt.user.client.rpc.SerializationStreamReader;
021import com.google.gwt.user.client.rpc.SerializationStreamWriter;
022import java.util.Collection;
023import java.util.LinkedHashMap;
024import java.util.Map;
025import java.util.Map.Entry;
026
027/**
028 * This class implements the GWT serialization of {@link LinkedHashMultimap}.
029 *
030 * @author Chris Povirk
031 */
032public class LinkedHashMultimap_CustomFieldSerializer {
033
034  public static void deserialize(SerializationStreamReader in, LinkedHashMultimap<?, ?> out) {}
035
036  public static LinkedHashMultimap<Object, Object> instantiate(SerializationStreamReader stream)
037      throws SerializationException {
038    LinkedHashMultimap<Object, Object> multimap = LinkedHashMultimap.create();
039
040    int distinctKeys = stream.readInt();
041    Map<Object, Collection<Object>> map = new LinkedHashMap<>();
042    for (int i = 0; i < distinctKeys; i++) {
043      Object key = stream.readObject();
044      map.put(key, multimap.createCollection(key));
045    }
046    int entries = stream.readInt();
047    for (int i = 0; i < entries; i++) {
048      Object key = stream.readObject();
049      Object value = stream.readObject();
050      map.get(key).add(value);
051    }
052    multimap.setMap(map);
053
054    return multimap;
055  }
056
057  public static void serialize(SerializationStreamWriter stream, LinkedHashMultimap<?, ?> multimap)
058      throws SerializationException {
059    stream.writeInt(multimap.keySet().size());
060    for (Object key : multimap.keySet()) {
061      stream.writeObject(key);
062    }
063    stream.writeInt(multimap.size());
064    for (Entry<?, ?> entry : multimap.entries()) {
065      stream.writeObject(entry.getKey());
066      stream.writeObject(entry.getValue());
067    }
068  }
069}