org.sfm.datastax.impl.SettableDataSetterFactory Maven / Gradle / Ivy
package org.sfm.datastax.impl;
import com.datastax.driver.core.*;
import org.sfm.datastax.DatastaxColumnKey;
import org.sfm.datastax.impl.setter.*;
import org.sfm.map.Mapper;
import org.sfm.map.MapperConfig;
import org.sfm.map.column.FieldMapperColumnDefinition;
import org.sfm.map.mapper.ColumnDefinition;
import org.sfm.map.mapper.PropertyMapping;
import org.sfm.map.setter.ConvertDelegateSetter;
import org.sfm.reflect.ReflectionService;
import org.sfm.reflect.Setter;
import org.sfm.reflect.SetterFactory;
import org.sfm.reflect.TypeHelper;
import org.sfm.tuples.Tuple2;
import org.sfm.utils.conv.Converter;
import org.sfm.utils.conv.ConverterFactory;
import java.lang.reflect.Type;
import java.math.BigDecimal;
import java.math.BigInteger;
import java.net.InetAddress;
import java.util.*;
public class SettableDataSetterFactory
implements
SetterFactory>>
{
private final Map, SetterFactory>>> factoryPerClass =
new HashMap, SetterFactory>>>();
private final MapperConfig> mapperConfig;
private final ReflectionService reflectionService;
{
factoryPerClass.put(int.class, new SetterFactory>>() {
@SuppressWarnings("unchecked")
@Override
public Setter getSetter(PropertyMapping, ?, DatastaxColumnKey, ? extends ColumnDefinition> arg) {
return (Setter) new IntSettableDataSetter(arg.getColumnKey().getIndex());
}
});
factoryPerClass.put(Integer.class, factoryPerClass.get(int.class));
factoryPerClass.put(long.class, new SetterFactory>>() {
@SuppressWarnings("unchecked")
@Override
public Setter getSetter(PropertyMapping, ?, DatastaxColumnKey, ? extends ColumnDefinition> arg) {
return (Setter) new LongSettableDataSetter(arg.getColumnKey().getIndex());
}
});
factoryPerClass.put(Long.class, factoryPerClass.get(long.class));
factoryPerClass.put(float.class, new SetterFactory>>() {
@SuppressWarnings("unchecked")
@Override
public Setter getSetter(PropertyMapping, ?, DatastaxColumnKey, ? extends ColumnDefinition> arg) {
return (Setter) new FloatSettableDataSetter(arg.getColumnKey().getIndex());
}
});
factoryPerClass.put(Float.class, factoryPerClass.get(float.class));
factoryPerClass.put(double.class, new SetterFactory>>() {
@SuppressWarnings("unchecked")
@Override
public Setter getSetter(PropertyMapping, ?, DatastaxColumnKey, ? extends ColumnDefinition> arg) {
return (Setter) new DoubleSettableDataSetter(arg.getColumnKey().getIndex());
}
});
factoryPerClass.put(Double.class, factoryPerClass.get(double.class));
factoryPerClass.put(String.class, new SetterFactory>>() {
@SuppressWarnings("unchecked")
@Override
public Setter getSetter(PropertyMapping, ?, DatastaxColumnKey, ? extends ColumnDefinition> arg) {
return (Setter) new StringSettableDataSetter(arg.getColumnKey().getIndex());
}
});
factoryPerClass.put(Date.class, new SetterFactory>>() {
@SuppressWarnings("unchecked")
@Override
public Setter getSetter(PropertyMapping, ?, DatastaxColumnKey, ? extends ColumnDefinition> arg) {
return (Setter) new DateSettableDataSetter(arg.getColumnKey().getIndex());
}
});
factoryPerClass.put(UUID.class, new SetterFactory>>() {
@SuppressWarnings("unchecked")
@Override
public Setter getSetter(PropertyMapping, ?, DatastaxColumnKey, ? extends ColumnDefinition> arg) {
return (Setter) new UUIDSettableDataSetter(arg.getColumnKey().getIndex());
}
});
factoryPerClass.put(BigDecimal.class, new SetterFactory>>() {
@SuppressWarnings("unchecked")
@Override
public Setter getSetter(PropertyMapping, ?, DatastaxColumnKey, ? extends ColumnDefinition> arg) {
return (Setter) new BigDecimalSettableDataSetter(arg.getColumnKey().getIndex());
}
});
factoryPerClass.put(BigInteger.class, new SetterFactory>>() {
@SuppressWarnings("unchecked")
@Override
public Setter getSetter(PropertyMapping, ?, DatastaxColumnKey, ? extends ColumnDefinition> arg) {
return (Setter) new BigIntegerSettableDataSetter(arg.getColumnKey().getIndex());
}
});
factoryPerClass.put(InetAddress.class, new SetterFactory>>() {
@SuppressWarnings("unchecked")
@Override
public Setter getSetter(PropertyMapping, ?, DatastaxColumnKey, ? extends ColumnDefinition> arg) {
return (Setter) new InetAddressSettableDataSetter(arg.getColumnKey().getIndex());
}
});
factoryPerClass.put(TupleValue.class, new SetterFactory>>() {
@SuppressWarnings("unchecked")
@Override
public Setter getSetter(PropertyMapping, ?, DatastaxColumnKey, ? extends ColumnDefinition> arg) {
return (Setter) new TupleValueSettableDataSetter(arg.getColumnKey().getIndex());
}
});
}
public SettableDataSetterFactory(MapperConfig> mapperConfig, ReflectionService reflectionService) {
this.mapperConfig = mapperConfig;
this.reflectionService = reflectionService;
}
@SuppressWarnings("unchecked")
@Override
public Setter getSetter(PropertyMapping, ?, DatastaxColumnKey, ? extends ColumnDefinition> arg) {
Setter setter = null;
Type propertyType = arg.getPropertyMeta().getPropertyType();
final DataType dataType = arg.getColumnKey().getDataType();
Type type = dataType != null ? dataType.asJavaClass() : null;
if (type == null) {
type = propertyType;
}
if (TypeHelper.isEnum(propertyType)) {
if (TypeHelper.isClass(type, String.class)) {
return (Setter) new StringEnumSettableDataSetter(arg.getColumnKey().getIndex());
} else {
return (Setter) new OrdinalEnumSettableDataSetter(arg.getColumnKey().getIndex());
}
}
SetterFactory>> setterFactory =
this.factoryPerClass.get(TypeHelper.toClass(type));
if (setterFactory != null) {
setter = setterFactory.getSetter(arg);
if (!TypeHelper.areEquals(TypeHelper.toBoxedClass(type), TypeHelper.toBoxedClass(propertyType))) {
Converter, ?> converter = getConverter(propertyType, TypeHelper.toClass(type), dataType, arg.getColumnDefinition());
if (converter != null) {
setter = (Setter) new ConvertDelegateSetter(setter, (Converter