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

org.meeuw.i18n.regions.bind.jaxb.Code 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.bind.jaxb;

import java.util.Optional;

import javax.xml.bind.annotation.adapters.XmlAdapter;

import org.checkerframework.checker.nullness.qual.Nullable;
import org.meeuw.i18n.regions.Region;
import org.meeuw.i18n.regions.RegionService;

/**
 * An XML Adapter for Regions. The obvious way to marshal/unmarshal is using {@link Region#getCode()}
 *
 * This is used to annotate {@link Region} itself, but it can also be used in specialized XMLWrappers to represent the code (besides e.g. the name of the country).
 *
 * @author Michiel Meeuwissen
 * @since 0.1
 */
public class Code extends XmlAdapter {
    @Override
    public Region unmarshal(String v) {
        return RegionService.getInstance().getByCode(v, true)
            .orElseThrow(() -> new IllegalArgumentException("No such region " + v));
    }

    @Override
    @Nullable
    public String marshal(@Nullable Region countryCode) {
        return Optional.ofNullable(countryCode)
            .map(Region::getCode)
            .orElse(null);
    }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy