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

cn.kduck.core.utils.ConversionUtils Maven / Gradle / Ivy

Go to download

The core of the K-Duck development framework encompasses all the featured components of the framework.

There is a newer version: 2.0.0
Show newest version
package cn.kduck.core.utils;

import org.springframework.core.convert.ConversionService;
import org.springframework.core.convert.converter.Converter;
import org.springframework.core.convert.support.DefaultConversionService;
import org.springframework.core.convert.support.GenericConversionService;
import org.springframework.util.StringUtils;

import java.io.Reader;
import java.io.StringReader;
import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.EnumMap;

/**
 * LiuHG
 */
public final class ConversionUtils {

    private static ConversionService conversionService = DefaultConversionService.getSharedInstance();

    static {
        GenericConversionService genericConversionService = (GenericConversionService)conversionService;
        genericConversionService.addConverter(new Converter() {
            @Override
            public Date convert(Long source) {
                return new Date(source);
            }
        });

        genericConversionService.addConverter(new Integer2DateConverter());
        genericConversionService.addConverter(new DateConverter());
        genericConversionService.addConverter(new Boolean2IntegerConverter());
        genericConversionService.addConverter(new Integer2BooleanConverter());
        genericConversionService.addConverter(new Boolean2LongConverter());
        genericConversionService.addConverter(new Long2BooleanConverter());
        genericConversionService.addConverter(new ReaderConverter());

    }

    public static  T convert(Object source, Class targetType){
        return conversionService.convert(source,targetType);
    }

    public static class Boolean2LongConverter implements Converter{
        @Override
        public Long convert(Boolean source) {
            if(source == null) return 0L;
            return source.booleanValue() ? 1L : 0L;
        }
    }

    public static class Long2BooleanConverter implements Converter{
        @Override
        public Boolean convert(Long source) {
            if(source == null) return false;
            return source.intValue() > 0L ? true : false;
        }
    }

    public static class Boolean2IntegerConverter implements Converter{
        @Override
        public Integer convert(Boolean source) {
            if(source == null) return 0;
            return source.booleanValue() ? 1 : 0;
        }
    }

    public static class Integer2BooleanConverter implements Converter{
        @Override
        public Boolean convert(Integer source) {
            if(source == null) return false;
            return source.intValue() > 0 ? true : false;
        }
    }

    public static class Integer2DateConverter implements Converter{
        @Override
        public Date convert(Integer source) {
            return new Date(source.longValue());
        }
    }

    public static class ReaderConverter implements Converter{

        @Override
        public Reader convert(String source) {
            if(source == null) return null;
            return new StringReader(source);
        }
    }

    public static class DateConverter implements Converter{

        private ThreadLocal dateConverterThreadLocal = new ThreadLocal<>();

//        private DateFormat DATE_FORMAT_YM = new SimpleDateFormat("yyyy-MM");
//        private DateFormat DATE_FORMAT_YMD = new SimpleDateFormat("yyyy-MM-dd");
//        private DateFormat DATE_FORMAT_YMDHM = new SimpleDateFormat("yyyy-MM-dd HH:mm");
//        private DateFormat DATE_FORMAT_YMDHMS = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");

        @Override
        public Date convert(String dateValue) {
            String value = dateValue.trim();
            if (!StringUtils.hasText(value)) {
                return null;
            }
            if (dateValue.matches("^\\d{4}-\\d{1,2}$")) {
                return parseDate(dateValue, DateFormatType.DATE_FORMAT_YM);
            } else if (dateValue.matches("^\\d{4}-\\d{1,2}-\\d{1,2}$")) {
                return parseDate(dateValue, DateFormatType.DATE_FORMAT_YMD);
            } else if (dateValue.matches("^\\d{4}-\\d{1,2}-\\d{1,2} {1}\\d{1,2}:\\d{1,2}$")) {
                return parseDate(dateValue, DateFormatType.DATE_FORMAT_YMDHM);
            } else if (dateValue.matches("^\\d{4}-\\d{1,2}-\\d{1,2} {1}\\d{1,2}:\\d{1,2}:\\d{1,2}$")) {
                return parseDate(dateValue, DateFormatType.DATE_FORMAT_YMDHMS);
            } else if (dateValue.matches("^[\\-|1-9]\\d*$")) {
                return new Date(Long.valueOf(dateValue));
            } else if (dateValue.matches("^{1}\\d{1,2}:\\d{1,2}:\\d{1,2}$")) {
                return parseDate(dateValue, DateFormatType.DATE_FORMAT_HMS);
            } else {
                throw new IllegalArgumentException("没有对该日期类型格式准备适合的转换器:" + dateValue);
            }
        }

        private Date parseDate(String dateValue, DateFormatType format) {
            DateFormatContainer dateFormatContainer = dateConverterThreadLocal.get();
            if(dateFormatContainer == null){
                dateFormatContainer = new DateFormatContainer();
                dateConverterThreadLocal.set(dateFormatContainer);
            }

            DateFormat dateFormat = dateFormatContainer.getDateFormat(format);
            try {
                return dateFormat.parse(dateValue);
            } catch (ParseException e) {
                throw new RuntimeException("格式化日期错误:pattern=" + ((SimpleDateFormat)dateFormat).toPattern() + ",dateValue=" + dateValue);
            }
        }

        private static class DateFormatContainer {

            private EnumMap dateFormatMap = new EnumMap(DateFormatType.class);

            public DateFormatContainer(){
                dateFormatMap.put(DateFormatType.DATE_FORMAT_HMS,new SimpleDateFormat("HH:mm:ss"));
                dateFormatMap.put(DateFormatType.DATE_FORMAT_YM,new SimpleDateFormat("yyyy-MM"));
                dateFormatMap.put(DateFormatType.DATE_FORMAT_YMD,new SimpleDateFormat("yyyy-MM-dd"));
                dateFormatMap.put(DateFormatType.DATE_FORMAT_YMDHM,new SimpleDateFormat("yyyy-MM-dd HH:mm"));
                dateFormatMap.put(DateFormatType.DATE_FORMAT_YMDHMS,new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"));
            }

            private DateFormat getDateFormat(DateFormatType format){
                return dateFormatMap.get(format);
            }

        }

        private enum DateFormatType{
            DATE_FORMAT_YM,
            DATE_FORMAT_YMD,
            DATE_FORMAT_YMDHM,
            DATE_FORMAT_YMDHMS,
            DATE_FORMAT_HMS,
        }
    }

}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy