From a3187e02a0e030f3d4e7e9a11650dfc7af8590b5 Mon Sep 17 00:00:00 2001 From: huifer Date: Mon, 12 Oct 2020 13:16:09 +0800 Subject: [PATCH] doc: format --- ...ateTimeFormatAnnotationFormatterFactory.md | 63 +++++++++++++++++++ .../format/Parser/Spring-DateTimeParser.md | 29 +++++++++ .../Spring-MillisecondInstantPrinter.md | 28 +++++++++ .../Spring-AnnotationFormatterFactory.md | 42 +++++++++++++ docs/Spring/clazz/format/Spring-Formatter.md | 11 ++++ docs/Spring/clazz/format/Spring-Parser.md | 27 ++++++++ docs/Spring/clazz/format/Spring-Printer.md | 20 ++++++ 7 files changed, 220 insertions(+) create mode 100644 docs/Spring/clazz/format/AnnotationFormatterFactory/Spring-DateTimeFormatAnnotationFormatterFactory.md create mode 100644 docs/Spring/clazz/format/Parser/Spring-DateTimeParser.md create mode 100644 docs/Spring/clazz/format/Printer/Spring-MillisecondInstantPrinter.md create mode 100644 docs/Spring/clazz/format/Spring-AnnotationFormatterFactory.md create mode 100644 docs/Spring/clazz/format/Spring-Formatter.md create mode 100644 docs/Spring/clazz/format/Spring-Parser.md create mode 100644 docs/Spring/clazz/format/Spring-Printer.md diff --git a/docs/Spring/clazz/format/AnnotationFormatterFactory/Spring-DateTimeFormatAnnotationFormatterFactory.md b/docs/Spring/clazz/format/AnnotationFormatterFactory/Spring-DateTimeFormatAnnotationFormatterFactory.md new file mode 100644 index 0000000..9c57907 --- /dev/null +++ b/docs/Spring/clazz/format/AnnotationFormatterFactory/Spring-DateTimeFormatAnnotationFormatterFactory.md @@ -0,0 +1,63 @@ +# Spring DateTimeFormatAnnotationFormatterFactory + +- 类全路径: `org.springframework.format.datetime.DateTimeFormatAnnotationFormatterFactory` + +- 类图 +![EmbeddedValueResolutionSupport](/images/spring/DateTimeFormatAnnotationFormatterFactory.png) + + +```java +public class DateTimeFormatAnnotationFormatterFactory extends EmbeddedValueResolutionSupport + implements AnnotationFormatterFactory { + + /** + * 字段类型 + */ + private static final Set> FIELD_TYPES; + + @Override + public Set> getFieldTypes() { + return FIELD_TYPES; + } + + @Override + public Printer getPrinter(DateTimeFormat annotation, Class fieldType) { + return getFormatter(annotation, fieldType); + } + + @Override + public Parser getParser(DateTimeFormat annotation, Class fieldType) { + return getFormatter(annotation, fieldType); + } + + protected Formatter getFormatter(DateTimeFormat annotation, Class fieldType) { + DateFormatter formatter = new DateFormatter(); + // style + String style = resolveEmbeddedValue(annotation.style()); + // 判断时间格式是否村子啊 + if (StringUtils.hasLength(style)) { + formatter.setStylePattern(style); + } + // iso 设置 + formatter.setIso(annotation.iso()); + // date time pattern + String pattern = resolveEmbeddedValue(annotation.pattern()); + // 设置 + if (StringUtils.hasLength(pattern)) { + formatter.setPattern(pattern); + } + return formatter; + } + + static { + Set> fieldTypes = new HashSet<>(4); + // 加入字段类型 + fieldTypes.add(Date.class); + fieldTypes.add(Calendar.class); + fieldTypes.add(Long.class); + FIELD_TYPES = Collections.unmodifiableSet(fieldTypes); + } + +} + +``` \ No newline at end of file diff --git a/docs/Spring/clazz/format/Parser/Spring-DateTimeParser.md b/docs/Spring/clazz/format/Parser/Spring-DateTimeParser.md new file mode 100644 index 0000000..ecca6f3 --- /dev/null +++ b/docs/Spring/clazz/format/Parser/Spring-DateTimeParser.md @@ -0,0 +1,29 @@ +# Spring DateTimeParser +- 类全路径: `org.springframework.format.datetime.joda.DateTimeParser` + +- 代码如下 + +```java +public final class DateTimeParser implements Parser { + + private final DateTimeFormatter formatter; + + + /** + * Create a new DateTimeParser. + * @param formatter the Joda DateTimeFormatter instance + */ + public DateTimeParser(DateTimeFormatter formatter) { + this.formatter = formatter; + } + + + @Override + public DateTime parse(String text, Locale locale) throws ParseException { + // DateTimeFormatter 转换字符串事件类型 + return JodaTimeContextHolder.getFormatter(this.formatter, locale).parseDateTime(text); + } + +} + +``` \ No newline at end of file diff --git a/docs/Spring/clazz/format/Printer/Spring-MillisecondInstantPrinter.md b/docs/Spring/clazz/format/Printer/Spring-MillisecondInstantPrinter.md new file mode 100644 index 0000000..36abd96 --- /dev/null +++ b/docs/Spring/clazz/format/Printer/Spring-MillisecondInstantPrinter.md @@ -0,0 +1,28 @@ +# Spring MillisecondInstantPrinter +- 类全路径: `org.springframework.format.datetime.joda.MillisecondInstantPrinter` + + +```java +public final class MillisecondInstantPrinter implements Printer { + + private final DateTimeFormatter formatter; + + + /** + * Create a new ReadableInstantPrinter. + * @param formatter the Joda DateTimeFormatter instance + */ + public MillisecondInstantPrinter(DateTimeFormatter formatter) { + this.formatter = formatter; + } + + + @Override + public String print(Long instant, Locale locale) { + // DateTimeFormatter .print + return JodaTimeContextHolder.getFormatter(this.formatter, locale).print(instant); + } + +} + +``` \ No newline at end of file diff --git a/docs/Spring/clazz/format/Spring-AnnotationFormatterFactory.md b/docs/Spring/clazz/format/Spring-AnnotationFormatterFactory.md new file mode 100644 index 0000000..6bef88e --- /dev/null +++ b/docs/Spring/clazz/format/Spring-AnnotationFormatterFactory.md @@ -0,0 +1,42 @@ +# Spring AnnotationFormatterFactory + +- 类全路径: `org.springframework.format.AnnotationFormatterFactory` + + +```java + +public interface AnnotationFormatterFactory { + + /** + * The types of fields that may be annotated with the <A> annotation. + * 字段类型 + */ + Set> getFieldTypes(); + + /** + * Get the Printer to print the value of a field of {@code fieldType} annotated with + * {@code annotation}. + *

If the type T the printer accepts is not assignable to {@code fieldType}, a + * coercion from {@code fieldType} to T will be attempted before the Printer is invoked. + * 通过注解和字段类型获取输出接口 + * @param annotation the annotation instance + * @param fieldType the type of field that was annotated + * @return the printer + */ + Printer getPrinter(A annotation, Class fieldType); + + /** + * Get the Parser to parse a submitted value for a field of {@code fieldType} + * annotated with {@code annotation}. + *

If the object the parser returns is not assignable to {@code fieldType}, + * a coercion to {@code fieldType} will be attempted before the field is set. + * 通过注解和字段类型获取解析接口 + * @param annotation the annotation instance + * @param fieldType the type of field that was annotated + * @return the parser + */ + Parser getParser(A annotation, Class fieldType); + +} + +``` \ No newline at end of file diff --git a/docs/Spring/clazz/format/Spring-Formatter.md b/docs/Spring/clazz/format/Spring-Formatter.md new file mode 100644 index 0000000..e3979e8 --- /dev/null +++ b/docs/Spring/clazz/format/Spring-Formatter.md @@ -0,0 +1,11 @@ +# Spring Formatter +- 类全路径: `org.springframework.format.Formatter` + +```java +public interface Formatter extends Printer, Parser { + +} +``` + +- 该接口继承了 printer 和 parser 两个接口. +- 比较常见的有: `DateFormatter` 就是继承这个接口. \ No newline at end of file diff --git a/docs/Spring/clazz/format/Spring-Parser.md b/docs/Spring/clazz/format/Spring-Parser.md new file mode 100644 index 0000000..7762c4f --- /dev/null +++ b/docs/Spring/clazz/format/Spring-Parser.md @@ -0,0 +1,27 @@ +# Spring Parser + +- 类全路径: `org.springframework.format.Parser` +- 类作用: 字符串准换成java对象 + +```java + +@FunctionalInterface +public interface Parser { + + /** + * Parse a text String to produce a T. + * 将字符串转换成对象 + * @param text the text string + * @param locale the current user locale + * @return an instance of T + * @throws ParseException when a parse exception occurs in a java.text parsing library + * @throws IllegalArgumentException when a parse exception occurs + */ + T parse(String text, Locale locale) throws ParseException; + +} +``` + +- 类图 + +![Parser](/images/spring/Parser.png) \ No newline at end of file diff --git a/docs/Spring/clazz/format/Spring-Printer.md b/docs/Spring/clazz/format/Spring-Printer.md new file mode 100644 index 0000000..66e3feb --- /dev/null +++ b/docs/Spring/clazz/format/Spring-Printer.md @@ -0,0 +1,20 @@ +# Spring Printer +- 类全路径: `org.springframework.format.Printer` +- 类作用: 对象转换成字符串 + + +```java +@FunctionalInterface +public interface Printer { + + /** + * Print the object of type T for display. + * 打印对象 + * @param object the instance to print + * @param locale the current user locale + * @return the printed text string + */ + String print(T object, Locale locale); + +} +``` \ No newline at end of file