package com.thoughtworks.xstream.core.util; import java.text.*; import java.text.SimpleDateFormat; import java.util.*; import java.util.Locale; /** * Wrapper around java.text.SimpleDateFormat that can * be called by multiple threads concurrently. *
SimpleDateFormat has a high overhead in creating * and is not thread safe. To make best use of resources, * the ThreadSafeSimpleDateFormat provides a dynamically * sizing pool of instances, each of which will only * be called by a single thread at a time.
*The pool has a maximum capacity, to limit overhead. * If all instances in the pool are in use and another is * required, it shall block until one becomes available.
* * @author Joe Walnes */ public class NonLenientThreadSafeSimpleDateFormat extends ThreadSafeSimpleDateFormat{ private final String formatString; private final Pool pool; public NonLenientThreadSafeSimpleDateFormat(String format, int initialPoolSize, int maxPoolSize) { super(format, initialPoolSize, maxPoolSize); formatString = format; pool = new Pool(initialPoolSize, maxPoolSize, new Pool.Factory() { public Object newInstance() { SimpleDateFormat format = new SimpleDateFormat(formatString, Locale.ENGLISH); format.setLenient(false); return format; } }); } public String format(Date date) { DateFormat format = fetchFromPool(); try { return format.format(date); } finally { pool.putInPool(format); } } public Date parse(String date) throws ParseException { DateFormat format = fetchFromPool(); try { return format.parse(date); } finally { pool.putInPool(format); } } private DateFormat fetchFromPool() { TimeZone tz = TimeZone.getDefault(); DateFormat format = (DateFormat)pool.fetchFromPool(); if (!tz.equals(format.getTimeZone())) { format.setTimeZone(tz); } return format; } }