All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.meeuw.i18n.regions.Regions Maven / Gradle / Ivy

Go to download

Provides the service loader for Regions, and some utilities to deal with regions generally

There is a newer version: 2.1.0
Show newest version
package org.meeuw.i18n.regions;

import java.util.Comparator;
import java.util.Locale;

import org.checkerframework.checker.nullness.qual.NonNull;

import com.neovisionaries.i18n.LanguageCode;

/**
 * Utilities related to {@link Region}s.
 * @author Michiel Meeuwissen
 * @since 0.1
 */
public class Regions {

    private Regions() {

    }


    /**
     * A {@link Comparator} to sort regions by {@link Region#getName(Locale)}, and hence in a certain language.
     * @param locale
     */
    public static Comparator sortByName(Locale locale) {
        return Comparator.comparing(o -> o.getName(locale));
    }

      /**
       * As {@link #sortByName(Locale)}, but with a {@link LanguageCode } argument.
       */
    public static Comparator sortByName(LanguageCode language) {
        return sortByName(language.toLocale());
    }

    /**
     * Utility for {@link Region#toStringBuilder(StringBuilder, Locale)} without the hassle of creating a {@link StringBuilder}
     */
    public static String toString(
        @NonNull Region region,
        @NonNull Locale language) {
        StringBuilder builder = new StringBuilder();
        region.toStringBuilder(builder, language);
        return builder.toString();
    }

    /**
     * As {@link #toString(Region, Locale)}, but with a {@link LanguageCode} argument.
     */
    public static String toString(
        @NonNull  Region region,
        @NonNull  LanguageCode language) {
        return toString(region, language.toLocale());
    }

    /**
     * As {@link Region#toStringBuilder(StringBuilder, Locale)} but  prefixed with the code.
     */
    public static String toStringWithCode(
        @NonNull Region region,
        @NonNull Locale language) {
        StringBuilder builder = new StringBuilder();
        builder.append(region.getCode());
        builder.append(':');
        region.toStringBuilder(builder, language);
        return builder.toString();
    }


    /**
     * As {@link #toStringWithCode(Region, Locale)} but with a {@link LanguageCode} argument.
     */
    public static String toStringWithCode(
        @NonNull Region region,
        @NonNull LanguageCode language) {
        return toStringWithCode(region, language.toLocale());
    }




}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy