View Javadoc
1   /*
2    * Copyright (C) 2009 The Guava Authors
3    *
4    * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except
5    * in compliance with the License. You may obtain a copy of the License at
6    *
7    * http://www.apache.org/licenses/LICENSE-2.0
8    *
9    * Unless required by applicable law or agreed to in writing, software distributed under the License
10   * is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express
11   * or implied. See the License for the specific language governing permissions and limitations under
12   * the License.
13   */
14  
15  package com.google.common.collect;
16  
17  import com.google.gwt.user.client.rpc.SerializationException;
18  import com.google.gwt.user.client.rpc.SerializationStreamReader;
19  import com.google.gwt.user.client.rpc.SerializationStreamWriter;
20  import java.util.Map;
21  import java.util.Map.Entry;
22  
23  /**
24   * This class contains static utility methods for writing {@link Table} GWT field serializers.
25   * Serializers should delegate to {@link #serialize} and {@link #populate}.
26   *
27   * <p>For {@link ImmutableTable}, see {@link ImmutableTable_CustomFieldSerializerBase}.
28   *
29   * @author Chris Povirk
30   */
31  final class Table_CustomFieldSerializerBase {
32    static <T extends StandardTable<Object, Object, Object>> T populate(
33        SerializationStreamReader reader, T table) throws SerializationException {
34      Map<?, ?> hashMap = (Map<?, ?>) reader.readObject();
35      for (Entry<?, ?> row : hashMap.entrySet()) {
36        table.row(row.getKey()).putAll((Map<?, ?>) row.getValue());
37      }
38      return table;
39    }
40  
41    static void serialize(SerializationStreamWriter writer, StandardTable<?, ?, ?> table)
42        throws SerializationException {
43      /*
44       * The backing map of a {Hash,Tree}BasedTable is a {Hash,Tree}Map of {Hash,Tree}Maps. Therefore,
45       * the backing map is serializable (assuming that the row, column and values, along with any
46       * comparators, are all serializable).
47       */
48      writer.writeObject(table.backingMap);
49    }
50  
51    private Table_CustomFieldSerializerBase() {}
52  }