@@ -22,11 +22,12 @@ import java.time.chrono.IsoChronology
22
22
import java .time .format .{DateTimeFormatter , DateTimeFormatterBuilder , ResolverStyle }
23
23
import java .time .temporal .{ChronoField , TemporalAccessor , TemporalQueries }
24
24
import java .util .Locale
25
- import java .util .concurrent .{Callable , TimeUnit }
26
25
27
26
import com .google .common .cache .CacheBuilder
28
27
import com .google .common .util .concurrent .{ExecutionError , UncheckedExecutionException }
29
28
29
+ import org .apache .spark .sql .catalyst .util .DateTimeFormatterHelper ._
30
+
30
31
trait DateTimeFormatterHelper {
31
32
protected def toInstantWithZoneId (temporalAccessor : TemporalAccessor , zoneId : ZoneId ): Instant = {
32
33
val localTime = if (temporalAccessor.query(TemporalQueries .localTime) == null ) {
@@ -39,15 +40,30 @@ trait DateTimeFormatterHelper {
39
40
val zonedDateTime = ZonedDateTime .of(localDateTime, zoneId)
40
41
Instant .from(zonedDateTime)
41
42
}
43
+
44
+ def getOrCreateFormatter (pattern : String , locale : Locale ): DateTimeFormatter = {
45
+ try {
46
+ val key = (pattern, locale)
47
+ var formatter = cache.getIfPresent(key)
48
+ if (formatter == null ) {
49
+ formatter = buildFormatter(pattern, locale)
50
+ cache.put(key, formatter)
51
+ }
52
+ formatter
53
+ } catch {
54
+ // Cache.get() may wrap the original exception.
55
+ case e @ (_ : UncheckedExecutionException | _ : ExecutionError ) =>
56
+ throw e.getCause
57
+ }
58
+ }
42
59
}
43
60
44
- object DateTimeFormatterHelper {
45
- private val cache = CacheBuilder .newBuilder()
61
+ private object DateTimeFormatterHelper {
62
+ val cache = CacheBuilder .newBuilder()
46
63
.maximumSize(128 )
47
64
.build[(String , Locale ), DateTimeFormatter ]()
48
65
49
-
50
- private def buildFormatter (pattern : String , locale : Locale ): DateTimeFormatter = {
66
+ def buildFormatter (pattern : String , locale : Locale ): DateTimeFormatter = {
51
67
new DateTimeFormatterBuilder ()
52
68
.parseCaseInsensitive()
53
69
.appendPattern(pattern)
@@ -60,20 +76,4 @@ object DateTimeFormatterHelper {
60
76
.withChronology(IsoChronology .INSTANCE )
61
77
.withResolverStyle(ResolverStyle .STRICT )
62
78
}
63
-
64
- def getFormatter (pattern : String , locale : Locale ): DateTimeFormatter = {
65
- try {
66
- val key = (pattern, locale)
67
- var formatter = cache.getIfPresent(key)
68
- if (formatter == null ) {
69
- formatter = buildFormatter(pattern, locale)
70
- cache.put(key, formatter)
71
- }
72
- formatter
73
- } catch {
74
- // Cache.get() may wrap the original exception.
75
- case e @ (_ : UncheckedExecutionException | _ : ExecutionError ) =>
76
- throw e.getCause
77
- }
78
- }
79
79
}
0 commit comments