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.Comparator;
24import java.util.Map;
25import java.util.Map.Entry;
26
27/**
28 * This class implements the GWT serialization of {@link TreeBasedTable}.
29 *
30 * @author Hayward Chan
31 */
32public class TreeBasedTable_CustomFieldSerializer {
33
34  public static void deserialize(SerializationStreamReader reader,
35      TreeBasedTable<?, ?, ?> instance) {
36  }
37
38  public static TreeBasedTable<Object, Object, Object> instantiate(
39      SerializationStreamReader reader) throws SerializationException {
40    @SuppressWarnings("unchecked") // The comparator isn't used statically.
41    Comparator<Object> rowComparator
42        = (Comparator<Object>) reader.readObject();
43    @SuppressWarnings("unchecked") // The comparator isn't used statically.
44    Comparator<Object> columnComparator
45        = (Comparator<Object>) reader.readObject();
46    Map<?, ?> backingMap = (Map<?, ?>) reader.readObject();
47
48    TreeBasedTable<Object, Object, Object> table
49        = TreeBasedTable.create(rowComparator, columnComparator);
50    for (Entry<?, ?> row : backingMap.entrySet()) {
51      table.row(row.getKey()).putAll((Map<?, ?>) row.getValue());
52    }
53    return table;
54  }
55
56  public static void serialize(SerializationStreamWriter writer,
57      TreeBasedTable<?, ?, ?> instance)
58      throws SerializationException {
59    /*
60     * The backing map of a TreeBasedTable is a tree map of tree map.
61     * Therefore, the backing map is GWT serializable (assuming the row,
62     * column, value, the row comparator and column comparator are all
63     * serializable).
64     */
65    writer.writeObject(instance.rowComparator());
66    writer.writeObject(instance.columnComparator());
67    writer.writeObject(instance.backingMap);
68  }
69}
70