From 8b4e4f9d2da5d80c79e02a2b1b8e78dc1fade86f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jo=CC=88rg=20Prante?= Date: Wed, 10 Aug 2022 08:15:02 +0200 Subject: [PATCH] spotbugs fixes --- .../content/resource/IRINamespaceContext.java | 69 +++++++------------ .../datastructures/DatastructureSettings.java | 5 +- 2 files changed, 29 insertions(+), 45 deletions(-) diff --git a/content-resource/src/main/java/org/xbib/content/resource/IRINamespaceContext.java b/content-resource/src/main/java/org/xbib/content/resource/IRINamespaceContext.java index b033547..464e84d 100644 --- a/content-resource/src/main/java/org/xbib/content/resource/IRINamespaceContext.java +++ b/content-resource/src/main/java/org/xbib/content/resource/IRINamespaceContext.java @@ -7,23 +7,40 @@ import java.util.Iterator; import java.util.List; import java.util.Locale; import java.util.Map; -import java.util.MissingResourceException; import java.util.ResourceBundle; import java.util.Set; import java.util.SortedMap; import java.util.TreeMap; -/** - * - */ public final class IRINamespaceContext implements NamespaceContext { - private static final String DEFAULT_RESOURCE = - IRINamespaceContext.class.getPackage().getName().replace('.', '/') + '/' + "namespace"; + private static final IRINamespaceContext DEFAULT_INSTANCE; - private static IRINamespaceContext instance; + static { + DEFAULT_INSTANCE = new IRINamespaceContext(); + try { + String bundleName = IRINamespaceContext.class.getPackage().getName().replace('.', '/') + '/' + "namespace"; + Locale locale = Locale.getDefault(); + ClassLoader classLoader = Thread.currentThread().getContextClassLoader(); + ResourceBundle resourceBundle = ResourceBundle.getBundle(bundleName, locale, classLoader); + Enumeration en = resourceBundle.getKeys(); + while (en.hasMoreElements()) { + String prefix = en.nextElement(); + String namespace = resourceBundle.getString(prefix); + DEFAULT_INSTANCE.addNamespace(prefix, namespace); + } + } catch (Exception e) { + // + } + } - private static final IRINamespaceContext DEFAULT_CONTEXT = newInstance(DEFAULT_RESOURCE); + public static IRINamespaceContext getInstance() { + return DEFAULT_INSTANCE; + } + + public static IRINamespaceContext newInstance() { + return new IRINamespaceContext(); + } // sort namespace by length in descending order, useful for compacting prefix private final SortedMap namespaces; @@ -34,46 +51,12 @@ public final class IRINamespaceContext implements NamespaceContext { private List sortedNamespacesByPrefixLength; - - public static IRINamespaceContext getInstance() { - return DEFAULT_CONTEXT; - } - - public static IRINamespaceContext newInstance(String bundleName) { - return newInstance(bundleName, Locale.getDefault(), Thread.currentThread().getContextClassLoader()); - } - - public static IRINamespaceContext newInstance(String bundleName, Locale locale, ClassLoader classLoader) { - if (instance == null) { - try { - instance = new IRINamespaceContext(ResourceBundle.getBundle(bundleName, locale, classLoader)); - } catch (MissingResourceException e) { - instance = new IRINamespaceContext(); - } - } - return instance; - } - - public static IRINamespaceContext newInstance() { - return new IRINamespaceContext(); - } - - protected IRINamespaceContext() { + private IRINamespaceContext() { this.namespaces = new TreeMap<>(); this.prefixes = new TreeMap<>(); this.lock = new Object(); } - private IRINamespaceContext(ResourceBundle bundle) { - this(); - Enumeration en = bundle.getKeys(); - while (en.hasMoreElements()) { - String prefix = en.nextElement(); - String namespace = bundle.getString(prefix); - addNamespace(prefix, namespace); - } - } - @Override public void addNamespace(String prefix, String namespace) { if (prefix != null && namespace != null) { diff --git a/settings-datastructures/src/main/java/org/xbib/settings/datastructures/DatastructureSettings.java b/settings-datastructures/src/main/java/org/xbib/settings/datastructures/DatastructureSettings.java index 7648c52..6234521 100644 --- a/settings-datastructures/src/main/java/org/xbib/settings/datastructures/DatastructureSettings.java +++ b/settings-datastructures/src/main/java/org/xbib/settings/datastructures/DatastructureSettings.java @@ -263,8 +263,9 @@ public class DatastructureSettings implements Settings { } } TinyMap.Builder retVal = TinyMap.builder(); - for (String key : hashMap.keySet()) { - TinyMap.Builder value = hashMap.get(key); + for (Map.Entry> entry : hashMap.entrySet()) { + String key = entry.getKey(); + TinyMap.Builder value = entry.getValue(); retVal.put(key, new DatastructureSettings(value.build())); } return retVal.build();