Example usage for java.util Locale filter

List of usage examples for java.util Locale filter

Introduction

In this page you can find the example usage for java.util Locale filter.

Prototype

public static List<Locale> filter(List<LanguageRange> priorityList, Collection<Locale> locales) 

Source Link

Document

Returns a list of matching Locale instances using the filtering mechanism defined in RFC 4647.

Usage

From source file:com.michellemay.mappings.MappingsFactory.java

private Mapping createCustomMapping(MappingConfig mappingConfig) {
    if (StringUtils.isBlank(mappingConfig.name)) {
        throw new IllegalArgumentException("Blank mapping name!");
    }//w w  w. j av  a  2 s  .com

    Map<String, Locale> curMap = new TreeMap<String, Locale>(
            mappingConfig.casesensitive ? null : String.CASE_INSENSITIVE_ORDER);

    // Inherit all mappings from bases
    if (mappingConfig.extend != null) {
        for (String baseMappingName : mappingConfig.extend) {
            if (StringUtils.isBlank(baseMappingName) || !mappings.containsKey(baseMappingName)) {
                throw new IllegalStateException("Base mapping name '" + baseMappingName + "' does not exists!");
            }
            Mapping baseMapping = mappings.get(baseMappingName);
            baseMapping.getMapping().forEach(curMap::putIfAbsent);
        }
    }

    // Filter out unwanted languages
    if (StringUtils.isNotBlank(mappingConfig.filter)) {
        List<Locale.LanguageRange> priorityList = Locale.LanguageRange.parse(mappingConfig.filter);
        List<Locale> toKeep = Locale.filter(priorityList, curMap.values());
        curMap.entrySet().removeIf(e -> !toKeep.contains(e.getValue()));
    }

    // Add new values
    if (mappingConfig.add != null) {
        mappingConfig.add.forEach((lang, values) -> {
            Locale langLocale = LocaleUtils.toLocale(lang);
            String[] displayValues = values.split(",");
            for (String value : displayValues) {
                String cleanedValue = value.trim();
                if (!cleanedValue.isEmpty()) {
                    curMap.putIfAbsent(cleanedValue, langLocale);
                }
            }
        });
    }

    // Override values
    if (mappingConfig.override != null) {
        mappingConfig.override.forEach((lang, values) -> {
            Locale langLocale = LocaleUtils.toLocale(lang);

            // Remove all existing mappings
            String langTag = langLocale.toLanguageTag();
            curMap.entrySet().removeIf(e -> e.getValue().toLanguageTag().equals(langTag));

            // Add new mappings.
            String[] displayValues = values.split(",");
            for (String value : displayValues) {
                String cleanedValue = value.trim();
                if (!cleanedValue.isEmpty()) {
                    curMap.put(cleanedValue, langLocale);
                }
            }
        });
    }

    return new CustomMapping(mappingConfig.name.trim()).withCaseSensitive(mappingConfig.casesensitive)
            .withMapping(curMap);
}