org.sfm.datastax.DatastaxMapperFactory 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;
import com.datastax.driver.core.GettableByIndexData;
import com.datastax.driver.core.ResultSet;
import com.datastax.driver.core.Row;
import com.datastax.driver.core.exceptions.DriverException;
import org.sfm.datastax.impl.DatastaxMappingContextFactoryBuilder;
import org.sfm.datastax.impl.RowGetterFactory;
import org.sfm.map.*;
import org.sfm.map.column.FieldMapperColumnDefinition;
import org.sfm.map.mapper.AbstractMapperFactory;
import org.sfm.map.mapper.DynamicSetRowMapper;
import org.sfm.map.mapper.FieldMapperColumnDefinitionProviderImpl;
import org.sfm.map.mapper.MapperKey;
import org.sfm.reflect.TypeReference;
import org.sfm.reflect.meta.ClassMeta;
import org.sfm.utils.UnaryFactory;
import org.sfm.utils.UnaryFactoryWithException;
import java.lang.reflect.Type;
public class DatastaxMapperFactory extends AbstractMapperFactory, DatastaxMapperFactory> {
private GetterFactory getterFactory = new RowGetterFactory(this);
private DatastaxMapperFactory() {
super(new FieldMapperColumnDefinitionProviderImpl(), FieldMapperColumnDefinition.identity());
}
public static DatastaxMapperFactory newInstance() {
return new DatastaxMapperFactory();
}
public DatastaxMapperBuilder newBuilder(Class type) {
return newBuilder((Type)type);
}
public DatastaxMapperBuilder newBuilder(TypeReference type) {
return newBuilder(type.getType());
}
public DatastaxMapperBuilder newBuilder(Type type) {
final ClassMeta classMeta = getClassMeta(type);
return newBuilder(classMeta);
}
public DatastaxMapperBuilder newBuilder(ClassMeta classMeta) {
return new DatastaxMapperBuilder(classMeta,
mapperConfig(),
getterFactory,
new DatastaxMappingContextFactoryBuilder());
}
public DatastaxBinder mapFrom(Class type) {
return mapFrom((Type) type);
}
public DatastaxBinder mapFrom(TypeReference type) {
return mapFrom(type.getType());
}
public DatastaxBinder mapFrom(Type type) {
final ClassMeta classMeta = getClassMeta(type);
return new DatastaxBinder(classMeta, mapperConfig());
}
public DatastaxMapper mapTo(Class type) {
return mapTo((Type) type);
}
public DatastaxMapper mapTo(TypeReference type) {
return mapTo(type.getType());
}
public DatastaxMapper mapTo(Type type) {
final ClassMeta classMeta = getClassMeta(type);
return new DynamicDatastaxSetRowMapper(new MapperFactory(classMeta), new MapperKeyFromRowFactory(), new MapperKeyFromSet());
}
private static class MapperKeyFromRowFactory implements UnaryFactoryWithException, DriverException> {
@Override
public MapperKey newInstance(Row row) throws DriverException {
return DatastaxColumnKey.mapperKey(row.getColumnDefinitions());
}
}
private static class MapperKeyFromSet implements UnaryFactoryWithException, DriverException> {
@Override
public MapperKey newInstance(ResultSet rows) throws DriverException {
return DatastaxColumnKey.mapperKey(rows.getColumnDefinitions());
}
}
private class MapperFactory implements UnaryFactory, SetRowMapper> {
private final ClassMeta classMeta;
public MapperFactory(ClassMeta classMeta) {
this.classMeta = classMeta;
}
@Override
public SetRowMapper newInstance(MapperKey datastaxColumnKeyMapperKey) {
DatastaxMapperBuilder builder = newBuilder(classMeta);
for(DatastaxColumnKey key : datastaxColumnKeyMapperKey.getColumns()) {
builder.addMapping(key);
}
return builder.mapper();
}
}
private static class DynamicDatastaxSetRowMapper
extends DynamicSetRowMapper
implements DatastaxMapper {
public DynamicDatastaxSetRowMapper(
UnaryFactory, SetRowMapper> mapperFactory,
UnaryFactoryWithException, DriverException> mapperKeyFromRow,
UnaryFactoryWithException, DriverException> mapperKeyFromSet) {
super(mapperFactory, mapperKeyFromRow, mapperKeyFromSet); }
}
}