io.tarantool.driver.mappers.AbstractTarantoolResultMapperFactory Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of cartridge-driver Show documentation
Show all versions of cartridge-driver Show documentation
Tarantool Cartridge driver for Tarantool versions 1.10+ based on Netty framework
package io.tarantool.driver.mappers;
import io.tarantool.driver.api.TarantoolResult;
import io.tarantool.driver.api.tuple.TarantoolTuple;
import io.tarantool.driver.metadata.TarantoolSpaceMetadata;
import org.msgpack.value.ArrayValue;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;
/**
* Base class for {@link TarantoolResult} mapper factories
*
* @author Alexey Kuzin
*/
public abstract class AbstractTarantoolResultMapperFactory {
protected final MessagePackMapper messagePackMapper;
protected final Map, MessagePackValueMapper> mapperCache = new ConcurrentHashMap<>();
/**
* Basic constructor
*
* @param messagePackMapper mapper for MessagePack entities into Java objects and vice versa
*/
public AbstractTarantoolResultMapperFactory(MessagePackMapper messagePackMapper) {
this.messagePackMapper = messagePackMapper;
}
/**
* Basic constructor with empty mapper
*/
public AbstractTarantoolResultMapperFactory() {
this.messagePackMapper = new DefaultMessagePackMapper();
}
protected abstract AbstractTarantoolResultMapper createMapper(ValueConverter valueConverter);
/**
* Get default {@link TarantoolTuple} converter
*
* @param spaceMetadata configured {@link TarantoolSpaceMetadata} instance
* @return default mapper instance configured with {@link DefaultTarantoolTupleValueConverter} instance
*/
public AbstractTarantoolResultMapper withDefaultTupleValueConverter(
TarantoolSpaceMetadata spaceMetadata) {
return withConverter(TarantoolTuple.class,
new DefaultTarantoolTupleValueConverter(messagePackMapper, spaceMetadata));
}
/**
* Create TarantoolResultMapper instance with the passed converter.
*
* @param valueConverter entity-to-object converter
* @param target object type
* @return a mapper instance
*/
public AbstractTarantoolResultMapper withConverter(ValueConverter valueConverter) {
return withConverter(MapperReflectionUtils.getConverterTargetType(valueConverter), valueConverter);
}
/**
* Create TarantoolResultMapper instance with the passed converter.
*
* @param tupleClass target object type class. Necessary for resolving ambiguity when more than one suitable
* converters are present in the configured mapper
* @param valueConverter entity-to-object converter
* @param target object type
* @return a mapper instance
*/
@SuppressWarnings("unchecked")
public AbstractTarantoolResultMapper withConverter(Class tupleClass,
ValueConverter valueConverter) {
return (AbstractTarantoolResultMapper) mapperCache.computeIfAbsent(
tupleClass, c -> createMapper(valueConverter));
}
}