<commit_before>/**
 * Copyright (c) 2012-2013 <NAME>
 * Alle Rechte vorbehalten / All Rights Reserved.  Use is subject to license terms.
 *
 * <https://github.com/anba/es6draft>
 */
package com.github.anba.es6draft.runtime.objects.intl;

import com.github.anba.es6draft.runtime.Realm;
import com.github.anba.es6draft.runtime.types.Callable;
import com.github.anba.es6draft.runtime.types.builtins.OrdinaryObject;
import com.ibm.icu.text.DecimalFormat;
import com.ibm.icu.text.NumberFormat;
import com.ibm.icu.util.Currency;
import com.ibm.icu.util.ULocale;

/**
 * <h1>11 NumberFormat Objects</h1>
 * <ul>
 * <li>11.4 Properties of Intl.NumberFormat Instances
 * </ul>
 */
public class NumberFormatObject extends OrdinaryObject {
    /** [[initializedIntlObject]] */
    private boolean initializedIntlObject;

    /** [[initializedNumberFormat]] */
    private boolean initializedNumberFormat;

    /** [[locale]] */
    private String locale;

    /** [[numberingSystem]] */
    private String numberingSystem;

    /** [[style]] */
    private String style;

    /** [[currency]] */
    private String currency;

    /** [[currencyDisplay]] */
    private String currencyDisplay;

    /** [[minimumIntegerDigits]] */
    private int minimumIntegerDigits;

    /** [[minimumFractionDigits]] */
    private int minimumFractionDigits;

    /** [[maximumFractionDigits]] */
    private int maximumFractionDigits;

    /** [[minimumSignificantDigits]] */
    private int minimumSignificantDigits;

    /** [[maximumSignificantDigits]] */
    private int maximumSignificantDigits;

    /** [[useGrouping]] */
    private boolean useGrouping;

    /** [[boundFormat]] */
    private Callable boundFormat;

    private NumberFormat numberFormat;

    public NumberFormatObject(Realm realm) {
        super(realm);
    }

    public NumberFormat getNumberFormat() {
        if (numberFormat == null) {
            numberFormat = createNumberFormat();
        }
        return numberFormat;
    }

    private NumberFormat createNumberFormat() {
        ULocale locale = ULocale.forLanguageTag(this.locale);
        int choice;
        if ("decimal".equals(style)) {
            choice = NumberFormat.NUMBERSTYLE;
        } else if ("percent".equals(style)) {
            choice = NumberFormat.PERCENTSTYLE;
        } else {
            if ("code".equals(currencyDisplay)) {
                choice = NumberFormat.ISOCURRENCYSTYLE;
            } else if ("symbol".equals(currencyDisplay)) {
                choice = NumberFormat.CURRENCYSTYLE;
            } else {
                choice = NumberFormat.PLURALCURRENCYSTYLE;
            }
        }
        DecimalFormat numberFormat = (DecimalFormat) NumberFormat.getInstance(locale, choice);
        if ("currency".equals(style)) {
            numberFormat.setCurrency(Currency.getInstance(currency));
        }
        // numberingSystem is already handled in language-tag
        // assert locale.getKeywordValue("numbers").equals(numberingSystem);
        if (minimumSignificantDigits != 0 && maximumSignificantDigits != 0) {
            numberFormat.setSignificantDigitsUsed(true);
            numberFormat.setMinimumSignificantDigits(minimumSignificantDigits);
            numberFormat.setMaximumFractionDigits(maximumSignificantDigits);
        } else {
            numberFormat.setMinimumIntegerDigits(minimumIntegerDigits);
            numberFormat.setMinimumFractionDigits(minimumFractionDigits);
            numberFormat.setMaximumFractionDigits(maximumFractionDigits);
        }
        numberFormat.setGroupingUsed(useGrouping);
        return numberFormat;
    }

    /** [[initializedIntlObject]] */
    public boolean isInitializedIntlObject() {
        return initializedIntlObject;
    }

    /** [[initializedIntlObject]] */<commit_msg>Set the proper rounding mode for Intl.NumberFormat<commit_after>/**
 * Copyright (c) 2012-2013 <NAME>
 * Alle Rechte vorbehalten / All Rights Reserved.  Use is subject to license terms.
 *
 * <https://github.com/anba/es6draft>
 */
package com.github.anba.es6draft.runtime.objects.intl;

import com.github.anba.es6draft.runtime.Realm;
import com.github.anba.es6draft.runtime.types.Callable;
import com.github.anba.es6draft.runtime.types.builtins.OrdinaryObject;
import com.ibm.icu.math.BigDecimal;
import com.ibm.icu.text.DecimalFormat;
import com.ibm.icu.text.NumberFormat;
import com.ibm.icu.util.Currency;
import com.ibm.icu.util.ULocale;

/**
 * <h1>11 NumberFormat Objects</h1>
 * <ul>
 * <li>11.4 Properties of Intl.NumberFormat Instances
 * </ul>
 */
public class NumberFormatObject extends OrdinaryObject {
    /** [[initializedIntlObject]] */
    private boolean initializedIntlObject;

    /** [[initializedNumberFormat]] */
    private boolean initializedNumberFormat;

    /** [[locale]] */
    private String locale;

    /** [[numberingSystem]] */
    private String numberingSystem;

    /** [[style]] */
    private String style;

    /** [[currency]] */
    private String currency;

    /** [[currencyDisplay]] */
    private String currencyDisplay;

    /** [[minimumIntegerDigits]] */
    private int minimumIntegerDigits;

    /** [[minimumFractionDigits]] */
    private int minimumFractionDigits;

    /** [[maximumFractionDigits]] */
    private int maximumFractionDigits;

    /** [[minimumSignificantDigits]] */
    private int minimumSignificantDigits;

    /** [[maximumSignificantDigits]] */
    private int maximumSignificantDigits;

    /** [[useGrouping]] */
    private boolean useGrouping;

    /** [[boundFormat]] */
    private Callable boundFormat;

    private NumberFormat numberFormat;

    public NumberFormatObject(Realm realm) {
        super(realm);
    }

    public NumberFormat getNumberFormat() {
        if (numberFormat == null) {
            numberFormat = createNumberFormat();
        }
        return numberFormat;
    }

    private NumberFormat createNumberFormat() {
        ULocale locale = ULocale.forLanguageTag(this.locale);
        int choice;
        if ("decimal".equals(style)) {
            choice = NumberFormat.NUMBERSTYLE;
        } else if ("percent".equals(style)) {
            choice = NumberFormat.PERCENTSTYLE;
        } else {
            if ("code".equals(currencyDisplay)) {
                choice = NumberFormat.ISOCURRENCYSTYLE;
            } else if ("symbol".equals(currencyDisplay)) {
                choice = NumberFormat.CURRENCYSTYLE;
            } else {
                choice = NumberFormat.PLURALCURRENCYSTYLE;
            }
        }
        DecimalFormat numberFormat = (DecimalFormat) NumberFormat.getInstance(locale, choice);
        if ("currency".equals(style)) {
            numberFormat.setCurrency(Currency.getInstance(currency));
        }
        // numberingSystem is already handled in language-tag
        // assert locale.getKeywordValue("numbers").equals(numberingSystem);
        if (minimumSignificantDigits != 0 && maximumSignificantDigits != 0) {
            numberFormat.setSignificantDigitsUsed(true);
            numberFormat.setMinimumSignificantDigits(minimumSignificantDigits);
            numberFormat.setMaximumFractionDigits(maximumSignificantDigits);
        } else {
            numberFormat.setMinimumIntegerDigits(minimumIntegerDigits);
            numberFormat.setMinimumFractionDigits(minimumFractionDigits);
            numberFormat.setMaximumFractionDigits(maximumFractionDigits);
        }
        numberFormat.setGroupingUsed(useGrouping);
        // as required by ToRawPrecision/ToRawFixed
        // FIXME: ICU4J bug:
        // new Intl.NumberFormat("en",{useGrouping:false}).format(111111111111111)
        // returns "111111111111111.02"
        numberFormat.setRoundingMode(BigDecimal.ROUND_HALF_UP);
        return numberFormat;
    }

    /** [[initializedIntlObject]] */
    public boolean isInitializedIntlObject() {
        return initializedIntlObject;
    }

    /** [[initializedIntlObject]] */
{'fraction_non_alphanumeric': 0.11332408924744737, 'fraction_numerical': 0.009706290180259675, 'mean_word_length': 2.935515873015873, 'pattern_counts': {'":': 0, '<': 17, '<?xml version=': 0, '>': 17, 'https://': 2, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 2, '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': '1894720', 'n_tokens_mistral': 2311, 'n_tokens_neox': 2171, 'n_words': 400}