All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.wildfly.clustering.marshalling.protostream.util.AbstractMapMarshaller Maven / Gradle / Ivy

/*
 * Copyright The WildFly Authors
 * SPDX-License-Identifier: Apache-2.0
 */

package org.wildfly.clustering.marshalling.protostream.util;

import java.io.IOException;
import java.util.AbstractMap;
import java.util.Map;

import org.wildfly.clustering.marshalling.protostream.ProtoStreamMarshaller;
import org.wildfly.clustering.marshalling.protostream.ProtoStreamWriter;

/**
 * Abstract marshaller for a {@link Map} that writes the entries of the map.
 * @author Paul Ferraro
 * @param  the map type of this marshaller
 */
public abstract class AbstractMapMarshaller> implements ProtoStreamMarshaller {
	protected static final int ENTRY_INDEX = 1;

	private final Class mapClass;

	public AbstractMapMarshaller(Class mapClass) {
		this.mapClass = mapClass;
	}

	@Override
	public void writeTo(ProtoStreamWriter writer, T map) throws IOException {
		synchronized (map) { // Avoid ConcurrentModificationException
			for (Map.Entry entry : map.entrySet()) {
				writer.writeObject(ENTRY_INDEX, new AbstractMap.SimpleEntry<>(entry));
			}
		}
	}

	@Override
	public Class getJavaClass() {
		return this.mapClass;
	}
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy