柯桥增值式服务
25개 이상의 토픽을 선택하실 수 없습니다. Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

43 lines
1.1KB

  1. package com.ningdatech.kqapi.common.converter;
  2. import java.util.Map;
  3. import java.util.Set;
  4. import java.util.function.Function;
  5. /**
  6. * 解决入参为 Date类型
  7. *
  8. * @author WendyYang
  9. * @date 2019-04-30
  10. */
  11. public abstract class BaseDateConverter<T> {
  12. /**
  13. * 值转换
  14. *
  15. * @param source 源数据
  16. * @param function 回调
  17. * @return 转换后的数据
  18. */
  19. public T convert(String source, Function<String, T> function) {
  20. if (source == null || source.isEmpty()) {
  21. return null;
  22. }
  23. String sourceTrim = source.trim();
  24. Set<Map.Entry<String, String>> entries = getFormat().entrySet();
  25. for (Map.Entry<String, String> entry : entries) {
  26. if (sourceTrim.matches(entry.getValue())) {
  27. return function.apply(entry.getKey());
  28. }
  29. }
  30. throw new IllegalArgumentException("无效的日期参数格式:'" + sourceTrim + "'");
  31. }
  32. /**
  33. * 获取子类 具体的格式化表达式
  34. *
  35. * @return 格式化
  36. */
  37. protected abstract Map<String, String> getFormat();
  38. }