Main.java Source code

Java tutorial

Introduction

Here is the source code for Main.java

Source

//package com.java2s;
/*
 * Copyright (C) 2011 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.
 */

import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;
import java.util.Locale;

public class Main {
    /**
     * Compares locales in case-insensitive ascending order based on their
     * display name.
     */
    public static final Comparator<Locale> LOCALE_COMPARATOR = new Comparator<Locale>() {
        @Override
        public int compare(Locale lhs, Locale rhs) {
            return lhs.getDisplayName().compareToIgnoreCase(rhs.getDisplayName());
        }
    };

    /**
     * Parses the contents of {@link Engine#EXTRA_AVAILABLE_VOICES} and returns
     * a unmodifiable list of {@link Locale}s sorted by display name. See
     * {@link #LOCALE_COMPARATOR} for sorting information.
     *
     * @param availableLanguages A list of locale strings in the form
     *            {@code language-country-variant}.
     * @return A sorted, unmodifiable list of {@link Locale}s.
     */
    public static List<Locale> parseAvailableLanguages(List<String> availableLanguages) {
        final List<Locale> results = new ArrayList<Locale>(availableLanguages.size());

        for (String availableLang : availableLanguages) {
            final String[] langCountryVar = availableLang.split("-");
            final Locale loc;

            if (langCountryVar.length == 1) {
                loc = new Locale(langCountryVar[0]);
            } else if (langCountryVar.length == 2) {
                loc = new Locale(langCountryVar[0], langCountryVar[1]);
            } else if (langCountryVar.length == 3) {
                loc = new Locale(langCountryVar[0], langCountryVar[1], langCountryVar[2]);
            } else {
                continue;
            }

            results.add(loc);
        }

        // Sort by display name, ascending case-insensitive.
        Collections.sort(results, LOCALE_COMPARATOR);

        return Collections.unmodifiableList(results);
    }
}