// Path: library/src/main/java/com/franmontiel/localechanger/utils/LocaleMatcher.java
// public class LocaleMatcher {
// 
//     /**
//      * Enum representing the level of matching of two Locales.
//      */
//     public enum MatchLevel {
//         NoMatch,
//         LanguageMatch,
//         LanguageAndCountryMatch,
//         CompleteMatch
//     }
// 
//     private LocaleMatcher() {
//     }
// 
//     /**
//      * Method to determine the level of matching of two Locales.
//      * @param l1
//      * @param l2
//      * @return
//      */
//     public static MatchLevel match(Locale l1, Locale l2) {
//         MatchLevel matchLevel = NoMatch;
//         if (l1.equals(l2)) {
//             matchLevel = MatchLevel.CompleteMatch;
//         } else if (l1.getLanguage().equals(l2.getLanguage()) && l1.getCountry().equals(l2.getCountry())) {
//             return MatchLevel.LanguageAndCountryMatch;
//         } else if (l1.getLanguage().equals(l2.getLanguage())) {
//             return MatchLevel.LanguageMatch;
//         }
//         return matchLevel;
//     }
// }


import com.franmontiel.localechanger.utils.LocaleMatcher;
import java.util.List;
import java.util.Locale;
/*
 * Copyright (c)  2017  Francisco José Montiel Navarro.
 *
 * 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 com.franmontiel.localechanger.matcher;



/**
 * An algorithm that match the Locales the same language in common.
 */
public final class LanguageMatchingAlgorithm implements MatchingAlgorithm {

    @Override
    public MatchingLocales findDefaultMatch(List<Locale> supportedLocales, List<Locale> systemLocales) {
        MatchingLocales matchingPair = null;
        for (Locale systemLocale : systemLocales) {
            Locale matchingSupportedLocale = findMatchingLocale(systemLocale, supportedLocales);
            if (matchingSupportedLocale != null) {
                matchingPair = new MatchingLocales(matchingSupportedLocale, systemLocale);
                break;
            }
        }
        return matchingPair;
    }

    @Override
    public MatchingLocales findMatch(Locale supportedLocale, List<Locale> systemLocales) {
        Locale matchingSystemLocale = findMatchingLocale(supportedLocale, systemLocales);
        return matchingSystemLocale != null ?
                new MatchingLocales(supportedLocale, matchingSystemLocale) :
                null;
    }

    private static Locale findMatchingLocale(Locale localeToMatch, List<Locale> candidates) {
        Locale matchingLocale = null;
        for (Locale candidate : candidates) {
// Path: library/src/main/java/com/franmontiel/localechanger/utils/LocaleMatcher.java
// public class LocaleMatcher {
// 
//     /**
//      * Enum representing the level of matching of two Locales.
//      */
//     public enum MatchLevel {
//         NoMatch,
//         LanguageMatch,
//         LanguageAndCountryMatch,
//         CompleteMatch
//     }
// 
//     private LocaleMatcher() {
//     }
// 
//     /**
//      * Method to determine the level of matching of two Locales.
//      * @param l1
//      * @param l2
//      * @return
//      */
//     public static MatchLevel match(Locale l1, Locale l2) {
//         MatchLevel matchLevel = NoMatch;
//         if (l1.equals(l2)) {
//             matchLevel = MatchLevel.CompleteMatch;
//         } else if (l1.getLanguage().equals(l2.getLanguage()) && l1.getCountry().equals(l2.getCountry())) {
//             return MatchLevel.LanguageAndCountryMatch;
//         } else if (l1.getLanguage().equals(l2.getLanguage())) {
//             return MatchLevel.LanguageMatch;
//         }
//         return matchLevel;
//     }
// }


// Path: library/src/main/java/com/franmontiel/localechanger/matcher/LanguageMatchingAlgorithm.java
import com.franmontiel.localechanger.utils.LocaleMatcher;
import java.util.List;
import java.util.Locale;

/*
 * Copyright (c)  2017  Francisco José Montiel Navarro.
 *
 * 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 com.franmontiel.localechanger.matcher;



/**
 * An algorithm that match the Locales the same language in common.
 */
public final class LanguageMatchingAlgorithm implements MatchingAlgorithm {

    @Override
    public MatchingLocales findDefaultMatch(List<Locale> supportedLocales, List<Locale> systemLocales) {
        MatchingLocales matchingPair = null;
        for (Locale systemLocale : systemLocales) {
            Locale matchingSupportedLocale = findMatchingLocale(systemLocale, supportedLocales);
            if (matchingSupportedLocale != null) {
                matchingPair = new MatchingLocales(matchingSupportedLocale, systemLocale);
                break;
            }
        }
        return matchingPair;
    }

    @Override
    public MatchingLocales findMatch(Locale supportedLocale, List<Locale> systemLocales) {
        Locale matchingSystemLocale = findMatchingLocale(supportedLocale, systemLocales);
        return matchingSystemLocale != null ?
                new MatchingLocales(supportedLocale, matchingSystemLocale) :
                null;
    }

    private static Locale findMatchingLocale(Locale localeToMatch, List<Locale> candidates) {
        Locale matchingLocale = null;
        for (Locale candidate : candidates) {
            LocaleMatcher.MatchLevel matchLevel = LocaleMatcher.match(localeToMatch, candidate);