use of org.simpleflatmapper.converter.ConvertingTypes in project SimpleFlatMapper by arnaudroger.
the class ConvertingTypesTest method testScoreExactMatch.
@Test
public void testScoreExactMatch() {
ConvertingTypes stringToDate = new ConvertingTypes(String.class, Date.class);
ConvertingTypes dateToString = new ConvertingTypes(Date.class, String.class);
assertEquals(256, stringToDate.score(stringToDate).getFromScore());
assertEquals(256, stringToDate.score(stringToDate).getToScore());
assertEquals(256 * 256 + 256, stringToDate.score(stringToDate).getScore());
assertEquals(256 * 256 + 256, dateToString.score(dateToString).getScore());
assertEquals(-1, stringToDate.score(dateToString).getScore());
assertEquals(-1, dateToString.score(stringToDate).getScore());
}
use of org.simpleflatmapper.converter.ConvertingTypes in project SimpleFlatMapper by arnaudroger.
the class ConvertingTypesTest method testCharSequenceEnum.
@Test
public void testCharSequenceEnum() {
ConvertingTypes csToEnum = new ConvertingTypes(CharSequence.class, Enum.class);
ConvertingTypes stringToEnum = new ConvertingTypes(Object.class, Long.class);
}
use of org.simpleflatmapper.converter.ConvertingTypes in project SimpleFlatMapper by arnaudroger.
the class ConvertingTypesTest method testTestScoreHierarchyFrom.
@Test
public void testTestScoreHierarchyFrom() {
ConvertingTypes objectToByte = new ConvertingTypes(Object.class, Byte.class);
ConvertingTypes numberToByte = new ConvertingTypes(Number.class, Byte.class);
ConvertingTypes longToByte = new ConvertingTypes(Long.class, Byte.class);
assertTrue(objectToByte.score(longToByte).getScore() > 0);
assertTrue(numberToByte.score(longToByte).getScore() > 0);
assertTrue(numberToByte.score(longToByte).getScore() < longToByte.score(longToByte).getScore());
assertTrue(objectToByte.score(longToByte).getScore() < numberToByte.score(longToByte).getScore());
}
use of org.simpleflatmapper.converter.ConvertingTypes in project SimpleFlatMapper by arnaudroger.
the class ConvertingTypesTest method testTestScoreHierarchyTo.
@Test
public void testTestScoreHierarchyTo() {
ConvertingTypes objectToNumber = new ConvertingTypes(Object.class, Number.class);
ConvertingTypes objectToLong = new ConvertingTypes(Object.class, Long.class);
ConvertingTypes objectToObject = new ConvertingTypes(Object.class, Object.class);
assertTrue(objectToObject.score(objectToNumber).getScore() < 0);
assertTrue(objectToNumber.score(objectToNumber).getScore() > 0);
assertTrue(objectToLong.score(objectToNumber).getScore() > 0);
assertTrue(objectToNumber.score(objectToNumber).getScore() > objectToNumber.score(objectToLong).getScore());
}
use of org.simpleflatmapper.converter.ConvertingTypes in project SimpleFlatMapper by arnaudroger.
the class JdbcConverterFactoryProducer method produce.
@Override
public void produce(Consumer<? super ConverterFactory<?, ?>> consumer) {
// IFJAVA8_START
constantConverter(consumer, Time.class, LocalTime.class, new TimeToLocalTimeConverter());
constantConverter(consumer, Date.class, LocalDate.class, new DateToLocalDateConverter());
factoryConverter(consumer, new AbstractConverterFactory<Time, OffsetTime>(Time.class, OffsetTime.class) {
@Override
public Converter<Time, OffsetTime> newConverter(ConvertingTypes targetedTypes, 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 AbstractConverterFactory<Array, Object>(Array.class, java.lang.reflect.Array.class) {
@Override
public Converter<? super Array, ? extends Object> newConverter(ConvertingTypes targetedTypes, 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 AbstractConverterFactory<Array, List>(Array.class, List.class) {
@Override
public Converter<? super Array, ? extends List> newConverter(ConvertingTypes targetedTypes, 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());
}
Aggregations