From be9d096986fb6e71e4b2553280efd048a767b290 Mon Sep 17 00:00:00 2001 From: Robin Rosenberg Date: Fri, 10 Sep 2010 00:11:53 +0200 Subject: [PATCH] Use only a single instance for NLS translation bundles As findbugs pointed out, there was a small risk for creating multiple instances of translation bundles. If that happens, drop the second instance. Change-Id: I3aacda86251d511f6bbc2ed7481d561449ce3b6c Signed-off-by: Robin Rosenberg --- org.eclipse.jgit/src/org/eclipse/jgit/nls/NLS.java | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/org.eclipse.jgit/src/org/eclipse/jgit/nls/NLS.java b/org.eclipse.jgit/src/org/eclipse/jgit/nls/NLS.java index 6f81af845..219b0a163 100644 --- a/org.eclipse.jgit/src/org/eclipse/jgit/nls/NLS.java +++ b/org.eclipse.jgit/src/org/eclipse/jgit/nls/NLS.java @@ -125,11 +125,16 @@ private NLS(Locale locale) { this.locale = locale; } + @SuppressWarnings("unchecked") private T get(Class type) { TranslationBundle bundle = map.get(type); if (bundle == null) { bundle = GlobalBundleCache.lookupBundle(locale, type); - map.putIfAbsent(type, bundle); + // There is a small opportunity for a race, which we may + // lose. Accept defeat and return the winner's instance. + TranslationBundle old = map.putIfAbsent(type, bundle); + if (old != null) + bundle = old; } return (T) bundle; }