1 package org.apache.maven.scm.util;
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22 import java.lang.ref.SoftReference;
23 import java.text.DateFormat;
24 import java.text.FieldPosition;
25 import java.text.ParsePosition;
26 import java.text.SimpleDateFormat;
27 import java.util.Date;
28
29
30
31
32
33
34
35
36
37 public class ThreadSafeDateFormat
38 extends DateFormat
39 {
40 private static final long serialVersionUID = 3786090697869963812L;
41
42 private final String dateFormat;
43
44 public ThreadSafeDateFormat( String sDateFormat )
45 {
46 dateFormat = sDateFormat;
47 }
48
49 private final ThreadLocal<SoftReference<SimpleDateFormat>> formatCache =
50 new ThreadLocal<SoftReference<SimpleDateFormat>>()
51 {
52 public SoftReference<SimpleDateFormat> get()
53 {
54 SoftReference<SimpleDateFormat> softRef = super.get();
55 if ( softRef == null || softRef.get() == null )
56 {
57 softRef = new SoftReference<SimpleDateFormat>( new SimpleDateFormat( dateFormat ) );
58 super.set( softRef );
59 }
60 return softRef;
61 }
62 };
63
64 private DateFormat getDateFormat()
65 {
66 return formatCache.get().get();
67 }
68
69 public StringBuffer format( Date date, StringBuffer toAppendTo, FieldPosition fieldPosition )
70 {
71 return getDateFormat().format( date, toAppendTo, fieldPosition );
72 }
73
74 public Date parse( String source, ParsePosition pos )
75 {
76 return getDateFormat().parse( source, pos );
77 }
78 }