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.common.annotations.GwtCompatible; 20 21 import java.util.Comparator; 22 import java.util.HashMap; 23 import java.util.TreeMap; 24 25 /** 26 * Contains dummy collection implementations to convince GWT that part of 27 * serializing a collection is serializing its elements. 28 * 29 * <p>Because of our use of final fields in our collections, GWT's normal 30 * heuristic for determining which classes might be serialized fails. That 31 * heuristic is, roughly speaking, to look at each parameter and return type of 32 * each RPC interface and to assume that implementations of those types might be 33 * serialized. Those types have their own dependencies -- their fields -- which 34 * are analyzed recursively and analogously. 35 * 36 * <p>For classes with final fields, GWT assumes that the class itself might be 37 * serialized but doesn't assume the same about its final fields. To work around 38 * this, we provide dummy implementations of our collections with their 39 * dependencies as non-final fields. Even though these implementations are never 40 * instantiated, they are visible to GWT when it performs its serialization 41 * analysis, and it assumes that their fields may be serialized. 42 * 43 * <p>Currently we provide dummy implementations of all the immutable 44 * collection classes necessary to support declarations like 45 * {@code ImmutableMultiset<String>} in RPC interfaces. Support for 46 * {@code ImmutableMultiset} in the interface is support for {@code Multiset}, 47 * so there is nothing further to be done to support the new collection 48 * interfaces. It is not support, however, for an RPC interface in terms of 49 * {@code HashMultiset}. It is still possible to send a {@code HashMultiset} 50 * over GWT RPC; it is only the declaration of an interface in terms of 51 * {@code HashMultiset} that we haven't tried to support. (We may wish to 52 * revisit this decision in the future.) 53 * 54 * @author Chris Povirk 55 */ 56 @GwtCompatible 57 // None of these classes are instantiated, let alone serialized: 58 @SuppressWarnings("serial") 59 final class GwtSerializationDependencies { 60 private GwtSerializationDependencies() {} 61 62 static final class ImmutableListMultimapDependencies<K, V> 63 extends ImmutableListMultimap<K, V> { 64 K key; 65 V value; 66 67 ImmutableListMultimapDependencies() { 68 super(null, 0); 69 } 70 } 71 72 // ImmutableMap is covered by ImmutableSortedMap/ImmutableBiMap. 73 74 // ImmutableMultimap is covered by ImmutableSetMultimap/ImmutableListMultimap. 75 76 static final class ImmutableSetMultimapDependencies<K, V> 77 extends ImmutableSetMultimap<K, V> { 78 K key; 79 V value; 80 81 ImmutableSetMultimapDependencies() { 82 super(null, 0, null); 83 } 84 } 85 86 /* 87 * We support an interface declared in terms of LinkedListMultimap because it 88 * supports entry ordering not supported by other implementations. 89 */ 90 static final class LinkedListMultimapDependencies<K, V> 91 extends LinkedListMultimap<K, V> { 92 K key; 93 V value; 94 95 LinkedListMultimapDependencies() { 96 super(); 97 } 98 } 99 100 static final class HashBasedTableDependencies<R, C, V> 101 extends HashBasedTable<R, C, V> { 102 HashMap<R, HashMap<C, V>> data; 103 104 HashBasedTableDependencies() { 105 super(null, null); 106 } 107 } 108 109 static final class TreeBasedTableDependencies<R, C, V> 110 extends TreeBasedTable<R, C, V> { 111 TreeMap<R, TreeMap<C, V>> data; 112 113 TreeBasedTableDependencies() { 114 super(null, null); 115 } 116 } 117 118 static final class TreeMultimapDependencies<K, V> 119 extends TreeMultimap<K, V> { 120 Comparator<? super K> keyComparator; 121 Comparator<? super V> valueComparator; 122 K key; 123 V value; 124 125 TreeMultimapDependencies() { 126 super(null, null); 127 } 128 } 129 } 130