/*
 * This file is part of Sponge, licensed under the MIT License (MIT).
 *
 * Copyright (c) SpongePowered <https://www.spongepowered.org>
 * Copyright (c) contributors
 *
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to deal
 * in the Software without restriction, including without limitation the rights
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included in
 * all copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
 * THE SOFTWARE.
 */
package org.spongepowered.common.util;

import com.google.common.cache.CacheBuilder;
import com.google.common.cache.CacheLoader;
import com.google.common.cache.LoadingCache;
import org.apache.commons.lang3.LocaleUtils;
import org.spongepowered.api.util.annotation.NonnullByDefault;
import org.spongepowered.common.SpongeImpl;

import java.util.Locale;

@NonnullByDefault
public final class LocaleCache {

    private static final LoadingCache<String, Locale> LOCALE_CACHE = CacheBuilder.newBuilder()
        .build(new CacheLoader<String, Locale>() {
            @Override
            public Locale load(final String key) throws Exception {
                try {
                    return LocaleUtils.toLocale(key);
                } catch (final IllegalArgumentException ignored) {
                    // Ignore the first exception and try again, this time using a "fixed" key.
                    final String fixedKey = this.fixLocaleKey(key);
                    try {
                        return LocaleUtils.toLocale(fixedKey);
                    } catch (final IllegalArgumentException e) {
                        SpongeImpl.getLogger().error("Could not transform '{}' or '{}' into a Locale", key, fixedKey);
                        throw e;
                    }
                }
            }

            private String fixLocaleKey(final String key) {
                final String[] parts = key.split("_");
                if (parts.length == 2) {
                    return parts[0] + '_' + parts[1].toUpperCase();
                } else {
                    return key;
                }
            }
        });

    private LocaleCache() {
    }

    /**
     * Gets a locale from the cache.
     *
     * @param tag The locale tag
     * @return The locale
     */
    public static Locale getLocale(final String tag) {
        return LOCALE_CACHE.getUnchecked(tag);
    }

    /**
     * Preload a locale into the cache.
     *
     * @param locale The locale to preload
     */
    public static void preload(final Locale locale) {
        final String tag = locale.toString();
        LOCALE_CACHE.put(tag, locale);
        LOCALE_CACHE.put(tag.toLowerCase(), locale);
    }

}

@@ -1311,114 +1311,19 @@
 on.c
-ache.CacheBuilder;%0Aimport com.google.common.cache.CacheLoader;%0Aimport com.google.common.cache.LoadingCache
+ollect.Maps
 ;%0Aim
@@ -1497,16 +1497,74 @@
 .Locale;
+%0Aimport java.util.Map;%0Aimport java.util.function.Function;
 %0A%0A@Nonnu
@@ -1638,20 +1638,11 @@
 nal 
-LoadingCache
+Map
 %3CStr
@@ -1673,64 +1673,107 @@
 E = 
-CacheBuilder.newBuilder
+Maps.newHashMap
 ()
+;
 %0A    
-    .build(new CacheLoader
+private static final Function%3CString, Locale%3E LOCALE_FUNCTION = new Function
 %3CStr
@@ -1785,28 +1785,24 @@
 Locale%3E() %7B%0A
-    
         @Ove
@@ -1815,20 +1815,16 @@
         
-    
 public L
@@ -1829,27 +1829,22 @@
  Locale 
-load(final 
+apply(
 String k
@@ -1851,31 +1851,10 @@
 ey) 
-throws Exception %7B%0A    
+%7B%0A
     
@@ -1875,36 +1875,32 @@
                 
-    
 return LocaleUti
@@ -1913,28 +1913,24 @@
 ocale(key);%0A
-    
             
@@ -1996,20 +1996,16 @@
         
- 
-   
 // Ignor
@@ -2080,36 +2080,32 @@
                 
-    
 final String fix
@@ -2148,38 +2148,30 @@
             
-   
- 
 try %7B%0A
-    
             
@@ -2209,36 +2209,32 @@
 cale(fixedKey);%0A
-    
                 
@@ -2298,20 +2298,16 @@
         
-    
 SpongeIm
@@ -2413,20 +2413,16 @@
         
-    
 throw e;
@@ -2422,20 +2422,16 @@
 hrow e;%0A
-    
         
@@ -2452,33 +2452,26 @@
         
-    %7D%0A   
+%7D%0A
         
  %7D%0A%0A    
@@ -2462,24 +2462,19 @@
         
- 
 %7D%0A%0A
-    
         
@@ -2525,36 +2525,32 @@
 ) %7B%0A            
-    
 final String%5B%5D p
@@ -2584,20 +2584,16 @@
         
- 
-   
 if (part
@@ -2605,28 +2605,24 @@
 gth == 2) %7B%0A
-    
             
@@ -2685,20 +2685,16 @@
         
-  
-  
 %7D else %7B
@@ -2694,20 +2694,16 @@
  else %7B%0A
-    
         
@@ -2722,20 +2722,16 @@
 rn key;%0A
-    
         
@@ -2748,24 +2748,15 @@
     
-    
 %7D%0A    
- 
-   %7D)
+%7D
 ;%0A%0A 
@@ -2995,24 +2995,44 @@
 CHE.
-getUnchecked(tag
+computeIfAbsent(tag, LOCALE_FUNCTION
 );%0A 

Replace LoadingCache with a normal HashMap