Configuration#get return value optimization (jeagles)

(cherry picked from commit e41aac0cd4)
This commit is contained in:
Jonathan Eagles 2017-03-27 12:52:59 -05:00
parent 4e40e9d4db
commit ec5a65a0ca
1 changed files with 29 additions and 25 deletions

View File

@ -610,6 +610,7 @@ public class Configuration implements Iterable<Map.Entry<String,String>>,
* deprecated key, the value of the deprecated key is set as the value for * deprecated key, the value of the deprecated key is set as the value for
* the provided property name. * the provided property name.
* *
* @param deprecations deprecation context
* @param name the property name * @param name the property name
* @return the first property in the list of properties mapping * @return the first property in the list of properties mapping
* the <code>name</code> or the <code>name</code> itself. * the <code>name</code> or the <code>name</code> itself.
@ -619,28 +620,34 @@ public class Configuration implements Iterable<Map.Entry<String,String>>,
if (null != name) { if (null != name) {
name = name.trim(); name = name.trim();
} }
ArrayList<String > names = new ArrayList<String>(); // Initialize the return value with requested name
if (isDeprecated(name)) { String[] names = new String[]{name};
// Deprecated keys are logged once and an updated names are returned
DeprecatedKeyInfo keyInfo = deprecations.getDeprecatedKeyMap().get(name); DeprecatedKeyInfo keyInfo = deprecations.getDeprecatedKeyMap().get(name);
warnOnceIfDeprecated(deprecations, name); if (keyInfo != null) {
for (String newKey : keyInfo.newKeys) { if (!keyInfo.getAndSetAccessed()) {
if(newKey != null) { logDeprecation(keyInfo.getWarningMessage(name));
names.add(newKey);
} }
// Override return value for deprecated keys
names = keyInfo.newKeys;
} }
// If there are no overlay values we can return early
Properties overlay = getOverlay();
if (overlay.isEmpty()) {
return names;
} }
if(names.size() == 0) { // Update properties and overlays with reverse lookup values
names.add(name);
}
for (String n : names) { for (String n : names) {
String deprecatedKey = deprecations.getReverseDeprecatedKeyMap().get(n); String deprecatedKey = deprecations.getReverseDeprecatedKeyMap().get(n);
if (deprecatedKey != null && !getOverlay().containsKey(n) && if (deprecatedKey != null && !overlay.containsKey(n)) {
getOverlay().containsKey(deprecatedKey)) { String deprecatedValue = overlay.getProperty(deprecatedKey);
getProps().setProperty(n, getOverlay().getProperty(deprecatedKey)); if (deprecatedValue != null) {
getOverlay().setProperty(n, getOverlay().getProperty(deprecatedKey)); getProps().setProperty(n, deprecatedValue);
overlay.setProperty(n, deprecatedValue);
} }
} }
return names.toArray(new String[names.size()]); }
return names;
} }
private void handleDeprecation() { private void handleDeprecation() {
@ -1175,11 +1182,8 @@ public class Configuration implements Iterable<Map.Entry<String,String>>,
} }
} }
private void warnOnceIfDeprecated(DeprecationContext deprecations, String name) { private void logDeprecation(String message) {
DeprecatedKeyInfo keyInfo = deprecations.getDeprecatedKeyMap().get(name); LOG_DEPRECATION.info(message);
if (keyInfo != null && !keyInfo.getAndSetAccessed()) {
LOG_DEPRECATION.info(keyInfo.getWarningMessage(name));
}
} }
/** /**