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

org.wildfly.clustering.marshalling.jboss.IdentityTable Maven / Gradle / Ivy

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

package org.wildfly.clustering.marshalling.jboss;

import java.io.IOException;
import java.util.IdentityHashMap;
import java.util.List;
import java.util.ListIterator;
import java.util.Map;

import org.jboss.marshalling.Marshaller;
import org.jboss.marshalling.Unmarshaller;
import org.wildfly.clustering.marshalling.IndexSerializer;
import org.wildfly.clustering.marshalling.IntSerializer;
import org.wildfly.common.function.ExceptionBiConsumer;

/**
 * Generic identity table.
 * @param  the table entry type
 * @author Paul Ferraro
 */
public interface IdentityTable {

	ExceptionBiConsumer findWriter(T value);

	T read(Unmarshaller unmarshaller) throws IOException, ClassNotFoundException;

	static  IdentityTable from(List entries) {
		IntSerializer indexSerializer = IndexSerializer.select(entries.size());
		Map indexes = new IdentityHashMap<>(entries.size());
		ListIterator iterator = entries.listIterator();
		while (iterator.hasNext()) {
			indexes.putIfAbsent(iterator.next(), iterator.previousIndex());
		}
		ExceptionBiConsumer writer = new ExceptionBiConsumer<>() {
			@Override
			public void accept(Marshaller marshaller, T value) throws IOException {
				int index = indexes.get(value);
				indexSerializer.writeInt(marshaller, index);
			}
		};
		return new IdentityTable<>() {
			@Override
			public ExceptionBiConsumer findWriter(T value) {
				return indexes.containsKey(value) ? writer : null;
			}

			@Override
			public T read(Unmarshaller unmarshaller) throws IOException, ClassNotFoundException {
				int index = indexSerializer.readInt(unmarshaller);
				return entries.get(index);
			}
		};
	}
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy