org.simpleflatmapper.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.simpleflatmapper.datastax;
import com.datastax.driver.core.GettableByIndexData;
import com.datastax.driver.core.ResultSet;
import com.datastax.driver.core.Row;
import com.datastax.driver.core.SettableByIndexData;
import com.datastax.driver.core.exceptions.DriverException;
import org.simpleflatmapper.reflect.getter.GetterFactory;
import org.simpleflatmapper.datastax.impl.DatastaxMapperKeyComparator;
import org.simpleflatmapper.datastax.impl.DatastaxMappingContextFactoryBuilder;
import org.simpleflatmapper.datastax.impl.RowGetterFactory;
import org.simpleflatmapper.datastax.impl.SettableDataSetterFactory;
import org.simpleflatmapper.map.MapperConfig;
import org.simpleflatmapper.map.SetRowMapper;
import org.simpleflatmapper.map.property.FieldMapperColumnDefinition;
import org.simpleflatmapper.map.mapper.AbstractMapperFactory;
import org.simpleflatmapper.map.mapper.ConstantTargetFieldMapperFactoryImpl;
import org.simpleflatmapper.map.mapper.DynamicSetRowMapper;
import org.simpleflatmapper.map.mapper.FieldMapperColumnDefinitionProviderImpl;
import org.simpleflatmapper.map.mapper.MapperKey;
import org.simpleflatmapper.util.TypeReference;
import org.simpleflatmapper.reflect.meta.ClassMeta;
import org.simpleflatmapper.util.UnaryFactory;
import org.simpleflatmapper.util.UnaryFactoryWithException;
import java.lang.reflect.Type;
public class DatastaxMapperFactory extends AbstractMapperFactory {
private GetterFactory getterFactory = new RowGetterFactory(this);
private DatastaxMapperFactory() {
super(new FieldMapperColumnDefinitionProviderImpl(), FieldMapperColumnDefinition.identity());
}
private DatastaxMapperFactory(AbstractMapperFactory config) {
super(config);
}
public static DatastaxMapperFactory newInstance() {
return new DatastaxMapperFactory();
}
public static DatastaxMapperFactory newInstance(AbstractMapperFactory config) {
return new DatastaxMapperFactory(config);
}
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 SettableDataMapperBuilder newBuilderFrom(TypeReference type) {
return newBuilderFrom(type.getType());
}
public SettableDataMapperBuilder newBuilderFrom(Class type) {
return newBuilderFrom((Type)type);
}
public SettableDataMapperBuilder newBuilderFrom(Type type) {
ClassMeta classMeta = getClassMeta(type);
return newBuilderFrom(classMeta);
}
public SettableDataMapperBuilder newBuilderFrom(ClassMeta classMeta) {
MapperConfig config = mapperConfig();
return new SettableDataMapperBuilder(
classMeta,
config,
ConstantTargetFieldMapperFactoryImpl
.newInstance(new SettableDataSetterFactory(config, classMeta.getReflectionService()), SettableByIndexData.class));
}
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());
}
public DatastaxCrudDSL crud(Class targetType, Class keyType) {
return crud((Type)targetType, (Type)keyType);
}
public DatastaxCrudDSL crud(Type targetType, Type keyType) {
return new DatastaxCrudDSL(targetType, keyType, this);
}
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, DatastaxMapperKeyComparator.INSTANCE); }
}
}