org.simpleflatmapper.datastax.impl.SettableDataSetterFactory Maven / Gradle / Ivy
Show all versions of sfm-datastax Show documentation
package org.simpleflatmapper.datastax.impl;
import com.datastax.driver.core.*;
import org.simpleflatmapper.converter.ConverterService;
import org.simpleflatmapper.datastax.DataTypeHelper;
import org.simpleflatmapper.datastax.DatastaxColumnKey;
import org.simpleflatmapper.map.Mapper;
import org.simpleflatmapper.map.MapperConfig;
import org.simpleflatmapper.map.property.FieldMapperColumnDefinition;
import org.simpleflatmapper.map.mapper.ColumnDefinition;
import org.simpleflatmapper.map.mapper.PropertyMapping;
import org.simpleflatmapper.reflect.setter.ConvertDelegateSetter;
import org.simpleflatmapper.reflect.ReflectionService;
import org.simpleflatmapper.reflect.Setter;
import org.simpleflatmapper.reflect.SetterFactory;
import org.simpleflatmapper.converter.Converter;
import org.simpleflatmapper.datastax.impl.setter.BigDecimalSettableDataSetter;
import org.simpleflatmapper.datastax.impl.setter.BigIntegerSettableDataSetter;
import org.simpleflatmapper.datastax.impl.setter.ByteSettableDataSetter;
import org.simpleflatmapper.datastax.impl.setter.ConverterToTupleValueMapper;
import org.simpleflatmapper.datastax.impl.setter.ConverterToUDTValueMapper;
import org.simpleflatmapper.datastax.impl.setter.DateSettableDataSetter;
import org.simpleflatmapper.datastax.impl.setter.DoubleSettableDataSetter;
import org.simpleflatmapper.datastax.impl.setter.FloatSettableDataSetter;
import org.simpleflatmapper.datastax.impl.setter.InetAddressSettableDataSetter;
import org.simpleflatmapper.datastax.impl.setter.IntSettableDataSetter;
import org.simpleflatmapper.datastax.impl.setter.ListSettableDataSetter;
import org.simpleflatmapper.datastax.impl.setter.ListWithConverterSettableDataSetter;
import org.simpleflatmapper.datastax.impl.setter.LongSettableDataSetter;
import org.simpleflatmapper.datastax.impl.setter.MapSettableDataSetter;
import org.simpleflatmapper.datastax.impl.setter.MapWithConverterSettableDataSetter;
import org.simpleflatmapper.datastax.impl.setter.OrdinalEnumSettableDataSetter;
import org.simpleflatmapper.datastax.impl.setter.SetSettableDataSetter;
import org.simpleflatmapper.datastax.impl.setter.SetWithConverterSettableDataSetter;
import org.simpleflatmapper.datastax.impl.setter.ShortSettableDataSetter;
import org.simpleflatmapper.datastax.impl.setter.StringEnumSettableDataSetter;
import org.simpleflatmapper.datastax.impl.setter.StringSettableDataSetter;
import org.simpleflatmapper.datastax.impl.setter.TimeSettableDataSetter;
import org.simpleflatmapper.datastax.impl.setter.TimestampSettableDataSetter;
import org.simpleflatmapper.datastax.impl.setter.TupleValueSettableDataSetter;
import org.simpleflatmapper.datastax.impl.setter.UDTObjectSettableDataSetter;
import org.simpleflatmapper.datastax.impl.setter.UDTValueSettableDataSetter;
import org.simpleflatmapper.datastax.impl.setter.UUIDSettableDataSetter;
import org.simpleflatmapper.util.TypeHelper;
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(short.class, new SetterFactory>>() {
@SuppressWarnings("unchecked")
@Override
public Setter getSetter(PropertyMapping, ?, DatastaxColumnKey, ? extends ColumnDefinition> arg) {
return (Setter) new ShortSettableDataSetter(arg.getColumnKey().getIndex());
}
});
factoryPerClass.put(Short.class, factoryPerClass.get(short.class));
factoryPerClass.put(byte.class, new SetterFactory>>() {
@SuppressWarnings("unchecked")
@Override
public Setter getSetter(PropertyMapping, ?, DatastaxColumnKey, ? extends ColumnDefinition> arg) {
return (Setter) new ByteSettableDataSetter(arg.getColumnKey().getIndex());
}
});
factoryPerClass.put(Byte.class, factoryPerClass.get(byte.class));
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) {
if (DataTypeHelper.isTime(arg.getColumnKey().getDataType().getName())) {
return (Setter)new TimeSettableDataSetter(arg.getColumnKey().getIndex());
}
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 TimestampSettableDataSetter(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());
}
});
if (DataTypeHelper.localDateClass != null) {
factoryPerClass.put(DataTypeHelper.localDateClass, new SetterFactory>>() {
@SuppressWarnings("unchecked")
@Override
public Setter getSetter(PropertyMapping, ?, DatastaxColumnKey, ? extends ColumnDefinition> arg) {
return (Setter) new DateSettableDataSetter(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 ? DataTypeHelper.asJavaClass(dataType) : null;
if (type == null) {
type = propertyType;
}
if (TypeHelper.isEnum(propertyType)) {
if (TypeHelper.areEquals(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