All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.simpleflatmapper.converter.impl.time.JavaTimeConverterFactoryProducer Maven / Gradle / Ivy

package org.simpleflatmapper.converter.impl.time;

import org.simpleflatmapper.converter.AbstractContextualConverterFactory;
import org.simpleflatmapper.converter.AbstractConverterFactory;
import org.simpleflatmapper.converter.AbstractContextualConverterFactoryProducer;
import org.simpleflatmapper.converter.ContextFactoryBuilder;
import org.simpleflatmapper.converter.ContextualConverter;
import org.simpleflatmapper.converter.ContextualConverterFactory;
import org.simpleflatmapper.converter.ConvertingTypes;
import org.simpleflatmapper.converter.ToStringConverter;
import org.simpleflatmapper.util.Consumer;

import java.time.*;
import java.time.format.DateTimeFormatter;
import java.time.temporal.Temporal;
import java.util.Date;

public class JavaTimeConverterFactoryProducer extends AbstractContextualConverterFactoryProducer {


    @Override
    public void produce(Consumer> consumer) {
        constantConverter(consumer, Instant.class, Date.class, new JavaInstantTojuDateConverter());
        factoryConverter(consumer, new AbstractContextualConverterFactory(LocalDateTime.class, Date.class) {
            @Override
            public ContextualConverter newConverter(ConvertingTypes targetedTypes, ContextFactoryBuilder contextFactoryBuilder, Object... params) {
                return new JavaLocalDateTimeTojuDateConverter(JavaTimeHelper.getZoneIdOrDefault(params));
            }
        });
        factoryConverter(consumer, new AbstractContextualConverterFactory(LocalDate.class, Date.class) {
            @Override
            public ContextualConverter newConverter(ConvertingTypes targetedTypes, ContextFactoryBuilder contextFactoryBuilder, Object... params) {
                return new JavaLocalDateTojuDateConverter(JavaTimeHelper.getZoneIdOrDefault(params));
            }
        });
        factoryConverter(consumer, new AbstractContextualConverterFactory(LocalTime.class, Date.class) {
            @Override
            public ContextualConverter newConverter(ConvertingTypes targetedTypes, ContextFactoryBuilder contextFactoryBuilder, Object... params) {
                return new JavaLocalTimeTojuDateConverter(JavaTimeHelper.getZoneIdOrDefault(params));
            }
        });
        constantConverter(consumer, ZonedDateTime.class, Date.class, new JavaZonedDateTimeTojuDateConverter());
        constantConverter(consumer, OffsetDateTime.class, Date.class, new JavaOffsetDateTimeTojuDateConverter());
        constantConverter(consumer, OffsetTime.class, Date.class, new JavaOffsetTimeTojuDateConverter());
        factoryConverter(consumer, new AbstractContextualConverterFactory(YearMonth.class, Date.class) {
            @Override
            public ContextualConverter newConverter(ConvertingTypes targetedTypes, ContextFactoryBuilder contextFactoryBuilder, Object... params) {
                return new JavaYearMonthTojuDateConverter(JavaTimeHelper.getZoneIdOrDefault(params));
            }
        });
        factoryConverter(consumer, new AbstractContextualConverterFactory(Year.class, Date.class) {
            @Override
            public ContextualConverter newConverter(ConvertingTypes targetedTypes, ContextFactoryBuilder contextFactoryBuilder, Object... params) {
                return new JavaYearTojuDateConverter(JavaTimeHelper.getZoneIdOrDefault(params));
            }
        });

        constantConverter(consumer, Date.class, Instant.class, new DateToJavaInstantConverter());
        factoryConverter(consumer, new AbstractContextualConverterFactory(Date.class, LocalDateTime.class) {
            @Override
            public ContextualConverter newConverter(ConvertingTypes targetedTypes, ContextFactoryBuilder contextFactoryBuilder, Object... params) {
                return new DateToJavaLocalDateTimeConverter(JavaTimeHelper.getZoneIdOrDefault(params));
            }
        });
        factoryConverter(consumer, new AbstractContextualConverterFactory(Date.class, LocalDate.class) {
            @Override
            public ContextualConverter newConverter(ConvertingTypes targetedTypes, ContextFactoryBuilder contextFactoryBuilder, Object... params) {
                return new DateToJavaLocalDateConverter(JavaTimeHelper.getZoneIdOrDefault(params));
            }
        });
        factoryConverter(consumer, new AbstractContextualConverterFactory(Date.class, LocalTime.class) {
            @Override
            public ContextualConverter newConverter(ConvertingTypes targetedTypes, ContextFactoryBuilder contextFactoryBuilder, Object... params) {
                return new DateToJavaLocalTimeConverter(JavaTimeHelper.getZoneIdOrDefault(params));
            }
        });
        factoryConverter(consumer, new AbstractContextualConverterFactory(Date.class, ZonedDateTime.class) {
            @Override
            public ContextualConverter newConverter(ConvertingTypes targetedTypes, ContextFactoryBuilder contextFactoryBuilder, Object... params) {
                return new DateToJavaZonedDateTimeConverter(JavaTimeHelper.getZoneIdOrDefault(params));
            }
        });
        factoryConverter(consumer, new AbstractContextualConverterFactory(Date.class, OffsetDateTime.class) {
            @Override
            public ContextualConverter newConverter(ConvertingTypes targetedTypes, ContextFactoryBuilder contextFactoryBuilder, Object... params) {
                return new DateToJavaOffsetDateTimeConverter(JavaTimeHelper.getZoneIdOrDefault(params));
            }
        });
        factoryConverter(consumer, new AbstractContextualConverterFactory(Date.class, OffsetTime.class) {
            @Override
            public ContextualConverter newConverter(ConvertingTypes targetedTypes, ContextFactoryBuilder contextFactoryBuilder, Object... params) {
                return new DateToJavaOffsetTimeConverter(JavaTimeHelper.getZoneIdOrDefault(params));
            }
        });
        factoryConverter(consumer, new AbstractContextualConverterFactory(Date.class, YearMonth.class) {
            @Override
            public ContextualConverter newConverter(ConvertingTypes targetedTypes, ContextFactoryBuilder contextFactoryBuilder, Object... params) {
                return new DateToJavaYearMonthConverter(JavaTimeHelper.getZoneIdOrDefault(params));
            }
        });
        factoryConverter(consumer, new AbstractContextualConverterFactory(Date.class, Year.class) {
            @Override
            public ContextualConverter newConverter(ConvertingTypes targetedTypes, ContextFactoryBuilder contextFactoryBuilder, Object... params) {
                return new DateToJavaYearConverter(JavaTimeHelper.getZoneIdOrDefault(params));
            }
        });

        factoryConverter(consumer, new AbstractContextualConverterFactory(Object.class, Instant.class) {
            @Override
            public ContextualConverter newConverter(ConvertingTypes targetedTypes, ContextFactoryBuilder contextFactoryBuilder, Object... params) {
                return new ObjectToJavaInstantConverter(JavaTimeHelper.getZoneIdOrDefault(params));
            }
        });
        factoryConverter(consumer, new AbstractContextualConverterFactory(Object.class, LocalDateTime.class) {
            @Override
            public ContextualConverter newConverter(ConvertingTypes targetedTypes, ContextFactoryBuilder contextFactoryBuilder, Object... params) {
                return new ObjectToJavaLocalDateTimeConverter(JavaTimeHelper.getZoneIdOrDefault(params));
            }
        });
        factoryConverter(consumer, new AbstractContextualConverterFactory(Object.class, LocalDate.class) {
            @Override
            public ContextualConverter newConverter(ConvertingTypes targetedTypes, ContextFactoryBuilder contextFactoryBuilder, Object... params) {
                return new ObjectToJavaLocalDateConverter(JavaTimeHelper.getZoneIdOrDefault(params));
            }
        });
        factoryConverter(consumer, new AbstractContextualConverterFactory(Object.class, LocalTime.class) {
            @Override
            public ContextualConverter newConverter(ConvertingTypes targetedTypes, ContextFactoryBuilder contextFactoryBuilder, Object... params) {
                return new ObjectToJavaLocalTimeConverter(JavaTimeHelper.getZoneIdOrDefault(params));
            }
        });
        factoryConverter(consumer, new AbstractContextualConverterFactory(Object.class, ZonedDateTime.class) {
            @Override
            public ContextualConverter newConverter(ConvertingTypes targetedTypes, ContextFactoryBuilder contextFactoryBuilder, Object... params) {
                return new ObjectToJavaZonedDateTimeConverter(JavaTimeHelper.getZoneIdOrDefault(params));
            }
        });
        factoryConverter(consumer, new AbstractContextualConverterFactory(Object.class, OffsetDateTime.class) {
            @Override
            public ContextualConverter newConverter(ConvertingTypes targetedTypes, ContextFactoryBuilder contextFactoryBuilder, Object... params) {
                return new ObjectToJavaOffsetDateTimeConverter(JavaTimeHelper.getZoneIdOrDefault(params));
            }
        });
        factoryConverter(consumer, new AbstractContextualConverterFactory(Object.class, OffsetTime.class) {
            @Override
            public ContextualConverter newConverter(ConvertingTypes targetedTypes, ContextFactoryBuilder contextFactoryBuilder, Object... params) {
                return new ObjectToJavaOffsetTimeConverter(JavaTimeHelper.getZoneIdOrDefault(params));
            }
        });
        factoryConverter(consumer, new AbstractContextualConverterFactory(Object.class, YearMonth.class) {
            @Override
            public ContextualConverter newConverter(ConvertingTypes targetedTypes, ContextFactoryBuilder contextFactoryBuilder, Object... params) {
                return new ObjectToJavaYearMonthConverter(JavaTimeHelper.getZoneIdOrDefault(params));
            }
        });
        factoryConverter(consumer, new AbstractContextualConverterFactory(Object.class, Year.class) {
            @Override
            public ContextualConverter newConverter(ConvertingTypes targetedTypes, ContextFactoryBuilder contextFactoryBuilder, Object... params) {
                return new ObjectToJavaYearConverter(JavaTimeHelper.getZoneIdOrDefault(params));
            }
        });



        factoryConverter(consumer, new AbstractMultiFormatContextualConverterFactory(CharSequence.class, Instant.class) {
            @SuppressWarnings("unchecked")
            @Override
            protected ContextualConverter newConverter(DateTimeFormatter formatter) {
                return new CharSequenceToInstantConverter(formatter);
            }
        });

        factoryConverter(consumer, new AbstractMultiFormatContextualConverterFactory(CharSequence.class, LocalDate.class) {
            @SuppressWarnings("unchecked")
            @Override
            protected ContextualConverter newConverter(DateTimeFormatter formatter) {
                return new CharSequenceToLocalDateConverter(formatter);
            }
        });
        factoryConverter(consumer, new AbstractMultiFormatContextualConverterFactory(CharSequence.class, LocalDateTime.class) {
            @SuppressWarnings("unchecked")
            @Override
            protected ContextualConverter newConverter(DateTimeFormatter formatter) {
                return new CharSequenceToLocalDateTimeConverter(formatter);
            }
        });
        factoryConverter(consumer, new AbstractMultiFormatContextualConverterFactory(CharSequence.class, LocalTime.class) {
            @SuppressWarnings("unchecked")
            @Override
            protected ContextualConverter newConverter(DateTimeFormatter formatter) {
                return new CharSequenceToLocalTimeConverter(formatter);
            }
        });
        factoryConverter(consumer, new AbstractMultiFormatContextualConverterFactory(CharSequence.class, OffsetDateTime.class) {
            @SuppressWarnings("unchecked")
            @Override
            protected ContextualConverter newConverter(DateTimeFormatter formatter) {
                return new CharSequenceToOffsetDateTimeConverter(formatter);
            }
        });
        factoryConverter(consumer, new AbstractMultiFormatContextualConverterFactory(CharSequence.class, OffsetTime.class) {
            @SuppressWarnings("unchecked")
            @Override
            protected ContextualConverter newConverter(DateTimeFormatter formatter) {
                return new CharSequenceToOffsetTimeConverter(formatter);
            }
        });
        factoryConverter(consumer, new AbstractMultiFormatContextualConverterFactory(CharSequence.class, Year.class) {
            @SuppressWarnings("unchecked")
            @Override
            protected ContextualConverter newConverter(DateTimeFormatter formatter) {
                return new CharSequenceToYearConverter(formatter);
            }
        });
        factoryConverter(consumer, new AbstractMultiFormatContextualConverterFactory(CharSequence.class, YearMonth.class) {
            @SuppressWarnings("unchecked")
            @Override
            protected ContextualConverter newConverter(DateTimeFormatter formatter) {
                return new CharSequenceToYearMonthConverter(formatter);
            }
        });
        factoryConverter(consumer, new AbstractMultiFormatContextualConverterFactory(CharSequence.class, ZonedDateTime.class) {
            @SuppressWarnings("unchecked")
            @Override
            protected ContextualConverter newConverter(DateTimeFormatter formatter) {
                return new CharSequenceToZonedDateTimeConverter(formatter);
            }
        });

        factoryConverter(consumer, new AbstractContextualConverterFactory(Temporal.class, String.class) {
            @Override
            public ContextualConverter newConverter(ConvertingTypes targetedTypes, ContextFactoryBuilder contextFactoryBuilder, Object... params) {
                DateTimeFormatter dateTimeFormatter = JavaTimeHelper.getDateTimeFormatter(params);
                if (dateTimeFormatter != null) {
                    return new JavaTemporalToStringConverter(dateTimeFormatter);
                } else {
                    return ToStringConverter.INSTANCE;
                }
            }
        });
    }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy