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
This commit is contained in:
Qimiao Chen 2020-05-12 20:55:58 +08:00 committed by GitHub
parent d54b903d28
commit 311b333814
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
1 changed files with 4 additions and 8 deletions

View File

@ -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<Locale, ResourceBundle> 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;