diff --git a/src/de/steamwar/bungeecore/Message.java b/src/de/steamwar/bungeecore/Message.java index 4356bcff..feca756d 100644 --- a/src/de/steamwar/bungeecore/Message.java +++ b/src/de/steamwar/bungeecore/Message.java @@ -33,6 +33,7 @@ import java.io.InputStream; import java.text.DateFormat; import java.text.MessageFormat; import java.util.*; +import java.util.concurrent.atomic.AtomicReference; public class Message { @@ -59,18 +60,15 @@ public class Message { } private static SteamwarResourceBundle getOrCreateSingle(String name, String cacheKey) { - if (bundles.containsKey(cacheKey)) { - return bundles.get(cacheKey); - } - InputStream inputStream = Message.class.getResourceAsStream(name); - if(inputStream == null) return null; - try { - SteamwarResourceBundle steamwarResourceBundle = new SteamwarResourceBundle(inputStream); - bundles.put(cacheKey, steamwarResourceBundle); - return steamwarResourceBundle; - } catch (IOException e) { - return null; - } + return bundles.computeIfAbsent(cacheKey, s -> { + InputStream inputStream = Message.class.getResourceAsStream(name); + if(inputStream == null) return null; + try { + return new SteamwarResourceBundle(inputStream); + } catch (IOException e) { + return null; + } + }); } private static ResourceBundle getResourceBundle(String baseName, Locale locale) { @@ -79,33 +77,23 @@ public class Message { } String path = baseName.replace('.', '/'); - SteamwarResourceBundle current = null; - SteamwarResourceBundle temp = getOrCreateSingle("/" + path + "_" + locale + ".properties", locale.toString()); - if (temp != null) { - current = temp; - bundles.put(locale.toString(), temp); - } + AtomicReference current = new AtomicReference<>(); + apply(current, getOrCreateSingle("/" + path + "_" + locale.toString() + ".properties", locale.toString()), locale.toString()); + apply(current, getOrCreateSingle("/" + path + "_" + locale.getLanguage() + ".properties", locale.getLanguage()), locale.toString()); + apply(current, getOrCreateSingle("/" + path + ".properties", ""), locale.toString()); - temp = getOrCreateSingle("/" + path + "_" + locale.getLanguage() + ".properties", locale.getLanguage()); - if (current != null) current.setParent(temp); - if (temp != null) { - current = temp; - bundles.put(locale.toString(), temp); - } - - temp = getOrCreateSingle("/" + path + ".properties", ""); - if (current != null) current.setParent(temp); - if (temp != null) { - current = temp; - bundles.put(locale.toString(), temp); - } - - if (current == null) { - throw new SecurityException("Could not find resource bundle for " + path); - } + if (current.get() == null) throw new SecurityException("Could not find resource bundle for " + path); return bundles.get(locale.toString()); } + private static void apply(AtomicReference current, SteamwarResourceBundle now, String bundleKey) { + if (current.get() != null) current.get().setParent(now); + if (now != null) { + current.set(now); + bundles.put(bundleKey, now); + } + } + private static String parse(String message, boolean prefixed, Locale locale, Object... params){ if(locale == null) locale = Locale.getDefault();