Skip to content

Instantly share code, notes, and snippets.

@RChehowski
Created September 2, 2019 16:07
Show Gist options
  • Select an option

  • Save RChehowski/c699839bb12aa2f2a8cecdcaabe6ff44 to your computer and use it in GitHub Desktop.

Select an option

Save RChehowski/c699839bb12aa2f2a8cecdcaabe6ff44 to your computer and use it in GitHub Desktop.

Revisions

  1. RChehowski created this gist Sep 2, 2019.
    130 changes: 130 additions & 0 deletions InternationalUrl.java
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,130 @@
    **
    * 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");
    }
    }
    };
    }
    }