/* ----------------------------------------
 * Origin  : GFI INFORMATIQUE 
 * Project : CDPP Propagation Tool 
 * ----------------------------------------
 * History 
 * Version : 01/05/2015 : SCA : V2.1
 * Version : 25/11/2024 : LBE : V2.11
 * DM-ID : Redmine#11104 : 25/11/2024 : VDA module added
 * ----------------------------------------
 */
package fr.tlse.irap.pt.client.controller.common;

import java.text.DecimalFormat;
import java.text.DecimalFormatSymbols;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.Locale;

public class Formatter {

	private static final DecimalFormat decimalFormatter0D = new DecimalFormat(
			"#", new DecimalFormatSymbols(Locale.US));

	private static final DecimalFormat decimalFormatter = new DecimalFormat(
			"#.##", new DecimalFormatSymbols(Locale.US));

	private static final DecimalFormat decimalFormatter1DFixed = new DecimalFormat(
			"0.0", new DecimalFormatSymbols(Locale.US));

	private static final DecimalFormat decimalFormatter1D = new DecimalFormat(
			"#.#", new DecimalFormatSymbols(Locale.US));

	private static final DecimalFormat decimalFormatter2D = new DecimalFormat(
			"#.##", new DecimalFormatSymbols(Locale.US));
        
	private static final DecimalFormat decimalFormatter3D = new DecimalFormat(
			"#.###", new DecimalFormatSymbols(Locale.US))
                ;
	private static final DecimalFormat decimalFormatter4D = new DecimalFormat(
			"#.####", new DecimalFormatSymbols(Locale.US));

	private static final DecimalFormat decimalFormatterExp = new DecimalFormat(
			"#.##E0", new DecimalFormatSymbols(Locale.US));


	public static DecimalFormat getFormatter() {
		return decimalFormatter;
	}

	public static DecimalFormat getFormatter0D() {
		return decimalFormatter0D;
	}

	public static DecimalFormat getFormatter1DFixed() {
		return decimalFormatter1DFixed;
	}

	public static DecimalFormat getFormatter1D() {
		return decimalFormatter1D;
	}
        
	public static DecimalFormat getFormatter2D() {
		return decimalFormatter2D;
	}
        
	public static DecimalFormat getFormatter3D() {
		return decimalFormatter3D;
	}

	public static DecimalFormat getFormatter4D() {
		return decimalFormatter4D;
	}

	public static DecimalFormat getFormatterExp() {
		return decimalFormatterExp;
	}

	public static SimpleDateFormat getSdf() {
		return new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss");
	}

	public static SimpleDateFormat getTdf() {
		return new SimpleDateFormat("yyyy-MM-dd\n'T'HH:mm:ss");
	}

	public static String toSdf(Calendar cal) {
		return (cal == null) ? null : new SimpleDateFormat("HH:mm:ss").format(
				cal.getTime());
	}

	public static String toSdf(Date time) {
		return (time == null) ? null : new SimpleDateFormat("HH:mm:ss").format(time);
	}

	public static String toDecimal(Double value, DecimalFormat formatter) {
		return (value == null) ? null : formatter.format(value);
	}

	public static String toDecimal(Double value) {
		return (value == null) ? null : Formatter.getFormatter().format(value);
	}

	public static String toDecimal1D(Double value) {
		return (value == null) ? null : Formatter.getFormatter1D()
				.format(value);
	}

	public static String toDecimal0D(Double value) {
		return (value == null) ? null : Formatter.getFormatter0D()
				.format(value);
	}

	public static String toDecimal4D(Double value) {
		return (value == null) ? null : Formatter.getFormatter4D()
				.format(value);
	}

	public static String toDecimalExp(Double value) {
		return (value == null) ? null : Formatter.getFormatterExp()
				.format(value);
	}

	public static Double toDouble(String dValue, Double defaultValue) {
		Double value = 0.0;
		try {
			if (dValue == null || dValue.isEmpty())
				return defaultValue;
			value = Formatter.getFormatter().parse(dValue).doubleValue();
			return value;
		} catch (ParseException e1) {
			return value;
		}
	}

	public static Double toDouble(String dValue) {
		return toDouble(dValue, 0.0);
	}

	public static Double toDouble1D(String dValue) {
		Double value = 0.0;
		try {
			if (dValue == null || dValue.isEmpty())
				return value;
			value = Formatter.getFormatter1D().parse(dValue).doubleValue();
			return value;
		} catch (ParseException e1) {
			return value;
		}
	}

	public static Double toDouble1D(String dValue, double defaultValue) {
		Double value = defaultValue;
		try {
			if (dValue == null || dValue.isEmpty())
				return value;
			value = Formatter.getFormatter1D().parse(dValue).doubleValue();
			return value;
		} catch (ParseException e1) {
			return value;
		}
	}

	public static Double toDouble4D(String dValue) {
		Double value = 0.0;
		try {
			if (dValue == null || dValue.isEmpty())
				return value;
			value = Formatter.getFormatter4D().parse(dValue).doubleValue();
			return value;
		} catch (ParseException e1) {
			return value;
		}
	}


}
