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

com.nls.util.Region Maven / Gradle / Ivy

There is a newer version: 1.0.14
Show newest version
package com.nls.util;

import java.text.Collator;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;
import java.util.Locale;
import java.util.Objects;
import java.util.stream.Collectors;

public class Region {
    private final Regions.Name code;
    private final String name;
    private final List countries;

    public Region(Regions.Name code, String name, String... countries) {
        this(code, name, Arrays.stream(countries)
                .map(Countries::fromIso)
                .filter(Objects::nonNull)
                .collect(Collectors.toList()));
    }

    public Region(Regions.Name code, String name, List countries) {
        this.code = code;
        this.name = name;
        this.countries = Collections.unmodifiableList(countries);
    }

    public String getName() {
        return name;
    }

    public List getCountries() {
        return countries.stream()
                .filter(o -> Countries.fromIso(o.getIso()) != null)
                .collect(Collectors.toList());
    }

    public List getCountries(Locale locale) {
        Collator coll = Collator.getInstance(locale);
        coll.setStrength(Collator.PRIMARY);
        return countries.stream()
                .filter(o -> Countries.fromIso(o.getIso()) != null)
                .sorted((l, r) -> coll.compare(l.getName(locale), r.getName(locale)))
                .collect(Collectors.toList());
    }

    public Regions.Name getCode() {
        return code;
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy