
org.wildfly.clustering.marshalling.protostream.FunctionalMarshaller Maven / Gradle / Ivy
/*
* Copyright The WildFly Authors
* SPDX-License-Identifier: Apache-2.0
*/
package org.wildfly.clustering.marshalling.protostream;
import java.io.IOException;
import java.util.function.Function;
import org.wildfly.common.function.ExceptionFunction;
/**
* Marshaller that uses a functional transformation of another marshaller.
* @author Paul Ferraro
* @param the type of this marshaller
* @param the type of the mapped marshaller
*/
public class FunctionalMarshaller implements ProtoStreamMarshaller {
private final Class targetClass;
private final Function> marshallerFactory;
private final ExceptionFunction function;
private final ExceptionFunction factory;
public FunctionalMarshaller(Class targetClass, Class sourceClass, ExceptionFunction function, ExceptionFunction factory) {
this(targetClass, operation -> operation.findMarshaller(sourceClass), function, factory);
}
public FunctionalMarshaller(Class targetClass, ProtoStreamMarshaller marshaller, ExceptionFunction function, ExceptionFunction factory) {
this(targetClass, operation -> marshaller, function, factory);
}
public FunctionalMarshaller(Class targetClass, Function> marshallerFactory, ExceptionFunction function, ExceptionFunction factory) {
this.targetClass = targetClass;
this.marshallerFactory = marshallerFactory;
this.function = function;
this.factory = factory;
}
@Override
public T readFrom(ProtoStreamReader reader) throws IOException {
ProtoStreamMarshaller marshaller = this.marshallerFactory.apply(reader);
V value = marshaller.readFrom(reader);
return this.factory.apply(value);
}
@Override
public void writeTo(ProtoStreamWriter writer, T object) throws IOException {
V value = this.function.apply(object);
ProtoStreamMarshaller marshaller = this.marshallerFactory.apply(writer);
marshaller.writeTo(writer, value);
}
@Override
public Class extends T> getJavaClass() {
return this.targetClass;
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy