Skip to content

Instantly share code, notes, and snippets.

@anuvrat
Last active August 31, 2015 16:15
Show Gist options
  • Save anuvrat/93cf4383674b2927a32e to your computer and use it in GitHub Desktop.
Save anuvrat/93cf4383674b2927a32e to your computer and use it in GitHub Desktop.
/**
* This method extracts the country information from the parsed lines. We first check if COUNTRY is present in the
* provided map. If not, we check if LOCALE is present.
* <br><br>
* In the case of LOCALE we process the string and try to extract the country code from it.
*
* @param values The values parsed from the list of lines.
*
* @return The Optional country object, which is present of if the values map contains COUNTRY or LOCALE key.
*/
private static Optional<Country> parseCountry(final Map<Attribute, List<String>> values) {
// Our default matcher is to not select anything.
Predicate<Country> matcher = c -> Boolean.FALSE;
if (values.containsKey(Attribute.COUNTRY)) {
// We use the country key as matcher
matcher = c -> c.getKey().equalsIgnoreCase(values.get(Attribute.COUNTRY).get(0));
} else if (values.containsKey(Attribute.LOCALE)) {
// We process the locale key and then match it against the country key.
final String[] value = values.get(Attribute.LOCALE).get(0).split("_|-");
matcher = c -> value.length == 2 && c.getKey().equalsIgnoreCase(value[1]);
}
return Arrays.stream(Country.values())
.filter(matcher)
.findAny();
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment