use of org.simpleflatmapper.converter.ContextualConverter in project SimpleFlatMapper by arnaudroger.
the class AbstractMultiFormatContextualConverterFactory method newConverter.
@SuppressWarnings("unchecked")
@Override
public ContextualConverter<? super I, ? extends O> newConverter(ConvertingTypes targetedTypes, ContextFactoryBuilder contextFactoryBuilder, Object... params) {
DateTimeFormatter[] dateTimeFormatters = JodaTimeHelper.getDateTimeFormatters(params);
DateTimeZone zoneId = JodaTimeHelper.getDateTimeZoneOrDefault(params);
ContextualConverter<I, O>[] converters = new ContextualConverter[dateTimeFormatters.length];
for (int i = 0; i < dateTimeFormatters.length; i++) {
DateTimeFormatter dateTimeFormatter = dateTimeFormatters[i];
if (dateTimeFormatter.getZone() == null) {
dateTimeFormatter.withZone(zoneId);
}
converters[i] = newConverter(dateTimeFormatter);
}
if (converters.length == 1) {
return converters[0];
} else {
return new MultiDateTimeFormatterConverter<I, O>(converters);
}
}
use of org.simpleflatmapper.converter.ContextualConverter in project SimpleFlatMapper by arnaudroger.
the class AbstractMultiFormatContextualConverterFactory method newConverter.
@SuppressWarnings("unchecked")
@Override
public ContextualConverter<? super I, ? extends O> newConverter(ConvertingTypes targetedTypes, ContextFactoryBuilder contextFactoryBuilder, Object... params) {
DateTimeFormatter[] dateTimeFormatters = JavaTimeHelper.getDateTimeFormatters(params);
if (dateTimeFormatters.length == 0) {
return null;
}
ZoneId zoneId = JavaTimeHelper.getZoneId(params);
ContextualConverter<I, O>[] converters = new ContextualConverter[dateTimeFormatters.length];
for (int i = 0; i < dateTimeFormatters.length; i++) {
DateTimeFormatter dateTimeFormatter = dateTimeFormatters[i];
if (dateTimeFormatter.getZone() == null) {
dateTimeFormatter.withZone(zoneId);
}
converters[i] = newConverter(dateTimeFormatter);
}
if (converters.length == 1) {
return converters[0];
} else {
return new MultiDateTimeFormatterConverter<I, O>(converters);
}
}
use of org.simpleflatmapper.converter.ContextualConverter in project SimpleFlatMapper by arnaudroger.
the class SettableDataSetterFactory method getSetter.
@SuppressWarnings("unchecked")
@Override
public <P> Setter<SettableByIndexData, P> getSetter(PropertyMapping<?, ?, 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>> setterFactory = this.factoryPerClass.get(TypeHelper.toClass(type));
EmptyContextFactoryBuilder contextFactoryBuilder = EmptyContextFactoryBuilder.INSTANCE;
if (setterFactory != null) {
setter = setterFactory.getSetter(arg);
if (!TypeHelper.areEquals(TypeHelper.toBoxedClass(type), TypeHelper.toBoxedClass(propertyType))) {
ContextualConverter<?, ?> converter = getConverter(propertyType, TypeHelper.toClass(type), dataType, arg.getColumnDefinition(), contextFactoryBuilder);
if (converter != null) {
setter = (Setter<SettableByIndexData, P>) new ConvertDelegateSetter<SettableByIndexData, Object, P>(setter, (ContextualConverter<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 {
ContextualConverter<?, ?> converter = getConverter(lEltType, dEltType, dataTypeElt, arg.getColumnDefinition(), contextFactoryBuilder);
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 {
ContextualConverter<?, ?> converter = getConverter(lEltType, dEltType, dataTypeElt, arg.getColumnDefinition(), contextFactoryBuilder);
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(), contextFactoryBuilder), getConverter(keyValueTypeOfMap.getValueType(), DataTypeHelper.asJavaClass(dtValueType), dtValueType, arg.getColumnDefinition(), contextFactoryBuilder));
}
}
}
return setter;
}
use of org.simpleflatmapper.converter.ContextualConverter in project SimpleFlatMapper by arnaudroger.
the class ConstantSourceFieldMapperFactoryImpl method getGetterInstantiator.
private <T, P> ContextualGetter<? super S, ? extends P> getGetterInstantiator(ClassMeta<P> classMeta, InstantiatorDefinition id, K key, ColumnDefinition<K, ?> columnDefinition, Collection<Type> types, MappingContextFactoryBuilder<?, ? extends FieldKey<?>> mappingContextFactoryBuilder) {
Instantiator<? super T, ? extends P> instantiator = classMeta.getReflectionService().getInstantiatorFactory().getOneArgIdentityInstantiator(id, classMeta.getReflectionService().builderIgnoresNullValues());
final Type paramType = id.getParameters()[0].getGenericType();
ContextualGetter<? super S, ? extends T> subGetter = getterFactory.newGetter(paramType, key, mappingContextFactoryBuilder, columnDefinition);
if (subGetter == null) {
if (types.contains(paramType)) {
// loop circuit cutter
return null;
}
types.add(paramType);
// converter?
Type sourceType = key.getType(paramType);
ContextualConverter converter = converterService.findConverter(sourceType, paramType, mappingContextFactoryBuilder, columnDefinition.properties());
if (converter != null) {
ContextualGetter sourceTypeGetter = getterFactory.newGetter(sourceType, key, mappingContextFactoryBuilder, columnDefinition);
subGetter = new FieldMapperGetterWithConverter(converter, sourceTypeGetter);
} else {
subGetter = lookForInstantiatorGetter(classMeta.getReflectionService().<T>getClassMeta(paramType), key, columnDefinition, types, mappingContextFactoryBuilder);
}
}
if (subGetter != null) {
return new InstantiatorContextualGetter<T, S, P>(instantiator, subGetter);
} else
return null;
}
use of org.simpleflatmapper.converter.ContextualConverter in project SimpleFlatMapper by arnaudroger.
the class JdbcConverterFactoryProducer method produce.
@Override
public void produce(Consumer<? super ContextualConverterFactory<?, ?>> consumer) {
// IFJAVA8_START
constantConverter(consumer, Time.class, LocalTime.class, new TimeToLocalTimeConverter());
constantConverter(consumer, Date.class, LocalDate.class, new DateToLocalDateConverter());
factoryConverter(consumer, new AbstractContextualConverterFactory<Time, OffsetTime>(Time.class, OffsetTime.class) {
@Override
public ContextualConverter<Time, OffsetTime> newConverter(ConvertingTypes targetedTypes, ContextFactoryBuilder contextFactoryBuilder, Object... params) {
ZoneOffset zoneOffset = getZoneOffset(params);
return new TimeToOffsetTimeConverter(zoneOffset);
}
@SuppressWarnings("unchecked")
private ZoneOffset getZoneOffset(Object[] params) {
for (Object prop : params) {
if (prop instanceof ZoneOffset) {
return (ZoneOffset) prop;
} else if (SupplierHelper.isSupplierOf(prop, ZoneOffset.class)) {
return ((Supplier<ZoneOffset>) prop).get();
}
}
return ZoneOffset.UTC;
}
});
// IFJAVA8_END
factoryConverter(consumer, new AbstractContextualConverterFactory<Array, Object>(Array.class, java.lang.reflect.Array.class) {
@Override
public ContextualConverter<? super Array, ? extends Object> newConverter(ConvertingTypes targetedTypes, ContextFactoryBuilder contextFactoryBuilder, Object... params) {
Type elementType = TypeHelper.getComponentTypeOfListOrArray(targetedTypes.getTo());
Getter<? super ResultSet, ?> getter = ResultSetGetterFactory.INSTANCE.newGetter(elementType, new JdbcColumnKey("elt", 2), params);
return new SqlArrayToJavaArrayConverter<Object>(TypeHelper.<Object>toClass(elementType), getter);
}
@Override
public ConvertingScore score(ConvertingTypes targetedTypes) {
return new ConvertingScore(super.score(targetedTypes).getFromScore(), TypeHelper.isArray(targetedTypes.getTo()) ? 1 : -1);
}
});
factoryConverter(consumer, new AbstractContextualConverterFactory<Array, List>(Array.class, List.class) {
@Override
public ContextualConverter<? super Array, ? extends List> newConverter(ConvertingTypes targetedTypes, ContextFactoryBuilder contextFactoryBuilder, Object... params) {
Type elementType = TypeHelper.getComponentTypeOfListOrArray(targetedTypes.getTo());
Getter<? super ResultSet, ?> getter = ResultSetGetterFactory.INSTANCE.newGetter(elementType, new JdbcColumnKey("elt", 2), params);
return new SqlArrayToListConverter<Object>(getter);
}
});
constantConverter(consumer, Calendar.class, Timestamp.class, new CalendarToTimestampConverter());
constantConverter(consumer, java.util.Date.class, Timestamp.class, new UtilDateToTimestampConverter());
constantConverter(consumer, java.util.Date.class, Time.class, new UtilDateToTimeConverter());
constantConverter(consumer, java.util.Date.class, Date.class, new UtilDateToDateConverter());
constantConverter(consumer, Blob.class, byte[].class, new BlobToByteConverter());
}
Aggregations