|
1 | 1 | /*
|
2 |
| - * Copyright 2002-2015 the original author or authors. |
| 2 | + * Copyright 2002-2016 the original author or authors. |
3 | 3 | *
|
4 | 4 | * Licensed under the Apache License, Version 2.0 (the "License");
|
5 | 5 | * you may not use this file except in compliance with the License.
|
|
29 | 29 | import java.time.ZonedDateTime;
|
30 | 30 | import java.time.format.DateTimeFormatter;
|
31 | 31 | import java.time.format.FormatStyle;
|
32 |
| -import java.util.HashMap; |
| 32 | +import java.util.EnumMap; |
33 | 33 | import java.util.Map;
|
34 | 34 |
|
35 | 35 | import org.springframework.format.FormatterRegistrar;
|
@@ -58,18 +58,19 @@ private enum Type {DATE, TIME, DATE_TIME}
|
58 | 58 |
|
59 | 59 |
|
60 | 60 | /**
|
61 |
| - * User defined formatters. |
| 61 | + * User-defined formatters. |
62 | 62 | */
|
63 |
| - private final Map<Type, DateTimeFormatter> formatters = new HashMap<Type, DateTimeFormatter>(); |
| 63 | + private final Map<Type, DateTimeFormatter> formatters = |
| 64 | + new EnumMap<Type, DateTimeFormatter>(Type.class); |
64 | 65 |
|
65 | 66 | /**
|
66 | 67 | * Factories used when specific formatters have not been specified.
|
67 | 68 | */
|
68 |
| - private final Map<Type, DateTimeFormatterFactory> factories; |
| 69 | + private final Map<Type, DateTimeFormatterFactory> factories = |
| 70 | + new EnumMap<Type, DateTimeFormatterFactory>(Type.class); |
69 | 71 |
|
70 | 72 |
|
71 | 73 | public DateTimeFormatterRegistrar() {
|
72 |
| - this.factories = new HashMap<Type, DateTimeFormatterFactory>(); |
73 | 74 | for (Type type : Type.values()) {
|
74 | 75 | this.factories.put(type, new DateTimeFormatterFactory());
|
75 | 76 | }
|
@@ -157,33 +158,38 @@ public void setDateTimeFormatter(DateTimeFormatter formatter) {
|
157 | 158 | public void registerFormatters(FormatterRegistry registry) {
|
158 | 159 | DateTimeConverters.registerConverters(registry);
|
159 | 160 |
|
160 |
| - DateTimeFormatter dateFormatter = getFormatter(Type.DATE); |
161 |
| - DateTimeFormatter timeFormatter = getFormatter(Type.TIME); |
162 |
| - DateTimeFormatter dateTimeFormatter = getFormatter(Type.DATE_TIME); |
| 161 | + DateTimeFormatter df = getFormatter(Type.DATE); |
| 162 | + DateTimeFormatter tf = getFormatter(Type.TIME); |
| 163 | + DateTimeFormatter dtf = getFormatter(Type.DATE_TIME); |
| 164 | + |
| 165 | + // Efficient ISO_LOCAL_* variants for printing since they are twice as fast... |
163 | 166 |
|
164 | 167 | registry.addFormatterForFieldType(LocalDate.class,
|
165 |
| - new TemporalAccessorPrinter(dateFormatter), |
166 |
| - new TemporalAccessorParser(LocalDate.class, dateFormatter)); |
| 168 | + new TemporalAccessorPrinter( |
| 169 | + df == DateTimeFormatter.ISO_DATE ? DateTimeFormatter.ISO_LOCAL_DATE : df), |
| 170 | + new TemporalAccessorParser(LocalDate.class, df)); |
167 | 171 |
|
168 | 172 | registry.addFormatterForFieldType(LocalTime.class,
|
169 |
| - new TemporalAccessorPrinter(timeFormatter), |
170 |
| - new TemporalAccessorParser(LocalTime.class, timeFormatter)); |
| 173 | + new TemporalAccessorPrinter( |
| 174 | + tf == DateTimeFormatter.ISO_TIME ? DateTimeFormatter.ISO_LOCAL_TIME : tf), |
| 175 | + new TemporalAccessorParser(LocalTime.class, tf)); |
171 | 176 |
|
172 | 177 | registry.addFormatterForFieldType(LocalDateTime.class,
|
173 |
| - new TemporalAccessorPrinter(dateTimeFormatter), |
174 |
| - new TemporalAccessorParser(LocalDateTime.class, dateTimeFormatter)); |
| 178 | + new TemporalAccessorPrinter( |
| 179 | + dtf == DateTimeFormatter.ISO_DATE_TIME ? DateTimeFormatter.ISO_LOCAL_DATE_TIME : dtf), |
| 180 | + new TemporalAccessorParser(LocalDateTime.class, dtf)); |
175 | 181 |
|
176 | 182 | registry.addFormatterForFieldType(ZonedDateTime.class,
|
177 |
| - new TemporalAccessorPrinter(dateTimeFormatter), |
178 |
| - new TemporalAccessorParser(ZonedDateTime.class, dateTimeFormatter)); |
| 183 | + new TemporalAccessorPrinter(dtf), |
| 184 | + new TemporalAccessorParser(ZonedDateTime.class, dtf)); |
179 | 185 |
|
180 | 186 | registry.addFormatterForFieldType(OffsetDateTime.class,
|
181 |
| - new TemporalAccessorPrinter(dateTimeFormatter), |
182 |
| - new TemporalAccessorParser(OffsetDateTime.class, dateTimeFormatter)); |
| 187 | + new TemporalAccessorPrinter(dtf), |
| 188 | + new TemporalAccessorParser(OffsetDateTime.class, dtf)); |
183 | 189 |
|
184 | 190 | registry.addFormatterForFieldType(OffsetTime.class,
|
185 |
| - new TemporalAccessorPrinter(timeFormatter), |
186 |
| - new TemporalAccessorParser(OffsetTime.class, timeFormatter)); |
| 191 | + new TemporalAccessorPrinter(tf), |
| 192 | + new TemporalAccessorParser(OffsetTime.class, tf)); |
187 | 193 |
|
188 | 194 | registry.addFormatterForFieldType(Instant.class, new InstantFormatter());
|
189 | 195 | registry.addFormatterForFieldType(Period.class, new PeriodFormatter());
|
|
0 commit comments