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 m_sDateFormat;
43
44 public ThreadSafeDateFormat( String sDateFormat )
45 {
46 m_sDateFormat = sDateFormat;
47 }
48
49 private final ThreadLocal<SoftReference<SimpleDateFormat>> m_formatCache = new ThreadLocal<SoftReference<SimpleDateFormat>>()
50 {
51 public SoftReference<SimpleDateFormat> get()
52 {
53 SoftReference<SimpleDateFormat> softRef = super.get();
54 if ( softRef == null || softRef.get() == null )
55 {
56 softRef = new SoftReference<SimpleDateFormat>( new SimpleDateFormat( m_sDateFormat ) );
57 super.set( softRef );
58 }
59 return softRef;
60 }
61 };
62
63 private DateFormat getDateFormat()
64 {
65 return m_formatCache.get().get();
66 }
67
68 public StringBuffer format( Date date, StringBuffer toAppendTo, FieldPosition fieldPosition )
69 {
70 return getDateFormat().format( date, toAppendTo, fieldPosition );
71 }
72
73 public Date parse( String source, ParsePosition pos )
74 {
75 return getDateFormat().parse( source, pos );
76 }
77 }