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

org.wildfly.clustering.marshalling.protostream.util.AbstractCollectionMarshaller 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.Collection;

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

/**
 * Abstract collection marshaller that writes the elements of the collection.
 * @author Paul Ferraro
 * @param  the collection type of this marshaller
 */
public abstract class AbstractCollectionMarshaller> implements ProtoStreamMarshaller {

	protected static final int ELEMENT_INDEX = 1;

	private final Class collectionClass;

	public AbstractCollectionMarshaller(Class collectionClass) {
		this.collectionClass = collectionClass;
	}

	@Override
	public void writeTo(ProtoStreamWriter writer, T collection) throws IOException {
		synchronized (collection) { // Avoid ConcurrentModificationException
			for (Object element : collection) {
				writer.writeAny(ELEMENT_INDEX, element);
			}
		}
	}

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




© 2015 - 2024 Weber Informatics LLC | Privacy Policy