From 311b3338145038ea92c693bb2a8babec9580deda Mon Sep 17 00:00:00 2001 From: Qimiao Chen Date: Tue, 12 May 2020 20:55:58 +0800 Subject: [PATCH] Use computeIfAbsent in ResourceBundleMessageSource This commit optimizes code in ResourceBundleMessageSource by using computeIfAbsent instead of putIfAbsent. In addition, the content of some Javadoc has been adjusted. Closes gh-25054 --- .../context/support/ResourceBundleMessageSource.java | 12 ++++-------- 1 file changed, 4 insertions(+), 8 deletions(-) diff --git a/spring-context/src/main/java/org/springframework/context/support/ResourceBundleMessageSource.java b/spring-context/src/main/java/org/springframework/context/support/ResourceBundleMessageSource.java index 6df9cdbf99..77519a4822 100644 --- a/spring-context/src/main/java/org/springframework/context/support/ResourceBundleMessageSource.java +++ b/spring-context/src/main/java/org/springframework/context/support/ResourceBundleMessageSource.java @@ -1,5 +1,5 @@ /* - * Copyright 2002-2019 the original author or authors. + * Copyright 2002-2020 the original author or authors. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -183,8 +183,8 @@ public class ResourceBundleMessageSource extends AbstractResourceBasedMessageSou /** - * Return a ResourceBundle for the given basename and code, - * fetching already generated MessageFormats from the cache. + * Return a ResourceBundle for the given basename and Locale, + * fetching already generated ResourceBundle from the cache. * @param basename the basename of the ResourceBundle * @param locale the Locale to find the ResourceBundle for * @return the resulting ResourceBundle, or {@code null} if none @@ -209,11 +209,7 @@ public class ResourceBundleMessageSource extends AbstractResourceBasedMessageSou try { ResourceBundle bundle = doGetBundle(basename, locale); if (localeMap == null) { - localeMap = new ConcurrentHashMap<>(); - Map existing = this.cachedResourceBundles.putIfAbsent(basename, localeMap); - if (existing != null) { - localeMap = existing; - } + localeMap = this.cachedResourceBundles.computeIfAbsent(basename, bn -> new ConcurrentHashMap<>()); } localeMap.put(locale, bundle); return bundle;