An open API service indexing awesome lists of open source software.

https://github.com/codice/countrycode

Initial Country Code converter
https://github.com/codice/countrycode

Last synced: 3 months ago
JSON representation

Initial Country Code converter

Awesome Lists containing this project

README

          

[![Build Status](https://travis-ci.org/codice/countrycode.svg?branch=master)](https://travis-ci.org/codice/countrycode)

# Country Code Library

The Country Code library provides facilities for converting between different country code standards.
This software is currently in a Beta form and is subject to change

## Usage

Add the Codice Repository to your build
```

codice
Codice Repository
https://artifacts.codice.org/content/groups/public/

```

Add the Converter Dependency

```

org.codice.countrycode
converter
${countryconverter.version}

```

Happy Converting!

## CountryCodeSimple

Simple interface for the typical use case of converting between the following standards: ISO 3166-1,
GENC 3.0.0, and FIPS 10-4, GEC Update 18.

```
String countryCode = CountryCodeSimple.convert("CH", FIPS_10_4_ALPHA2, ISO_3166_1_ALPHA3)
assert countryCode.equals("CHN");
```

## Supported Standards
Standards are available in various formats. For example, the ISO 3166-1 `alpha2` format for the country
Afghanistan is `AF`, while its `alpha3` and `numeric` formats are `AFG` and `004`, respectively.

There are 4 currently supported standards and their respective available formats (the `name` and `version`
in parenthesis, respectively, can be used to lookup the standard in the default `StandardRegistry`):
* FIPS 10-4 (`FIPS`, `10-4`)
- alpha2
* GEC 18 (`GEC`, `18`)
- alpha2
* ISO 3166-1 (`ISO3166`, `1`)
- alpha2
- alpha3
- numeric
* GENC 3.0.0 (`GENC`, `3.0.0`)
- alpha2
- alpha3
- numeric

A country code can be retrieved for a given format with the following:

CountryCode countryCode = new CountryCode();
String alpha2Format = countryCode.getAsFormat("alpha2"); // ex, "AF"

## Low-level Interfaces


Converter

The converter is used to convert between country code standards. Standards can be retrieved via the `StandardRegistry`.

```
Converter converter = new CountryCodeConverter();

StandardRegistry registry = StandardRegistryImpl.getInstance();
StandardProvider fipsStandard = registry.lookup("FIPS", "10-4");
StandardProvider isoStandard = registry.lookup("ISO3166", "1");

// converting from FIPS 10-4 to ISO 3166-1 with alpha2
Set convertedCountryCodes = converter.fromAlpha2("AF", fipsStandard.getStandard(), isoStandard.getStandard());

// converting from FIPS 10-4 to ISO 3166-1 with alpha3
Set convertedCountryCodes = converter.fromAlpha3("AFG", fipsStandard.getStandard(), isoStandard.getStandard());

// converting from FIPS 10-4 to ISO 3166-1 with numeric
Set convertedCountryCodes = converter.fromNumeric("004", fipsStandard.getStandard(), isoStandard.getStandard());

```


StandardProvider


```
StandardRegistry registry = StandardRegistryImpl.getInstance();
StandardProvider isoStandard = registry.lookup("ISO3166", "1");

// converting from ISO 3166-1 alpha2 to ISO 3166-1 alpha3
Optional optionalCountryCode =
isoStandard
.getStandardEntries()
.stream()
.filter(c -> c.getAsFormat("alpha2").equals("AT"))
.findFirst();

if (optionalCountryCode.isPresent())
System.out.println(optionalCountryCode.get().getAsFormat("alpha3"));

```