Skip to content

Instantly share code, notes, and snippets.

@RChehowski
Created September 2, 2019 16:07
Show Gist options
  • Save RChehowski/c699839bb12aa2f2a8cecdcaabe6ff44 to your computer and use it in GitHub Desktop.
Save RChehowski/c699839bb12aa2f2a8cecdcaabe6ff44 to your computer and use it in GitHub Desktop.
**
* Enables locale-dependent URLs to be loaded from configuration files.
*/
public class InternationalUrl
{
private static Logger log = LogManager.getLogger(InternationalUrl.class);
/**
* Could be either:
* - {@link java.net.URL} - non-localizable URL.
* - {@link java.util.Map<String,URL>} - localizable URL, keys are locales, values are URL.
*/
private final Object urls;
private InternationalUrl(final URL url)
{
this.urls = url;
}
private InternationalUrl(final Map<String, URL> urls)
{
this.urls = urls;
}
/**
* Retrieves an optional value from the localizable URL.
*
* @return An optional URL.
*/
public final Optional<URL> getOptional()
{
if (urls instanceof URL)
return getNotLocalized();
else if (urls instanceof Map<?, ?>)
return getLocalized();
throw new RuntimeException("Unsupported urls type: \"" + urls + "\"");
}
/**
* Value from the map, return using Locale.getDefault().
*
* @return Localized URL.
*/
private Optional<URL> getLocalized()
{
@SuppressWarnings("unchecked")
final Map<String, URL> map = (Map<String, URL>) urls;
// Find by installed locale
final Locale defaultLocale = Locale.getDefault();
if (defaultLocale == null)
{
log.error("Default locale is not present. Locale.getDefault() returned null.");
return Optional.empty();
}
// Get URL by key
final URL url = map.get(defaultLocale.toString());
if (url == null)
{
log.error("Unable to retrieve value {} from the map", defaultLocale.toString());
return Optional.empty();
}
return Optional.of(url);
}
/**
* Single value (not localized), return it immediately
*
* @return Not localized URL.
*/
private Optional<URL> getNotLocalized()
{
return Optional.ofNullable((URL) urls);
}
/**
* Retrieves the serializer for the complex localizable URL.
*
* @return The deserializer.
*/
public static JsonDeserializer<InternationalUrl> getJsonDeserializer()
{
return new JsonDeserializer<>() {
@Override
public InternationalUrl deserialize(final JsonElement e, final Type t, final JsonDeserializationContext c)
throws JsonParseException
{
if (e instanceof JsonPrimitive)
return deserializeNotLocalized((JsonPrimitive) e);
else if (e instanceof JsonObject)
return deserializeLocalized((JsonObject) e);
throw new RuntimeException("Unknown element class: " + e.getClass());
}
private InternationalUrl deserializeLocalized(final JsonObject e)
{
final Map<String, URL> urls = new LinkedHashMap<>();
for (final Map.Entry<String, JsonElement> entry : e.entrySet())
{
final JsonElement value = entry.getValue();
urls.put(entry.getKey(), stringToUrl(value.getAsString()));
}
return new InternationalUrl(urls);
}
private InternationalUrl deserializeNotLocalized(final JsonPrimitive e)
{
return new InternationalUrl(stringToUrl(e.getAsString()));
}
private URL stringToUrl(final String string)
{
try {
return new URL(string);
}
catch (MalformedURLException e) {
throw new UnsupportedOperationException("Unable to convert \"" + string + "\" into URL");
}
}
};
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment