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

io.tarantool.driver.mappers.factories.AbstractResultMapperFactory Maven / Gradle / Ivy

Go to download

Tarantool Cartridge driver for Tarantool versions 1.10+ based on Netty framework

There is a newer version: 0.14.0
Show newest version
package io.tarantool.driver.mappers.factories;

import io.tarantool.driver.exceptions.TarantoolClientException;
import io.tarantool.driver.mappers.AbstractResultMapper;
import io.tarantool.driver.mappers.InterfaceParameterClassNotFoundException;
import io.tarantool.driver.mappers.MapperReflectionUtils;
import io.tarantool.driver.mappers.MessagePackValueMapper;
import io.tarantool.driver.mappers.converters.ValueConverter;
import io.tarantool.driver.mappers.converters.ValueConverterWithInputTypeWrapper;
import org.msgpack.value.Value;
import org.msgpack.value.ValueType;

import java.util.List;

/**
 * Base class for result mapper factories.
 *
 * @param  target result mapper type
 * @param  target result type
 * @author Alexey Kuzin
 */
public abstract class AbstractResultMapperFactory> {

    /**
     * Basic constructor
     */
    public AbstractResultMapperFactory() {
    }

    /**
     * Instantiate the mapper for result contents
     *
     * @param valueMapper    MessagePack value-to-object mapper for result contents
     * @param valueType      MessagePack source type
     * @param valueConverter converter for result contents (an array)
     * @param resultClass    result type
     * @return new mapper instance
     */
    protected abstract T createMapper(
        MessagePackValueMapper valueMapper,
        ValueType valueType,
        ValueConverter valueConverter,
        Class resultClass);

    protected abstract T createMapper(
        MessagePackValueMapper valueMapper,
        ValueType valueType,
        ValueConverter valueConverter);

    protected abstract T createMapper(
        MessagePackValueMapper valueMapper,
        List> converters,
        Class resultClass);

    protected abstract T createMapper(
        MessagePackValueMapper valueMapper,
        List> converters);

    /**
     * Create {@link AbstractResultMapper} instance with the passed converter.
     *
     * @param valueMapper    MessagePack value-to-object mapper for result contents
     * @param valueConverter entity-to-object converter
     * @return a mapper instance
     */
    public T withConverter(
        MessagePackValueMapper valueMapper,
        ValueConverter valueConverter) {
        return withConverter(valueMapper, ValueType.ARRAY, valueConverter);
    }

    public T withConverter(
        MessagePackValueMapper valueMapper, ValueType valueType,
        ValueConverter valueConverter) {
        try {
            return withConverter(
                valueMapper, valueType, valueConverter, MapperReflectionUtils.getConverterTargetType(valueConverter));
        } catch (InterfaceParameterClassNotFoundException e) {
            throw new TarantoolClientException(e);
        }
    }

    public T withConverterWithoutTargetClass(
        MessagePackValueMapper valueMapper, ValueType valueType,
        ValueConverter valueConverter) {
        return createMapper(valueMapper, valueType, valueConverter);
    }

    /**
     * Create {@link AbstractResultMapper} instance with the passed converter.
     *
     * @param valueMapper    MessagePack value-to-object mapper for result contents
     * @param valueConverter entity-to-object converter
     * @param resultClass    target result type class. Necessary for resolving ambiguity when more than one suitable
     *                       converters are present in the configured mapper
     * @return a mapper instance
     */
    public T withConverter(
        MessagePackValueMapper valueMapper,
        ValueConverter valueConverter,
        Class resultClass) {
        return withConverter(valueMapper, ValueType.ARRAY, valueConverter, resultClass);
    }

    public T withConverter(
        MessagePackValueMapper valueMapper,
        ValueType valueType,
        ValueConverter valueConverter,
        Class resultClass) {
        return createMapper(valueMapper, valueType, valueConverter, resultClass);
    }

    public T withConverters(
        MessagePackValueMapper valueMapper,
        List> converters) {
        if (converters.size() < 1) {
            throw new TarantoolClientException("Empty converters list");
        }
        try {
            return withConverters(valueMapper, converters,
                MapperReflectionUtils.getConverterTargetType(converters.get(0).getValueConverter()));
        } catch (InterfaceParameterClassNotFoundException e) {
            throw new TarantoolClientException(e);
        }
    }

    public T withConverters(
        MessagePackValueMapper valueMapper,
        List> converters,
        Class resultClass) {
        return createMapper(valueMapper, converters, resultClass);
    }

    public T withConverterWithoutTargetClass(
        MessagePackValueMapper valueMapper,
        List> converters) {
        return createMapper(valueMapper, converters);
    }

}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy