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 23 /** 24 * This class contains static utility methods for writing {@code Multiset} GWT 25 * field serializers. Serializers should delegate to 26 * {@link #serialize(SerializationStreamWriter, Multiset)} and to either 27 * {@link #instantiate(SerializationStreamReader, ImmutableMultiset.Builder)} or 28 * {@link #populate(SerializationStreamReader, Multiset)}. 29 * 30 * @author Chris Povirk 31 */ 32 final class Multiset_CustomFieldSerializerBase { instantiate( SerializationStreamReader reader, ImmutableMultiset.Builder<Object> builder)33 static ImmutableMultiset<Object> instantiate( 34 SerializationStreamReader reader, 35 ImmutableMultiset.Builder<Object> builder) 36 throws SerializationException { 37 int distinctElements = reader.readInt(); 38 for (int i = 0; i < distinctElements; i++) { 39 Object element = reader.readObject(); 40 int count = reader.readInt(); 41 builder.addCopies(element, count); 42 } 43 return builder.build(); 44 } 45 populate( SerializationStreamReader reader, Multiset<Object> multiset)46 static Multiset<Object> populate( 47 SerializationStreamReader reader, Multiset<Object> multiset) 48 throws SerializationException { 49 int distinctElements = reader.readInt(); 50 for (int i = 0; i < distinctElements; i++) { 51 Object element = reader.readObject(); 52 int count = reader.readInt(); 53 multiset.add(element, count); 54 } 55 return multiset; 56 } 57 serialize(SerializationStreamWriter writer, Multiset<?> instance)58 static void serialize(SerializationStreamWriter writer, Multiset<?> instance) 59 throws SerializationException { 60 int entryCount = instance.entrySet().size(); 61 writer.writeInt(entryCount); 62 for (Multiset.Entry<?> entry : instance.entrySet()) { 63 writer.writeObject(entry.getElement()); 64 writer.writeInt(entry.getCount()); 65 } 66 } 67 Multiset_CustomFieldSerializerBase()68 private Multiset_CustomFieldSerializerBase() {} 69 } 70