Search in sources :

Example 1 with ConvertingTypes

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());
}
Also used : ConvertingTypes(org.simpleflatmapper.converter.ConvertingTypes) Test(org.junit.Test)

Example 2 with ConvertingTypes

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);
}
Also used : ConvertingTypes(org.simpleflatmapper.converter.ConvertingTypes) Test(org.junit.Test)

Example 3 with ConvertingTypes

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());
}
Also used : ConvertingTypes(org.simpleflatmapper.converter.ConvertingTypes) Test(org.junit.Test)

Example 4 with ConvertingTypes

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());
}
Also used : ConvertingTypes(org.simpleflatmapper.converter.ConvertingTypes) Test(org.junit.Test)

Example 5 with ConvertingTypes

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());
}
Also used : Getter(org.simpleflatmapper.reflect.Getter) Time(java.sql.Time) LocalTime(java.time.LocalTime) OffsetTime(java.time.OffsetTime) ZoneOffset(java.time.ZoneOffset) TimeToLocalTimeConverter(org.simpleflatmapper.jdbc.converter.time.TimeToLocalTimeConverter) ConvertingTypes(org.simpleflatmapper.converter.ConvertingTypes) OffsetTime(java.time.OffsetTime) ResultSet(java.sql.ResultSet) TimeToLocalTimeConverter(org.simpleflatmapper.jdbc.converter.time.TimeToLocalTimeConverter) Converter(org.simpleflatmapper.converter.Converter) TimeToOffsetTimeConverter(org.simpleflatmapper.jdbc.converter.time.TimeToOffsetTimeConverter) DateToLocalDateConverter(org.simpleflatmapper.jdbc.converter.time.DateToLocalDateConverter) TimeToOffsetTimeConverter(org.simpleflatmapper.jdbc.converter.time.TimeToOffsetTimeConverter) Supplier(org.simpleflatmapper.util.Supplier) List(java.util.List) ConvertingScore(org.simpleflatmapper.converter.ConvertingScore) Array(java.sql.Array) JdbcColumnKey(org.simpleflatmapper.jdbc.JdbcColumnKey) Type(java.lang.reflect.Type) DateToLocalDateConverter(org.simpleflatmapper.jdbc.converter.time.DateToLocalDateConverter)

Aggregations

ConvertingTypes (org.simpleflatmapper.converter.ConvertingTypes)7 Test (org.junit.Test)4 Converter (org.simpleflatmapper.converter.Converter)3 Date (java.util.Date)2 ConvertingScore (org.simpleflatmapper.converter.ConvertingScore)2 ToStringConverter (org.simpleflatmapper.converter.ToStringConverter)2 Type (java.lang.reflect.Type)1 Array (java.sql.Array)1 ResultSet (java.sql.ResultSet)1 Time (java.sql.Time)1 LocalTime (java.time.LocalTime)1 OffsetTime (java.time.OffsetTime)1 ZoneOffset (java.time.ZoneOffset)1 List (java.util.List)1 DateTime (org.joda.time.DateTime)1 Instant (org.joda.time.Instant)1 LocalDate (org.joda.time.LocalDate)1 LocalDateTime (org.joda.time.LocalDateTime)1 LocalTime (org.joda.time.LocalTime)1 ReadableInstant (org.joda.time.ReadableInstant)1