<commit_before>/*
 *************************************************************************
 * Copyright (c) 2006 Actuate Corporation.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 *
 * Contributors:
 *  Actuate Corporation  - initial API and implementation
 *  
 *************************************************************************
 */

package org.eclipse.birt.core.data;

import java.util.HashMap;

import com.ibm.icu.text.DateFormat;
import com.ibm.icu.text.SimpleDateFormat;

import com.ibm.icu.util.ULocale;

/**
 * Manages per-thread storage of locale-specific DateFormat instances. Use this
 * factory to obtain DateFormat instances to address two issues: (1) Reduce cost
 * of on-the-fly construction of DateFormat instances. The factory uses cached
 * instances whenever possible. (2) MT-safety issue related to use of shared
 * DateFormat instances across threads
 */
public class DateFormatFactory
{

	private static final int NO_TIME_STYLE = -999;

	// TLS HashMap from locale/style key to DateFormat instance
	private static ThreadLocal tlsCache = new ThreadLocal( ) {

		protected Object initialValue( )
		{
			return new HashMap( );
		}
	};

	private static ThreadLocal patternCache = new ThreadLocal( ) {
		
		protected Object initialValue( )
		{
			HashMap value = new HashMap( );
			String[] dateFormatPattern = {
					"yyyy-MM-dd HH:mm:ss.SSS z",
					"yyyy-MM-dd HH:mm:ss.SSS Z",
					"yyyy-MM-dd HH:mm:ss.SSSz",
					"yyyy-MM-dd HH:mm:ss.SSSZ",
					"yyyy-MM-dd HH:mm:ss.SSS",
					"yyyy-MM-dd HH:mm:ss z",
					"yyyy-MM-dd HH:mm:ss Z",
					"yyyy-MM-dd HH:mm:ssz",
					"yyyy-MM-dd HH:mm:ssZ",
					"yyyy-MM-dd HH:mm:ss",
					"yyyy-MM-dd HH:mm z",
					"yyyy-MM-dd HH:mm Z",
					"yyyy-MM-dd HH:mmz",
					"yyyy-MM-dd HH:mmZ",
					"yyyy-MM-dd HH:mm",
					"yyyy-MM-dd",
					"yyyy-MM",
					"yyyy",
					"HH:mm:ss.SSS"
			};
			SimpleDateFormat simpleDateFormatter = null;
			PatternKey patterKey = null;
			
			for ( int i = 0; i < dateFormatPattern.length; i++ )
			{
				patterKey = PatternKey.getPatterKey( dateFormatPattern[i] );
				simpleDateFormatter = new SimpleDateFormat( dateFormatPattern[i] );
				simpleDateFormatter.setLenient( false );
				value.put( patterKey, simpleDateFormatter );
			}
			return value;
		}
	};

	/**
	 * Gets DateFormat instance allocated to the current thread for the given
	 * date style, timestyle and locale. Returned instance is safe to use
	 * 
	 */
	public static DateFormat getDateTimeInstance( int dateStyle, int timeStyle,
			ULocale locale )
	{
		assert locale != null;

		// Create key string for cache lookup
		String keyStr = locale.getName( )
				+ "/" + Integer.toString( dateStyle ) + "/"
				+ Integer.toString( timeStyle );

		HashMap tlsMap = (HashMap) tlsCache.get( );
		assert tlsMap != null;

		DateFormat result = (DateFormat) tlsMap.get( keyStr );

		// Create new instance and add to cache if no instance available for
		// current thread/style/locale combination
		if ( result == null )
		{
			if ( timeStyle == NO_TIME_STYLE )
				result = DateFormat.getDateInstance( dateStyle,
						locale.toLocale( ) );
			else
				result = DateFormat.getDateTimeInstance( dateStyle,
						timeStyle,
						locale.toLocale( ) );
			result.setLenient( false );
			tlsMap.put( keyStr, result );
		}

		return result;

	}

	/**
	 * Gets DateFormat instance allocated to the current thread for the given
	 * date style, timestyle and locale. Returned instance is safe to use
	 * 
	 */
	public static DateFormat getDateInstance( int dateStyle, ULocale locale )
	{
		return getDateTimeInstance( dateStyle, NO_TIME_STYLE, locale );
	}<commit_msg>Fix 58713 Date Time display incorrect when preview script dataset<commit_after>/*
 *************************************************************************
 * Copyright (c) 2006 Actuate Corporation.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 *
 * Contributors:
 *  Actuate Corporation  - initial API and implementation
 *  
 *************************************************************************
 */

package org.eclipse.birt.core.data;

import java.util.HashMap;

import com.ibm.icu.text.DateFormat;
import com.ibm.icu.text.SimpleDateFormat;

import com.ibm.icu.util.ULocale;

/**
 * Manages per-thread storage of locale-specific DateFormat instances. Use this
 * factory to obtain DateFormat instances to address two issues: (1) Reduce cost
 * of on-the-fly construction of DateFormat instances. The factory uses cached
 * instances whenever possible. (2) MT-safety issue related to use of shared
 * DateFormat instances across threads
 */
public class DateFormatFactory
{

	private static final int NO_TIME_STYLE = -999;

	// TLS HashMap from locale/style key to DateFormat instance
	private static ThreadLocal tlsCache = new ThreadLocal( ) {

		protected Object initialValue( )
		{
			return new HashMap( );
		}
	};

	private static ThreadLocal patternCache = new ThreadLocal( ) {
		
		protected Object initialValue( )
		{
			HashMap value = new HashMap( );
			String[] dateFormatPattern = {
					"yyyy-MM-dd HH:mm:ss.SSS z",
					"yyyy-MM-dd HH:mm:ss.SSS Z",
					"yyyy-MM-dd HH:mm:ss.SSSz",
					"yyyy-MM-dd HH:mm:ss.SSSZ",
					"yyyy-MM-dd HH:mm:ss.SSS",
					"yyyy-MM-dd HH:mm:ss z",
					"yyyy-MM-dd HH:mm:ss Z",
					"yyyy-MM-dd HH:mm:ssz",
					"yyyy-MM-dd HH:mm:ssZ",
					"yyyy-MM-dd HH:mm:ss",
					"yyyy-MM-dd HH:mm z",
					"yyyy-MM-dd HH:mm Z",
					"yyyy-MM-dd HH:mmz",
					"yyyy-MM-dd HH:mmZ",
					"yyyy-MM-dd HH:mm",
					"yyyy-MM-dd",
					"yyyy-MM",
					"yyyy",
					"HH:mm:ss.SSS"
			};
			SimpleDateFormat simpleDateFormatter = null;
			PatternKey patterKey = null;
			
			for ( int i = 0; i < dateFormatPattern.length; i++ )
			{
				patterKey = PatternKey.getPatterKey( dateFormatPattern[i] );
				simpleDateFormatter = new SimpleDateFormat( dateFormatPattern[i] );
				simpleDateFormatter.setLenient( false );
				value.put( patterKey, simpleDateFormatter );
			}
			return value;
		}
	};

	/**
	 * Gets DateFormat instance allocated to the current thread for the given
	 * date style, timestyle and locale. Returned instance is safe to use
	 * 
	 */
	public static DateFormat getDateTimeInstance( int dateStyle, int timeStyle,
			ULocale locale )
	{
		assert locale != null;

		// Create key string for cache lookup
		String keyStr = locale.getName( )
				+ "/" + Integer.toString( dateStyle ) + "/"
				+ Integer.toString( timeStyle );

		HashMap tlsMap = (HashMap) tlsCache.get( );
		assert tlsMap != null;

		DateFormat result = (DateFormat) tlsMap.get( keyStr );

		// Create new instance and add to cache if no instance available for
		// current thread/style/locale combination
		if ( result == null )
		{
			if ( timeStyle == NO_TIME_STYLE )
				result = DateFormat.getDateInstance( dateStyle,
						locale.toLocale( ) );
			else
				result = DateFormat.getDateTimeInstance( dateStyle,
						timeStyle,
						locale.toLocale( ) );
			tlsMap.put( keyStr, result );
		}

		return result;

	}

	/**
	 * Gets DateFormat instance allocated to the current thread for the given
	 * date style, timestyle and locale. Returned instance is safe to use
	 * 
	 */
	public static DateFormat getDateInstance( int dateStyle, ULocale locale )
	{
		return getDateTimeInstance( dateStyle, NO_TIME_STYLE, locale );
	}
{'fraction_non_alphanumeric': 0.1426894482848572, 'fraction_numerical': 0.00430416068866571, 'mean_word_length': 3.384219554030875, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6003110', 'n_tokens_mistral': 2819, 'n_tokens_neox': 2319, 'n_words': 693}