Search in sources :

Example 1 with ConvertDelegateSetter

use of org.simpleflatmapper.reflect.setter.ConvertDelegateSetter in project SimpleFlatMapper by arnaudroger.

the class SettableDataSetterFactory method getSetter.

@SuppressWarnings("unchecked")
@Override
public <P> Setter<SettableByIndexData, P> getSetter(PropertyMapping<?, ?, DatastaxColumnKey, ? extends ColumnDefinition<DatastaxColumnKey, ?>> arg) {
    Setter<SettableByIndexData, P> 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<SettableByIndexData, P>) new StringEnumSettableDataSetter(arg.getColumnKey().getIndex());
        } else {
            return (Setter<SettableByIndexData, P>) new OrdinalEnumSettableDataSetter(arg.getColumnKey().getIndex());
        }
    }
    SetterFactory<SettableByIndexData, PropertyMapping<?, ?, DatastaxColumnKey, ? extends ColumnDefinition<DatastaxColumnKey, ?>>> 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<SettableByIndexData, P>) new ConvertDelegateSetter<SettableByIndexData, Object, P>(setter, (Converter<Object, P>) converter);
            } else {
                setter = null;
            }
        }
    }
    if (setter == null && dataType != null) {
        if (dataType instanceof UserType) {
            if (propertyType.equals(UDTValue.class)) {
                setter = (Setter<SettableByIndexData, P>) new UDTValueSettableDataSetter(arg.getColumnKey().getIndex());
            } else {
                setter = (Setter<SettableByIndexData, P>) UDTObjectSettableDataSetter.newInstance(propertyType, (UserType) dataType, arg.getColumnKey().getIndex(), mapperConfig, reflectionService);
            }
        } else if (TypeHelper.isAssignable(List.class, type) && TypeHelper.isAssignable(List.class, propertyType)) {
            DataType dataTypeElt = dataType.getTypeArguments().get(0);
            Class<?> dEltType = DataTypeHelper.asJavaClass(dataTypeElt);
            Type lEltType = TypeHelper.getComponentTypeOfListOrArray(propertyType);
            if (TypeHelper.areEquals(lEltType, dEltType)) {
                setter = new ListSettableDataSetter(arg.getColumnKey().getIndex());
            } else {
                Converter<?, ?> converter = getConverter(lEltType, dEltType, dataTypeElt, arg.getColumnDefinition());
                if (converter != null) {
                    setter = new ListWithConverterSettableDataSetter(arg.getColumnKey().getIndex(), converter);
                }
            }
        } else if (TypeHelper.isAssignable(Set.class, type) && TypeHelper.isAssignable(Set.class, propertyType)) {
            DataType dataTypeElt = dataType.getTypeArguments().get(0);
            Class<?> dEltType = DataTypeHelper.asJavaClass(dataTypeElt);
            Type lEltType = TypeHelper.getComponentTypeOfListOrArray(propertyType);
            if (TypeHelper.areEquals(lEltType, dEltType)) {
                setter = new SetSettableDataSetter(arg.getColumnKey().getIndex());
            } else {
                Converter<?, ?> converter = getConverter(lEltType, dEltType, dataTypeElt, arg.getColumnDefinition());
                if (converter != null) {
                    setter = new SetWithConverterSettableDataSetter(arg.getColumnKey().getIndex(), converter);
                }
            }
        } else if (TypeHelper.isAssignable(Map.class, type) && TypeHelper.isAssignable(Map.class, propertyType)) {
            DataType dtKeyType = dataType.getTypeArguments().get(0);
            DataType dtValueType = dataType.getTypeArguments().get(1);
            TypeHelper.MapEntryTypes keyValueTypeOfMap = TypeHelper.getKeyValueTypeOfMap(propertyType);
            if (areSame(dtKeyType, keyValueTypeOfMap.getKeyType()) && areSame(dtValueType, keyValueTypeOfMap.getValueType())) {
                setter = new MapSettableDataSetter(arg.getColumnKey().getIndex());
            } else {
                setter = new MapWithConverterSettableDataSetter(arg.getColumnKey().getIndex(), getConverter(keyValueTypeOfMap.getKeyType(), DataTypeHelper.asJavaClass(dtKeyType), dtKeyType, arg.getColumnDefinition()), getConverter(keyValueTypeOfMap.getValueType(), DataTypeHelper.asJavaClass(dtValueType), dtValueType, arg.getColumnDefinition()));
            }
        }
    }
    return setter;
}
Also used : OrdinalEnumSettableDataSetter(org.simpleflatmapper.datastax.impl.setter.OrdinalEnumSettableDataSetter) SetSettableDataSetter(org.simpleflatmapper.datastax.impl.setter.SetSettableDataSetter) ConvertDelegateSetter(org.simpleflatmapper.reflect.setter.ConvertDelegateSetter) ListWithConverterSettableDataSetter(org.simpleflatmapper.datastax.impl.setter.ListWithConverterSettableDataSetter) StringEnumSettableDataSetter(org.simpleflatmapper.datastax.impl.setter.StringEnumSettableDataSetter) MapWithConverterSettableDataSetter(org.simpleflatmapper.datastax.impl.setter.MapWithConverterSettableDataSetter) Converter(org.simpleflatmapper.converter.Converter) ListSettableDataSetter(org.simpleflatmapper.datastax.impl.setter.ListSettableDataSetter) UDTValueSettableDataSetter(org.simpleflatmapper.datastax.impl.setter.UDTValueSettableDataSetter) MapSettableDataSetter(org.simpleflatmapper.datastax.impl.setter.MapSettableDataSetter) FieldMapperColumnDefinition(org.simpleflatmapper.map.property.FieldMapperColumnDefinition) ColumnDefinition(org.simpleflatmapper.map.mapper.ColumnDefinition) Type(java.lang.reflect.Type) ListSettableDataSetter(org.simpleflatmapper.datastax.impl.setter.ListSettableDataSetter) DoubleSettableDataSetter(org.simpleflatmapper.datastax.impl.setter.DoubleSettableDataSetter) BigDecimalSettableDataSetter(org.simpleflatmapper.datastax.impl.setter.BigDecimalSettableDataSetter) TimeSettableDataSetter(org.simpleflatmapper.datastax.impl.setter.TimeSettableDataSetter) BigIntegerSettableDataSetter(org.simpleflatmapper.datastax.impl.setter.BigIntegerSettableDataSetter) SetWithConverterSettableDataSetter(org.simpleflatmapper.datastax.impl.setter.SetWithConverterSettableDataSetter) IntSettableDataSetter(org.simpleflatmapper.datastax.impl.setter.IntSettableDataSetter) UUIDSettableDataSetter(org.simpleflatmapper.datastax.impl.setter.UUIDSettableDataSetter) TupleValueSettableDataSetter(org.simpleflatmapper.datastax.impl.setter.TupleValueSettableDataSetter) SetSettableDataSetter(org.simpleflatmapper.datastax.impl.setter.SetSettableDataSetter) InetAddressSettableDataSetter(org.simpleflatmapper.datastax.impl.setter.InetAddressSettableDataSetter) ListWithConverterSettableDataSetter(org.simpleflatmapper.datastax.impl.setter.ListWithConverterSettableDataSetter) ShortSettableDataSetter(org.simpleflatmapper.datastax.impl.setter.ShortSettableDataSetter) UDTObjectSettableDataSetter(org.simpleflatmapper.datastax.impl.setter.UDTObjectSettableDataSetter) DateSettableDataSetter(org.simpleflatmapper.datastax.impl.setter.DateSettableDataSetter) TimestampSettableDataSetter(org.simpleflatmapper.datastax.impl.setter.TimestampSettableDataSetter) MapWithConverterSettableDataSetter(org.simpleflatmapper.datastax.impl.setter.MapWithConverterSettableDataSetter) FloatSettableDataSetter(org.simpleflatmapper.datastax.impl.setter.FloatSettableDataSetter) StringEnumSettableDataSetter(org.simpleflatmapper.datastax.impl.setter.StringEnumSettableDataSetter) LongSettableDataSetter(org.simpleflatmapper.datastax.impl.setter.LongSettableDataSetter) OrdinalEnumSettableDataSetter(org.simpleflatmapper.datastax.impl.setter.OrdinalEnumSettableDataSetter) Setter(org.simpleflatmapper.reflect.Setter) ByteSettableDataSetter(org.simpleflatmapper.datastax.impl.setter.ByteSettableDataSetter) MapSettableDataSetter(org.simpleflatmapper.datastax.impl.setter.MapSettableDataSetter) ConvertDelegateSetter(org.simpleflatmapper.reflect.setter.ConvertDelegateSetter) StringSettableDataSetter(org.simpleflatmapper.datastax.impl.setter.StringSettableDataSetter) UDTValueSettableDataSetter(org.simpleflatmapper.datastax.impl.setter.UDTValueSettableDataSetter) PropertyMapping(org.simpleflatmapper.map.mapper.PropertyMapping) SetWithConverterSettableDataSetter(org.simpleflatmapper.datastax.impl.setter.SetWithConverterSettableDataSetter)

Example 2 with ConvertDelegateSetter

use of org.simpleflatmapper.reflect.setter.ConvertDelegateSetter in project SimpleFlatMapper by arnaudroger.

the class ConvertDelegateSetterTest method testSet.

@Test
public void testSet() throws Exception {
    ConvertDelegateSetter<DbObject, UUID, String> setter = new ConvertDelegateSetter<DbObject, UUID, String>(new Setter<DbObject, String>() {

        @Override
        public void set(DbObject target, String value) throws Exception {
            target.setName(value);
        }
    }, new Converter<UUID, String>() {

        @Override
        public String convert(UUID in) throws Exception {
            return in.toString();
        }
    });
    DbObject o = new DbObject();
    UUID uuid = UUID.randomUUID();
    setter.set(o, uuid);
    assertEquals(uuid.toString(), o.getName());
}
Also used : ConvertDelegateSetter(org.simpleflatmapper.reflect.setter.ConvertDelegateSetter) DbObject(org.simpleflatmapper.test.beans.DbObject) UUID(java.util.UUID) Test(org.junit.Test)

Aggregations

ConvertDelegateSetter (org.simpleflatmapper.reflect.setter.ConvertDelegateSetter)2 Type (java.lang.reflect.Type)1 UUID (java.util.UUID)1 Test (org.junit.Test)1 Converter (org.simpleflatmapper.converter.Converter)1 BigDecimalSettableDataSetter (org.simpleflatmapper.datastax.impl.setter.BigDecimalSettableDataSetter)1 BigIntegerSettableDataSetter (org.simpleflatmapper.datastax.impl.setter.BigIntegerSettableDataSetter)1 ByteSettableDataSetter (org.simpleflatmapper.datastax.impl.setter.ByteSettableDataSetter)1 DateSettableDataSetter (org.simpleflatmapper.datastax.impl.setter.DateSettableDataSetter)1 DoubleSettableDataSetter (org.simpleflatmapper.datastax.impl.setter.DoubleSettableDataSetter)1 FloatSettableDataSetter (org.simpleflatmapper.datastax.impl.setter.FloatSettableDataSetter)1 InetAddressSettableDataSetter (org.simpleflatmapper.datastax.impl.setter.InetAddressSettableDataSetter)1 IntSettableDataSetter (org.simpleflatmapper.datastax.impl.setter.IntSettableDataSetter)1 ListSettableDataSetter (org.simpleflatmapper.datastax.impl.setter.ListSettableDataSetter)1 ListWithConverterSettableDataSetter (org.simpleflatmapper.datastax.impl.setter.ListWithConverterSettableDataSetter)1 LongSettableDataSetter (org.simpleflatmapper.datastax.impl.setter.LongSettableDataSetter)1 MapSettableDataSetter (org.simpleflatmapper.datastax.impl.setter.MapSettableDataSetter)1 MapWithConverterSettableDataSetter (org.simpleflatmapper.datastax.impl.setter.MapWithConverterSettableDataSetter)1 OrdinalEnumSettableDataSetter (org.simpleflatmapper.datastax.impl.setter.OrdinalEnumSettableDataSetter)1 SetSettableDataSetter (org.simpleflatmapper.datastax.impl.setter.SetSettableDataSetter)1