brooklyn-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From aleds...@apache.org
Subject [1/2] brooklyn-server git commit: Remove a bit of code duplication.
Date Fri, 14 Jul 2017 12:55:45 GMT
Repository: brooklyn-server
Updated Branches:
  refs/heads/master 0e35b6151 -> ee5514951


Remove a bit of code duplication.

Small follow-up to https://github.com/apache/brooklyn-server/pull/734.


Project: http://git-wip-us.apache.org/repos/asf/brooklyn-server/repo
Commit: http://git-wip-us.apache.org/repos/asf/brooklyn-server/commit/6f2c08a5
Tree: http://git-wip-us.apache.org/repos/asf/brooklyn-server/tree/6f2c08a5
Diff: http://git-wip-us.apache.org/repos/asf/brooklyn-server/diff/6f2c08a5

Branch: refs/heads/master
Commit: 6f2c08a50c6d6c22f0f4550f7a17f6accfeaf6aa
Parents: 0e35b61
Author: Geoff Macartney <geoff.macartney@cloudsoftcorp.com>
Authored: Thu Jul 13 11:19:56 2017 +0100
Committer: Geoff Macartney <geoff.macartney@cloudsoftcorp.com>
Committed: Thu Jul 13 11:19:56 2017 +0100

----------------------------------------------------------------------
 .../brooklyn/util/core/config/ConfigBag.java    | 71 ++++++--------------
 1 file changed, 22 insertions(+), 49 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/brooklyn-server/blob/6f2c08a5/core/src/main/java/org/apache/brooklyn/util/core/config/ConfigBag.java
----------------------------------------------------------------------
diff --git a/core/src/main/java/org/apache/brooklyn/util/core/config/ConfigBag.java b/core/src/main/java/org/apache/brooklyn/util/core/config/ConfigBag.java
index 9f9bf09..1075cf1 100644
--- a/core/src/main/java/org/apache/brooklyn/util/core/config/ConfigBag.java
+++ b/core/src/main/java/org/apache/brooklyn/util/core/config/ConfigBag.java
@@ -23,6 +23,7 @@ import static com.google.common.base.Preconditions.checkNotNull;
 import java.util.ConcurrentModificationException;
 import java.util.LinkedHashMap;
 import java.util.Map;
+import java.util.function.Function;
 
 import javax.annotation.Nonnull;
 
@@ -527,90 +528,62 @@ public class ConfigBag {
         return getStringKeyMaybe(key, markUsed).orNull();
     }
 
-    private synchronized Maybe<Object> getRawKeyMaybe(ConfigKey<?> key, boolean
markUsed) {
-        Maybe<Object> val = getRawStringKeyMaybe(key.getName(), markUsed);
-        
+
+    private synchronized Maybe<Object> getKeyMaybeInternal(ConfigKey<?> key,
Function<String, Maybe<Object>> getKey) {
+        Maybe<Object> val = getKey.apply(key.getName());
+
         String firstDeprecatedName = null;
         Maybe<Object> firstDeprecatedVal = null;
         for (String deprecatedName : key.getDeprecatedNames()) {
-            Maybe<Object> deprecatedVal = getRawStringKeyMaybe(deprecatedName, markUsed);
+            Maybe<Object> deprecatedVal = getKey.apply(deprecatedName);
             if (deprecatedVal.isPresent()) {
                 if (val.isPresent()) {
                     if (!Objects.equal(val.get(), deprecatedVal.get())) {
                         log.warn("Conflicting value for key "+key+" from deprecated name
'"+deprecatedName+"'; "
-                                + "using value from preferred name "+key.getName());
+                            + "using value from preferred name "+key.getName());
                     } else {
                         log.warn("Duplicate value for key "+key+" from deprecated name '"+deprecatedName+"';
"
-                                + "using same value from preferred name "+key.getName());
+                            + "using same value from preferred name "+key.getName());
                     }
                 } else if (firstDeprecatedVal != null && firstDeprecatedVal.isPresent())
{
                     if (!Objects.equal(firstDeprecatedVal.get(), deprecatedVal.get())) {
                         log.warn("Conflicting value for key "+key+" from deprecated name
'"+deprecatedName+"'; "
-                                + "using earlier deprecated name "+firstDeprecatedName);
+                            + "using earlier deprecated name "+firstDeprecatedName);
                     } else {
                         log.warn("Duplicate value for key "+key+" from deprecated name '"+deprecatedName+"';
"
-                                + "using same value from earlier depreated name "+key.getName());
+                            + "using same value from earlier depreated name "+key.getName());
                     }
                 } else {
                     // new value, from deprecated name
                     log.warn("Value for key "+key+" found with deprecated name '"+deprecatedName+"';
"
-                            + "recommend changing to preferred name '"+key.getName()+"';
this will not be supported in future versions");
+                        + "recommend changing to preferred name '"+key.getName()+"'; this
will not be supported in future versions");
                     firstDeprecatedName = deprecatedName;
                     firstDeprecatedVal = deprecatedVal;
                 }
             }
         }
-        
 
         return val.isPresent() ? val : (firstDeprecatedVal != null && firstDeprecatedVal.isPresent()
? firstDeprecatedVal : val);
     }
 
-    private synchronized Maybe<Object> getRawStringKeyMaybe(String key, boolean markUsed)
{
-        if (config.containsKey(key)) {
-            if (markUsed) markUsed(key);
-            return Maybe.of(config.get(key));
-        }
-        return Maybe.absent();
+
+    private synchronized Maybe<Object> getRawKeyMaybe(ConfigKey<?> key, boolean
markUsed) {
+        return getKeyMaybeInternal(key, name -> getRawStringKeyMaybe(name, markUsed));
     }
 
     /**
      * @return Unresolved configuration value. May be overridden to provide resolution -
@see {@link ResolvingConfigBag#getStringKeyMaybe(String, boolean)}
      */
     protected synchronized Maybe<Object> getKeyMaybe(ConfigKey<?> key, boolean
markUsed) {
-        Maybe<Object> val = getStringKeyMaybe(key.getName(), markUsed);
-        
-        String firstDeprecatedName = null;
-        Maybe<Object> firstDeprecatedVal = null;
-        for (String deprecatedName : key.getDeprecatedNames()) {
-            Maybe<Object> deprecatedVal = getStringKeyMaybe(deprecatedName, markUsed);
-            if (deprecatedVal.isPresent()) {
-                if (val.isPresent()) {
-                    if (!Objects.equal(val.get(), deprecatedVal.get())) {
-                        log.warn("Conflicting value for key "+key+" from deprecated name
'"+deprecatedName+"'; "
-                                + "using value from preferred name "+key.getName());
-                    } else {
-                        log.warn("Duplicate value for key "+key+" from deprecated name '"+deprecatedName+"';
"
-                                + "using same value from preferred name "+key.getName());
-                    }
-                } else if (firstDeprecatedVal != null && firstDeprecatedVal.isPresent())
{
-                    if (!Objects.equal(firstDeprecatedVal.get(), deprecatedVal.get())) {
-                        log.warn("Conflicting value for key "+key+" from deprecated name
'"+deprecatedName+"'; "
-                                + "using earlier deprecated name "+firstDeprecatedName);
-                    } else {
-                        log.warn("Duplicate value for key "+key+" from deprecated name '"+deprecatedName+"';
"
-                                + "using same value from earlier depreated name "+key.getName());
-                    }
-                } else {
-                    // new value, from deprecated name
-                    log.warn("Value for key "+key+" found with deprecated name '"+deprecatedName+"';
"
-                            + "recommend changing to preferred name '"+key.getName()+"';
this will not be supported in future versions");
-                    firstDeprecatedName = deprecatedName;
-                    firstDeprecatedVal = deprecatedVal;
-                }
-            }
+        return getKeyMaybeInternal(key, name -> getStringKeyMaybe(name, markUsed));
+    }
+
+    private synchronized Maybe<Object> getRawStringKeyMaybe(String key, boolean markUsed)
{
+        if (config.containsKey(key)) {
+            if (markUsed) markUsed(key);
+            return Maybe.of(config.get(key));
         }
-        
-        return val.isPresent() ? val : (firstDeprecatedVal != null && firstDeprecatedVal.isPresent()
? firstDeprecatedVal : val);
+        return Maybe.absent();
     }
 
     /**


Mime
View raw message