Search in sources :

Example 6 with Context

use of org.simpleflatmapper.converter.Context in project SimpleFlatMapper by arnaudroger.

the class JodaTimeConverterFactoryProducer method produce.

@Override
public void produce(Consumer<? super ContextualConverterFactory<?, ?>> consumer) {
    // Date to joda time
    factoryConverter(consumer, new AbstractContextualConverterFactory<Date, DateTime>(Date.class, DateTime.class) {

        @Override
        public ContextualConverter<Date, DateTime> newConverter(ConvertingTypes targetedTypes, ContextFactoryBuilder contextFactoryBuilder, Object... params) {
            return new DateToJodaDateTimeConverter(JodaTimeHelper.getDateTimeZoneOrDefault(params));
        }
    });
    constantConverter(consumer, Date.class, Instant.class, new DateToJodaInstantConverter());
    constantConverter(consumer, Date.class, LocalDate.class, new DateToJodaLocalDateConverter());
    constantConverter(consumer, Date.class, LocalDateTime.class, new DateToJodaLocalDateTimeConverter());
    constantConverter(consumer, Date.class, LocalTime.class, new DateToJodaLocalTimeConverter());
    // joda time to date
    constantConverter(consumer, DateTime.class, Date.class, new JodaDateTimeTojuDateConverter());
    constantConverter(consumer, Instant.class, Date.class, new JodaInstantTojuDateConverter());
    constantConverter(consumer, LocalDate.class, Date.class, new JodaLocalDateTojuDateConverter());
    factoryConverter(consumer, new AbstractContextualConverterFactory<LocalDateTime, Date>(LocalDateTime.class, Date.class) {

        @Override
        public ContextualConverter<LocalDateTime, Date> newConverter(ConvertingTypes targetedTypes, ContextFactoryBuilder contextFactoryBuilder, Object... params) {
            return new JodaLocalDateTimeTojuDateConverter(JodaTimeHelper.getDateTimeZoneOrDefault(params));
        }
    });
    factoryConverter(consumer, new AbstractContextualConverterFactory<LocalTime, Date>(LocalTime.class, Date.class) {

        @Override
        public ContextualConverter<LocalTime, Date> newConverter(ConvertingTypes targetedTypes, ContextFactoryBuilder contextFactoryBuilder, Object... params) {
            return new JodaLocalTimeTojuDateConverter(JodaTimeHelper.getDateTimeZoneOrDefault(params));
        }
    });
    // char sequence to joda time
    factoryConverter(consumer, new AbstractMultiFormatContextualConverterFactory<CharSequence, DateTime>(CharSequence.class, DateTime.class) {

        @Override
        protected ContextualConverter<CharSequence, DateTime> newConverter(DateTimeFormatter formatter) {
            return new CharSequenceToJodaDateTimeConverter(formatter);
        }
    });
    factoryConverter(consumer, new AbstractMultiFormatContextualConverterFactory<CharSequence, Instant>(CharSequence.class, Instant.class) {

        @Override
        protected ContextualConverter<CharSequence, Instant> newConverter(DateTimeFormatter formatter) {
            return new CharSequenceToJodaInstantConverter(formatter);
        }
    });
    factoryConverter(consumer, new AbstractMultiFormatContextualConverterFactory<CharSequence, LocalDate>(CharSequence.class, LocalDate.class) {

        @Override
        protected ContextualConverter<CharSequence, LocalDate> newConverter(DateTimeFormatter formatter) {
            return new CharSequenceToJodaLocalDateConverter(formatter);
        }
    });
    factoryConverter(consumer, new AbstractMultiFormatContextualConverterFactory<CharSequence, LocalDateTime>(CharSequence.class, LocalDateTime.class) {

        @Override
        protected ContextualConverter<CharSequence, LocalDateTime> newConverter(DateTimeFormatter formatter) {
            return new CharSequenceToJodaLocalDateTimeConverter(formatter);
        }
    });
    factoryConverter(consumer, new AbstractMultiFormatContextualConverterFactory<CharSequence, LocalTime>(CharSequence.class, LocalTime.class) {

        @Override
        protected ContextualConverter<CharSequence, LocalTime> newConverter(DateTimeFormatter formatter) {
            return new CharSequenceToJodaLocalTimeConverter(formatter);
        }
    });
    factoryConverter(consumer, new AbstractContextualConverterFactory<ReadableInstant, String>(ReadableInstant.class, String.class) {

        @Override
        public ContextualConverter<? super ReadableInstant, String> newConverter(ConvertingTypes targetedTypes, ContextFactoryBuilder contextFactoryBuilder, Object... params) {
            DateTimeFormatter dateTimeFormatter = JodaTimeHelper.getDateTimeFormatter(params);
            if (dateTimeFormatter != null) {
                return new JodaReadableInstantToStringConverter(dateTimeFormatter);
            } else {
                return ToStringConverter.INSTANCE;
            }
        }
    });
    factoryConverter(consumer, new AbstractContextualConverterFactory<ReadablePartial, String>(ReadablePartial.class, String.class) {

        @Override
        public ContextualConverter<? super ReadablePartial, String> newConverter(ConvertingTypes targetedTypes, ContextFactoryBuilder contextFactoryBuilder, Object... params) {
            DateTimeFormatter dateTimeFormatter = JodaTimeHelper.getDateTimeFormatter(params);
            if (dateTimeFormatter != null) {
                return new JodaReadablePartialToStringConverter(dateTimeFormatter);
            } else {
                return ToStringConverter.INSTANCE;
            }
        }
    });
    // IFJAVA8_START
    constantConverter(consumer, LocalDateTime.class, java.time.LocalDateTime.class, new org.simpleflatmapper.converter.joda.impl.time.JodaLocalDateTimeTojuLocalDateTimeConverter());
    constantConverter(consumer, LocalDate.class, java.time.LocalDate.class, new org.simpleflatmapper.converter.joda.impl.time.JodaLocalDateTojuLocalDateConverter());
    constantConverter(consumer, LocalTime.class, java.time.LocalTime.class, new org.simpleflatmapper.converter.joda.impl.time.JodaLocalTimeTojuLocalTimeConverter());
    // IFJAVA8_END
    constantConverter(consumer, LocalTime.class, Long.class, new ContextualConverter<LocalTime, Long>() {

        @Override
        public Long convert(LocalTime in, Context context) throws Exception {
            if (in == null)
                return null;
            return TimeUnit.MILLISECONDS.toNanos(in.getMillisOfDay());
        }
    });
}
Also used : DateToJodaDateTimeConverter(org.simpleflatmapper.converter.joda.impl.DateToJodaDateTimeConverter) DateToJodaLocalDateTimeConverter(org.simpleflatmapper.converter.joda.impl.DateToJodaLocalDateTimeConverter) JodaInstantTojuDateConverter(org.simpleflatmapper.converter.joda.impl.JodaInstantTojuDateConverter) ReadableInstant(org.joda.time.ReadableInstant) LocalDate(org.joda.time.LocalDate) DateTime(org.joda.time.DateTime) LocalDateTime(org.joda.time.LocalDateTime) ContextFactoryBuilder(org.simpleflatmapper.converter.ContextFactoryBuilder) ReadablePartial(org.joda.time.ReadablePartial) JodaLocalTimeTojuDateConverter(org.simpleflatmapper.converter.joda.impl.JodaLocalTimeTojuDateConverter) DateToJodaLocalDateConverter(org.simpleflatmapper.converter.joda.impl.DateToJodaLocalDateConverter) LocalTime(org.joda.time.LocalTime) ReadableInstant(org.joda.time.ReadableInstant) Instant(org.joda.time.Instant) CharSequenceToJodaLocalDateConverter(org.simpleflatmapper.converter.joda.impl.CharSequenceToJodaLocalDateConverter) JodaLocalDateTimeTojuDateConverter(org.simpleflatmapper.converter.joda.impl.JodaLocalDateTimeTojuDateConverter) DateToJodaInstantConverter(org.simpleflatmapper.converter.joda.impl.DateToJodaInstantConverter) DateTimeFormatter(org.joda.time.format.DateTimeFormatter) LocalDateTime(org.joda.time.LocalDateTime) JodaReadablePartialToStringConverter(org.simpleflatmapper.converter.joda.impl.JodaReadablePartialToStringConverter) JodaReadableInstantToStringConverter(org.simpleflatmapper.converter.joda.impl.JodaReadableInstantToStringConverter) ConvertingTypes(org.simpleflatmapper.converter.ConvertingTypes) CharSequenceToJodaDateTimeConverter(org.simpleflatmapper.converter.joda.impl.CharSequenceToJodaDateTimeConverter) JodaLocalDateTojuDateConverter(org.simpleflatmapper.converter.joda.impl.JodaLocalDateTojuDateConverter) JodaDateTimeTojuDateConverter(org.simpleflatmapper.converter.joda.impl.JodaDateTimeTojuDateConverter) CharSequenceToJodaInstantConverter(org.simpleflatmapper.converter.joda.impl.CharSequenceToJodaInstantConverter) Context(org.simpleflatmapper.converter.Context) CharSequenceToJodaLocalTimeConverter(org.simpleflatmapper.converter.joda.impl.CharSequenceToJodaLocalTimeConverter) ContextualConverter(org.simpleflatmapper.converter.ContextualConverter) Date(java.util.Date) LocalDate(org.joda.time.LocalDate) CharSequenceToJodaLocalDateTimeConverter(org.simpleflatmapper.converter.joda.impl.CharSequenceToJodaLocalDateTimeConverter) DateToJodaLocalTimeConverter(org.simpleflatmapper.converter.joda.impl.DateToJodaLocalTimeConverter)

Example 7 with Context

use of org.simpleflatmapper.converter.Context in project SimpleFlatMapper by arnaudroger.

the class CsvMapperImplTest method testCsvFieldMappingError.

@Test
public void testCsvFieldMappingError() throws IOException {
    CsvMapper<Integer> mapper = CsvMapperFactory.newInstance().newMapper(Integer.class);
    final Iterator<Integer> iterator = mapper.iterator(new StringReader("value\n1\n2\nnnn"));
    assertEquals(1, iterator.next().intValue());
    assertEquals(2, iterator.next().intValue());
    try {
        iterator.next();
    } catch (NumberFormatException e) {
        System.out.println(e.toString());
    }
    CsvMapper<Integer> csvMapper = CsvMapperFactory.newInstance().fieldMapperErrorHandler(new FieldMapperErrorHandler<CsvColumnKey>() {

        @Override
        public void errorMappingField(CsvColumnKey key, Object source, Object target, Exception error, Context mappingContext) throws MappingException {
            System.out.println(error.toString());
        }
    }).newMapper(Integer.class);
    Integer next = csvMapper.iterator(new StringReader("val\nnnnn")).next();
    System.out.println("next = " + next);
}
Also used : BigInteger(java.math.BigInteger) Context(org.simpleflatmapper.converter.Context) StringReader(java.io.StringReader) CsvColumnKey(org.simpleflatmapper.csv.CsvColumnKey) DbObject(org.simpleflatmapper.test.beans.DbObject) FieldMapperErrorHandler(org.simpleflatmapper.map.FieldMapperErrorHandler) MappingException(org.simpleflatmapper.map.MappingException) ParseException(java.text.ParseException) IOException(java.io.IOException) UnsupportedEncodingException(java.io.UnsupportedEncodingException) Test(org.junit.Test)

Example 8 with Context

use of org.simpleflatmapper.converter.Context in project SimpleFlatMapper by arnaudroger.

the class CsvMapperImplTest method testConverterProperty.

@Test
public void testConverterProperty() throws IOException {
    DbObject d = CsvParser.mapWith(CsvMapperFactory.newInstance().addColumnProperty("typeName", ConverterProperty.of(new ContextualConverter<String, DbObject.Type>() {

        @Override
        public DbObject.Type convert(String in, Context context) throws Exception {
            return DbObject.Type.shortForm(in);
        }
    })).newMapper(DbObject.class)).iterator("typeName\nt1").next();
    assertEquals(d.getTypeName(), DbObject.Type.type1);
}
Also used : Context(org.simpleflatmapper.converter.Context) DbObject(org.simpleflatmapper.test.beans.DbObject) ContextualConverter(org.simpleflatmapper.converter.ContextualConverter) Test(org.junit.Test)

Example 9 with Context

use of org.simpleflatmapper.converter.Context in project SimpleFlatMapper by arnaudroger.

the class JooqConverterFactoryProducer method produce.

@Override
public void produce(Consumer<? super ContextualConverterFactory<?, ?>> consumer) {
    this.constantConverter(consumer, byte.class, UByte.class, new ContextualConverter<Byte, UByte>() {

        @Override
        public UByte convert(Byte in, Context context) throws Exception {
            if (in == null) {
                return null;
            }
            return UByte.valueOf(in);
        }
    });
    this.constantConverter(consumer, short.class, UByte.class, new ContextualConverter<Short, UByte>() {

        @Override
        public UByte convert(Short in, Context context) throws Exception {
            if (in == null) {
                return null;
            }
            return UByte.valueOf(in);
        }
    });
    this.constantConverter(consumer, int.class, UByte.class, new ContextualConverter<Integer, UByte>() {

        @Override
        public UByte convert(Integer in, Context context) throws Exception {
            if (in == null) {
                return null;
            }
            return UByte.valueOf(in);
        }
    });
    this.constantConverter(consumer, long.class, UByte.class, new ContextualConverter<Long, UByte>() {

        @Override
        public UByte convert(Long in, Context context) throws Exception {
            if (in == null) {
                return null;
            }
            return UByte.valueOf(in);
        }
    });
    this.constantConverter(consumer, short.class, UShort.class, new ContextualConverter<Short, UShort>() {

        @Override
        public UShort convert(Short in, Context context) throws Exception {
            if (in == null) {
                return null;
            }
            return UShort.valueOf(in);
        }
    });
    this.constantConverter(consumer, int.class, UShort.class, new ContextualConverter<Integer, UShort>() {

        @Override
        public UShort convert(Integer in, Context context) throws Exception {
            if (in == null) {
                return null;
            }
            return UShort.valueOf(in);
        }
    });
    this.constantConverter(consumer, int.class, UInteger.class, new ContextualConverter<Integer, UInteger>() {

        @Override
        public UInteger convert(Integer in, Context context) throws Exception {
            if (in == null) {
                return null;
            }
            return UInteger.valueOf(in);
        }
    });
    this.constantConverter(consumer, long.class, UInteger.class, new ContextualConverter<Long, UInteger>() {

        @Override
        public UInteger convert(Long in, Context context) throws Exception {
            if (in == null) {
                return null;
            }
            return UInteger.valueOf(in);
        }
    });
    this.constantConverter(consumer, long.class, ULong.class, new ContextualConverter<Long, ULong>() {

        @Override
        public ULong convert(Long in, Context context) throws Exception {
            if (in == null) {
                return null;
            }
            return ULong.valueOf(in);
        }
    });
    this.constantConverter(consumer, BigInteger.class, ULong.class, new ContextualConverter<BigInteger, ULong>() {

        @Override
        public ULong convert(BigInteger in, Context context) throws Exception {
            if (in == null) {
                return null;
            }
            return ULong.valueOf(in);
        }
    });
    this.constantConverter(consumer, String.class, JSONObject.class, new ContextualConverter<String, JSONObject>() {

        @Override
        public JSONObject convert(String in, Context context) throws Exception {
            if (in == null) {
                return null;
            }
            JSONParser parser = new JSONParser();
            return (JSONObject) parser.parse(in);
        }
    });
}
Also used : UInteger(org.jooq.types.UInteger) UShort(org.jooq.types.UShort) Context(org.simpleflatmapper.converter.Context) ULong(org.jooq.types.ULong) UShort(org.jooq.types.UShort) UByte(org.jooq.types.UByte) UInteger(org.jooq.types.UInteger) BigInteger(java.math.BigInteger) JSONObject(org.jooq.tools.json.JSONObject) UByte(org.jooq.types.UByte) ULong(org.jooq.types.ULong) BigInteger(java.math.BigInteger) JSONParser(org.jooq.tools.json.JSONParser)

Example 10 with Context

use of org.simpleflatmapper.converter.Context in project SimpleFlatMapper by arnaudroger.

the class JdbcMapperEnumTest method testIndexedEnumFactoryMethod.

@Test
public void testIndexedEnumFactoryMethod() throws Exception {
    JdbcMapperBuilder<DbEnumObject> builder = JdbcMapperFactoryHelper.asm().newBuilder(DbEnumObject.class);
    builder.addMapping("val", 1, Types.VARCHAR, new Object[] { ConverterProperty.of(new ContextualConverter<String, DbObject.Type>() {

        @Override
        public DbObject.Type convert(String s, Context context) throws Exception {
            return DbObject.Type.shortForm(s);
        }
    }) });
    JdbcMapper<DbEnumObject> mapper = builder.mapper();
    ResultSet rs = mock(ResultSet.class);
    when(rs.getString(1)).thenReturn("t2");
    assertEquals(DbObject.Type.type2, mapper.map(rs).getVal());
}
Also used : Context(org.simpleflatmapper.converter.Context) DbObject(org.simpleflatmapper.test.beans.DbObject) ResultSet(java.sql.ResultSet) DbEnumObject(org.simpleflatmapper.test.beans.DbEnumObject) ContextualConverter(org.simpleflatmapper.converter.ContextualConverter) Test(org.junit.Test)

Aggregations

Context (org.simpleflatmapper.converter.Context)13 Test (org.junit.Test)9 DbObject (org.simpleflatmapper.test.beans.DbObject)6 MappingException (org.simpleflatmapper.map.MappingException)4 ContextualConverter (org.simpleflatmapper.converter.ContextualConverter)3 Type (java.lang.reflect.Type)2 BigInteger (java.math.BigInteger)2 ResultSet (java.sql.ResultSet)2 SQLException (java.sql.SQLException)2 ParseException (java.text.ParseException)2 FieldMapperErrorHandler (org.simpleflatmapper.map.FieldMapperErrorHandler)2 SampleFieldKey (org.simpleflatmapper.test.map.SampleFieldKey)2 IOException (java.io.IOException)1 StringReader (java.io.StringReader)1 UnsupportedEncodingException (java.io.UnsupportedEncodingException)1 Connection (java.sql.Connection)1 Statement (java.sql.Statement)1 ArrayList (java.util.ArrayList)1 Date (java.util.Date)1 UUID (java.util.UUID)1