org.sfm.datastax.impl.getter.DatastaxTupleGetter Maven / Gradle / Ivy
Show all versions of sfm-datastax Show documentation
package org.sfm.datastax.impl.getter;
import com.datastax.driver.core.DataType;
import com.datastax.driver.core.GettableByIndexData;
import com.datastax.driver.core.TupleType;
import org.sfm.datastax.DatastaxColumnKey;
import org.sfm.datastax.DatastaxMapperFactory;
import org.sfm.map.Mapper;
import org.sfm.map.column.FieldMapperColumnDefinition;
import org.sfm.map.mapper.FieldMapperMapperBuilder;
import org.sfm.reflect.Getter;
import org.sfm.tuples.Tuple2;
import java.lang.reflect.Type;
import java.util.List;
public class DatastaxTupleGetter> implements Getter {
private final Mapper mapper;
private final int index;
public DatastaxTupleGetter(Mapper mapper, int index) {
this.mapper = mapper;
this.index = index;
}
@Override
public T get(GettableByIndexData target) throws Exception {
return mapper.map(target.getTupleValue(index));
}
@SuppressWarnings("unchecked")
public static > Getter newInstance(DatastaxMapperFactory factory, Type target, TupleType tt, int index) {
Mapper mapper = newTupleMapper(target, tt, factory);
return new DatastaxTupleGetter(mapper, index);
}
public static
> Mapper newTupleMapper(Type target, TupleType tt, DatastaxMapperFactory factory) {
FieldMapperMapperBuilder builder =
DatastaxUDTGetter.newFieldMapperBuilder(factory, target);
List componentTypes = tt.getComponentTypes();
for(int i = 0; i < componentTypes.size(); i++) {
FieldMapperColumnDefinition identity = FieldMapperColumnDefinition.identity();
builder.addMapping(new DatastaxColumnKey("elt" + i, i, componentTypes.get(i)),
identity);
}
return builder.mapper();
}
}