package com.red.circle.console.infra.framework.convert;


import com.red.circle.tool.core.date.LocalDateTimeUtils;
import com.red.circle.tool.core.parse.DataTypeUtils;
import com.red.circle.tool.core.regex.RegexConstant;
import com.red.circle.tool.core.text.StringUtils;
import java.time.LocalDateTime;
import org.springframework.core.convert.converter.Converter;
import org.springframework.stereotype.Component;

/**
 * @author pengliang on 2020/9/3
 */
@Component
public class LocalDateTimeConvert implements Converter<String, LocalDateTime> {

  @Override
  public LocalDateTime convert(String source) {
    if (StringUtils.isBlank(source)) {
      return null;
    }

    if (source.matches(RegexConstant.NUMBER)) {
      return LocalDateTimeUtils.convert(DataTypeUtils.toLong(source));
    }

    throw new IllegalArgumentException("Cannot convert " + source + " to  LocalDateTimeUtils.");
  }
}
