<commit_before>/*
 * jETeL/CloverETL - Java based ETL application framework.
 * Copyright (c) Javlin, a.s. (<EMAIL>)
 *  
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
 */
package org.jetel.ctl.extensions;

import java.util.Objects;

import org.jetel.ctl.TransformLangExecutorRuntimeException;
import org.jetel.util.formatter.DateFormatter;
import org.jetel.util.formatter.DateFormatterFactory;

/**
 * @author jakub (<EMAIL>)
 *         (c) (c) Javlin, a.s. (www.javlin.eu) (www.cloveretl.com)
 *
 * @created May 25, 2010
 */
public class TLDateFormatLocaleCache extends TLFormatterCache {

	private DateFormatter cachedFormatter;
	
	private TLDateFormatLocaleCache(TLFunctionCallContext context) {
		super(context);
	}
	
	/**
	 * @deprecated Use {@link #TLDateFormatLocaleCache(TLFunctionCallContext, int, int, int)} instead. 
	 */
	@Deprecated
	public TLDateFormatLocaleCache(TLFunctionCallContext context, int patternPos, int localePos) {
		this(context);
		createCachedLocaleFormat(context, patternPos, localePos, -1);
	}

	public TLDateFormatLocaleCache(TLFunctionCallContext context, int patternPos, int localePos, int timeZonePos) {
		this(context);
		createCachedLocaleFormat(context, patternPos, localePos, timeZonePos);
	}
	
	public void createCachedLocaleFormat(TLFunctionCallContext context, int patternPos, int localePos, int timeZonePos) {
		
		if (context.getLiteralsSize() <= patternPos) {
			return;
		}
		
		Object paramPattern = context.getParamValue(patternPos);

		// CLO-1190 - null check added
		if ((paramPattern != null) && !(paramPattern instanceof String)) {
			return;
		}
		
		// this construction which allows overloading, where you can use the parameter on the same position as 'format' but 
		// in different meaning with overloaded functions (as long as it is not 'String')
		if (context.getLiteralsSize() <= localePos || !(context.getParamValue(localePos) instanceof String)) {
			String pattern = (String) paramPattern;
			if (context.isLiteral(patternPos)) {
				cachedFormatter = DateFormatterFactory.getFormatter(pattern, context.getDefaultLocale(), context.getDefaultTimeZone());
			}
			return;
		}
		
		if (context.getLiteralsSize() > timeZonePos) {
			if (context.isLiteral(patternPos) && context.isLiteral(localePos) && context.isLiteral(timeZonePos)) {
				String pattern = (String) paramPattern;
				String paramLocale = (String) context.getParamValue(localePos);
				String paramTimeZone = (String) context.getParamValue(timeZonePos);
				cachedFormatter = DateFormatterFactory.getFormatter(pattern, getLocale(paramLocale), getTimeZone(paramTimeZone));
			}
		} else {
			if (context.isLiteral(patternPos) && context.isLiteral(localePos)) {
				String pattern = (String) paramPattern;
				String paramLocale = (String) context.getParamValue(localePos);
				cachedFormatter = DateFormatterFactory.getFormatter(pattern, getLocale(paramLocale), context.getDefaultTimeZone());
			}
		}
		
	}
	
	private DateFormatter getCachedFormatter3(TLFunctionCallContext context, 
			String format, String localeString, String timeZoneString,
			int patternPos, int localePos, int timeZonePos) {
		// if we use the variant with format, locale and time zone specified
		if ((context.isLiteral(patternPos) && context.isLiteral(localePos) && context.isLiteral(timeZonePos))
				// either format, locale and timeZone were literals (thus cached at init)<commit_msg>FIX: CLO-6306 - CTL2: Date format cache fixed to accept null literal as locale.<commit_after>/*
 * jETeL/CloverETL - Java based ETL application framework.
 * Copyright (c) Javlin, a.s. (<EMAIL>)
 *  
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
 */
package org.jetel.ctl.extensions;

import java.util.Objects;

import org.jetel.ctl.TransformLangExecutorRuntimeException;
import org.jetel.util.formatter.DateFormatter;
import org.jetel.util.formatter.DateFormatterFactory;

/**
 * @author jakub (<EMAIL>)
 *         (c) (c) Javlin, a.s. (www.javlin.eu) (www.cloveretl.com)
 *
 * @created May 25, 2010
 */
public class TLDateFormatLocaleCache extends TLFormatterCache {

	private DateFormatter cachedFormatter;
	
	private TLDateFormatLocaleCache(TLFunctionCallContext context) {
		super(context);
	}
	
	/**
	 * @deprecated Use {@link #TLDateFormatLocaleCache(TLFunctionCallContext, int, int, int)} instead. 
	 */
	@Deprecated
	public TLDateFormatLocaleCache(TLFunctionCallContext context, int patternPos, int localePos) {
		this(context);
		createCachedLocaleFormat(context, patternPos, localePos, -1);
	}

	public TLDateFormatLocaleCache(TLFunctionCallContext context, int patternPos, int localePos, int timeZonePos) {
		this(context);
		createCachedLocaleFormat(context, patternPos, localePos, timeZonePos);
	}
	
	public void createCachedLocaleFormat(TLFunctionCallContext context, int patternPos, int localePos, int timeZonePos) {
		
		if (context.getLiteralsSize() <= patternPos) {
			return;
		}
		
		Object paramPattern = context.getParamValue(patternPos);

		// CLO-1190 - null check added
		if ((paramPattern != null) && !(paramPattern instanceof String)) {
			return;
		}
		
		// CLO-6306: instanceof check removed
		if (context.getLiteralsSize() <= localePos) {
			String pattern = (String) paramPattern;
			if (context.isLiteral(patternPos)) {
				cachedFormatter = DateFormatterFactory.getFormatter(pattern, context.getDefaultLocale(), context.getDefaultTimeZone());
			}
			return;
		}
		
		if (context.getLiteralsSize() > timeZonePos) {
			if (context.isLiteral(patternPos) && context.isLiteral(localePos) && context.isLiteral(timeZonePos)) {
				String pattern = (String) paramPattern;
				String paramLocale = (String) context.getParamValue(localePos);
				String paramTimeZone = (String) context.getParamValue(timeZonePos);
				cachedFormatter = DateFormatterFactory.getFormatter(pattern, getLocale(paramLocale), getTimeZone(paramTimeZone));
			}
		} else {
			if (context.isLiteral(patternPos) && context.isLiteral(localePos)) {
				String pattern = (String) paramPattern;
				String paramLocale = (String) context.getParamValue(localePos);
				cachedFormatter = DateFormatterFactory.getFormatter(pattern, getLocale(paramLocale), context.getDefaultTimeZone());
			}
		}
		
	}
	
	private DateFormatter getCachedFormatter3(TLFunctionCallContext context, 
			String format, String localeString, String timeZoneString,
			int patternPos, int localePos, int timeZonePos) {
		// if we use the variant with format, locale and time zone specified
		if ((context.isLiteral(patternPos) && context.isLiteral(localePos) && context.isLiteral(timeZonePos))
				// either format, locale and timeZone were literals (thus cached at init)
{'fraction_non_alphanumeric': 0.09470264867566217, 'fraction_numerical': 0.008120939530234883, 'mean_word_length': 4.758992805755396, 'pattern_counts': {'":': 0, '<': 11, '<?xml version=': 0, '>': 9, 'https://': 0, 'lorem ipsum': 0, 'www.': 4, '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': '1090948', 'n_tokens_mistral': 2454, 'n_tokens_neox': 2200, 'n_words': 693}