org.simpleflatmapper.datastax.impl.setter.TupleValueSettableDataSetter 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.simpleflatmapper.datastax.impl.setter;
import com.datastax.driver.core.DataType;
import com.datastax.driver.core.SettableByIndexData;
import com.datastax.driver.core.TupleType;
import com.datastax.driver.core.TupleValue;
import org.simpleflatmapper.datastax.DatastaxColumnKey;
import org.simpleflatmapper.datastax.SettableDataMapperBuilder;
import org.simpleflatmapper.datastax.impl.SettableDataSetterFactory;
import org.simpleflatmapper.map.Mapper;
import org.simpleflatmapper.map.MapperConfig;
import org.simpleflatmapper.map.property.FieldMapperColumnDefinition;
import org.simpleflatmapper.map.mapper.ConstantTargetFieldMapperFactoryImpl;
import org.simpleflatmapper.reflect.ReflectionService;
import org.simpleflatmapper.reflect.Setter;
import org.simpleflatmapper.reflect.meta.ClassMeta;
import org.simpleflatmapper.tuple.Tuple2;
import java.lang.reflect.Type;
import java.util.List;
public class TupleValueSettableDataSetter implements Setter {
private final int index;
public TupleValueSettableDataSetter(int index) {
this.index = index;
}
@Override
public void set(SettableByIndexData target, TupleValue value) throws Exception {
if (value == null) {
target.setToNull(index);
} else {
target.setTupleValue(index, value);
}
}
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, ConstantTargetFieldMapperFactoryImpl.newInstance(new SettableDataSetterFactory(config, reflectionService), SettableByIndexData.class));
}
}