org.meeuw.i18n.regions.bind.jaxb.Code Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of i18n-regions Show documentation
Show all versions of i18n-regions Show documentation
Provides the service loader for Regions, and some utilities to deal with regions generally
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