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 27 * {@link #populate(SerializationStreamReader, Multiset)}. 28 * 29 * @author Chris Povirk 30 */ 31 final class Multiset_CustomFieldSerializerBase { 32 populate( SerializationStreamReader reader, Multiset<Object> multiset)33 static Multiset<Object> populate( 34 SerializationStreamReader reader, Multiset<Object> multiset) 35 throws SerializationException { 36 int distinctElements = reader.readInt(); 37 for (int i = 0; i < distinctElements; i++) { 38 Object element = reader.readObject(); 39 int count = reader.readInt(); 40 multiset.add(element, count); 41 } 42 return multiset; 43 } 44 serialize(SerializationStreamWriter writer, Multiset<?> instance)45 static void serialize(SerializationStreamWriter writer, Multiset<?> instance) 46 throws SerializationException { 47 int entryCount = instance.entrySet().size(); 48 writer.writeInt(entryCount); 49 for (Multiset.Entry<?> entry : instance.entrySet()) { 50 writer.writeObject(entry.getElement()); 51 writer.writeInt(entry.getCount()); 52 } 53 } 54 Multiset_CustomFieldSerializerBase()55 private Multiset_CustomFieldSerializerBase() {} 56 } 57