org.sfm.datastax.impl.setter.TupleSettableDataSetter Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of sfm-datastax Show documentation
Show all versions of sfm-datastax Show documentation
Cassandra Datastax SFM supports.
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)));
}
}