<commit_before>/*
 * Copyright (C) 2021 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package androidx.appcompat.app;

import android.os.LocaleList;

import androidx.annotation.RequiresApi;
import androidx.core.os.LocaleListCompat;

import java.util.Locale;

/**
 * Static utilities to overlay locales on top of another LocaleListCompat.
 *
 * <p>This is used to overlay application-specific locales on top of
 *  system locales.</p>
 */
@RequiresApi(24)
final class LocaleOverlayHelper {

    private LocaleOverlayHelper() {}

    /**
     * Combines the overlay locales and base locales.
     * @return the combined {@link LocaleListCompat} if the overlay locales is not empty/null else
     * returns an empty LocaleListCompat.
     */
    static LocaleListCompat combineLocalesIfOverlayExists(LocaleListCompat overlayLocales,
            LocaleListCompat baseLocales) {
        if (overlayLocales == null || overlayLocales.isEmpty()) {
            return LocaleListCompat.getEmptyLocaleList();
        }
        return combineLocales(overlayLocales, baseLocales);
    }

    static LocaleListCompat combineLocalesIfOverlayExists(LocaleList overlayLocales,
            LocaleList baseLocales) {
        if (overlayLocales == null || overlayLocales.isEmpty()) {
            return LocaleListCompat.getEmptyLocaleList();
        }
        return combineLocales(LocaleListCompat.wrap(overlayLocales),
                LocaleListCompat.wrap(baseLocales));
    }

    /**
     * Creates a combined {@link LocaleListCompat} by placing overlay locales before base
     * locales and dropping duplicates from the base locales.
     */
    private static LocaleListCompat combineLocales(LocaleListCompat overlayLocales,
            LocaleListCompat baseLocales) {
        Locale[] combinedLocales =
                new Locale[overlayLocales.size() + baseLocales.size()];
        for (int i = 0; i < overlayLocales.size(); i++) {
            combinedLocales[i] = overlayLocales.get(i);
        }
        for (int i = 0; i < baseLocales.size(); i++) {
            combinedLocales[i + overlayLocales.size()] = baseLocales.get(i);
        }
        LocaleList combineLocaleList = new LocaleList(combinedLocales);
        // Constructor of {@link LocaleListCompat} removes duplicates
        return LocaleListCompat.wrap(combineLocaleList);
    }
}<commit_msg>Modified LocaleOverlayHelper to handle duplicate locales.<commit_after>/*
 * Copyright (C) 2021 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package androidx.appcompat.app;

import android.os.LocaleList;

import androidx.annotation.RequiresApi;
import androidx.core.os.LocaleListCompat;

import java.util.LinkedHashSet;
import java.util.Locale;
import java.util.Set;

/**
 * Static utilities to overlay locales on top of another LocaleListCompat.
 *
 * <p>This is used to overlay application-specific locales on top of
 * system locales.</p>
 */
@RequiresApi(24)
final class LocaleOverlayHelper {

    private LocaleOverlayHelper() {}

    /**
     * Combines the overlay locales and base locales.
     *
     * @return the combined {@link LocaleListCompat} if the overlay locales is not empty/null else
     * returns an empty LocaleListCompat.
     */
    static LocaleListCompat combineLocalesIfOverlayExists(LocaleListCompat overlayLocales,
            LocaleListCompat baseLocales) {
        if (overlayLocales == null || overlayLocales.isEmpty()) {
            return LocaleListCompat.getEmptyLocaleList();
        }
        return combineLocales(overlayLocales, baseLocales);
    }

    static LocaleListCompat combineLocalesIfOverlayExists(LocaleList overlayLocales,
            LocaleList baseLocales) {
        if (overlayLocales == null || overlayLocales.isEmpty()) {
            return LocaleListCompat.getEmptyLocaleList();
        }
        return combineLocales(LocaleListCompat.wrap(overlayLocales),
                LocaleListCompat.wrap(baseLocales));
    }

    /**
     * Creates a combined {@link LocaleListCompat} by placing overlay locales before base
     * locales and dropping duplicates from the base locales.
     */
    private static LocaleListCompat combineLocales(LocaleListCompat overlayLocales,
            LocaleListCompat baseLocales) {
        // using LinkedHashSet to drop duplicates.
        Set<Locale> combinedLocales = new LinkedHashSet<>();
        for (int i = 0; i < overlayLocales.size() + baseLocales.size(); i++) {
            Locale currLocale;
            if (i < overlayLocales.size()) {
                currLocale = overlayLocales.get(i);
            } else {
                currLocale = baseLocales.get(i - overlayLocales.size());
            }
            if (currLocale != null) {
                combinedLocales.add(currLocale);
            }
        }
        return LocaleListCompat.create(combinedLocales.toArray(
                new Locale[combinedLocales.size()]));
    }
}
{'fraction_non_alphanumeric': 0.08338903743315508, 'fraction_numerical': 0.003843582887700535, 'mean_word_length': 3.052132701421801, 'pattern_counts': {'":': 0, '<': 13, '<?xml version=': 0, '>': 9, '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': '2147273', 'n_tokens_mistral': 1626, 'n_tokens_neox': 1557, 'n_words': 491}