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

org.sfm.datastax.impl.setter.TupleSettableDataSetter Maven / Gradle / Ivy

There is a newer version: 8.2.3
Show newest version
package org.sfm.datastax.impl.setter;

import com.datastax.driver.core.*;
import org.sfm.datastax.DatastaxColumnKey;
import org.sfm.datastax.SettableDataMapperBuilder;
import org.sfm.datastax.impl.SettableDataSetterFactory;
import org.sfm.map.Mapper;
import org.sfm.map.MapperConfig;
import org.sfm.map.column.FieldMapperColumnDefinition;
import org.sfm.map.mapper.ConstantTargetFieldMapperFactorImpl;
import org.sfm.reflect.ReflectionService;
import org.sfm.reflect.Setter;
import org.sfm.reflect.meta.ClassMeta;
import org.sfm.tuples.Tuple2;

import java.lang.reflect.Type;
import java.util.List;

public class TupleSettableDataSetter> implements Setter {
    private final int index;
    private final TupleType tupleType;
    private final Mapper mapper;

    public TupleSettableDataSetter(int index, TupleType tupleType, Mapper mapper) {
        this.index = index;
        this.tupleType = tupleType;
        this.mapper = mapper;
    }

    @Override
    public void set(SettableByIndexData target, T value) throws Exception {
        if (value == null) {
            target.setToNull(index);
        } else {
            TupleValue tupleValue = tupleType.newValue();
            mapper.mapTo(value, tupleValue, null);
            target.setTupleValue(index, tupleValue);
        }
    }

    public static > Setter newInstance(Type target,  TupleType tt, int index, MapperConfig> config,
                                                                                      ReflectionService reflectionService) {
        Mapper mapper = newTupleMapper(target, tt, config, reflectionService);
        return new TupleSettableDataSetter(index, tt, mapper);
    }

    public static > Mapper newTupleMapper(Type target, TupleType tt,
                                                                                         MapperConfig> config,
                                                                                         ReflectionService reflectionService) {
        SettableDataMapperBuilder builder = newFieldMapperBuilder(config, reflectionService, target);
        List componentTypes = tt.getComponentTypes();
        for(int i = 0; i < componentTypes.size(); i++) {
            builder.addColumn(new DatastaxColumnKey("elt" + i, i, componentTypes.get(i)));
        }
        return builder.mapper();
    }

    public static  SettableDataMapperBuilder newFieldMapperBuilder(MapperConfig> config,
                                                                         ReflectionService reflectionService,  Type target) {
        ClassMeta classMeta = reflectionService.getClassMeta(target);
        return new SettableDataMapperBuilder(classMeta, config, ConstantTargetFieldMapperFactorImpl.instance(new SettableDataSetterFactory(config, reflectionService)));
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy