fluo-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From ktur...@apache.org
Subject [fluo] branch master updated: fixes #894 merge SimpleConfiguration Objects (#902)
Date Mon, 14 Aug 2017 14:59:28 GMT
This is an automated email from the ASF dual-hosted git repository.

kturner pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/fluo.git


The following commit(s) were added to refs/heads/master by this push:
     new a349a9d  fixes #894 merge SimpleConfiguration Objects (#902)
a349a9d is described below

commit a349a9d00b5081545d4b052bb90f13d7b497905c
Author: Christopher McTague <cjmctague@apache.org>
AuthorDate: Mon Aug 14 10:59:26 2017 -0400

    fixes #894 merge SimpleConfiguration Objects (#902)
---
 .../fluo/api/config/SimpleConfiguration.java       | 40 +++++++++-
 .../fluo/api/config/SimpleConfigurationTest.java   | 90 ++++++++++++++++++++++
 2 files changed, 129 insertions(+), 1 deletion(-)

diff --git a/modules/api/src/main/java/org/apache/fluo/api/config/SimpleConfiguration.java
b/modules/api/src/main/java/org/apache/fluo/api/config/SimpleConfiguration.java
index f54e3f5..afe8c6f 100644
--- a/modules/api/src/main/java/org/apache/fluo/api/config/SimpleConfiguration.java
+++ b/modules/api/src/main/java/org/apache/fluo/api/config/SimpleConfiguration.java
@@ -27,6 +27,7 @@ import java.io.Serializable;
 import java.util.Iterator;
 import java.util.Map;
 import java.util.Map.Entry;
+import java.util.Objects;
 
 import com.google.common.collect.ImmutableMap;
 import com.google.common.collect.ImmutableMap.Builder;
@@ -213,7 +214,6 @@ public class SimpleConfiguration implements Serializable {
     }
   }
 
-
   public void save(OutputStream out) {
     PropertiesConfiguration pconf = new PropertiesConfiguration();
     pconf.setDelimiterParsingDisabled(true);
@@ -250,6 +250,44 @@ public class SimpleConfiguration implements Serializable {
     return new SimpleConfiguration(internalConfig.subset(prefix));
   }
 
+  /**
+   * @param fallback SimpleConfiguration to join together
+   * @return a new simple configuration that contains all of the current properties from
this plus
+   *         the properties from fallback that are not present in this.
+   * 
+   * @since 1.2.0
+   */
+  public SimpleConfiguration orElse(SimpleConfiguration fallback) {
+    SimpleConfiguration copy = new SimpleConfiguration(this);
+    for (Map.Entry<String, String> entry : fallback.toMap().entrySet()) {
+      if (!copy.containsKey(entry.getKey())) {
+        copy.setProperty(entry.getKey(), entry.getValue());
+      }
+    }
+    return copy;
+  }
+
+  @Override
+  public int hashCode() {
+    return Objects.hashCode(this.toMap().entrySet());
+  }
+
+  @Override
+  public boolean equals(Object o) {
+    if (o == this) {
+      return true;
+    }
+
+    if (o instanceof SimpleConfiguration) {
+      Map<String, String> th = this.toMap();
+      Map<String, String> sc = ((SimpleConfiguration) o).toMap();
+      if (th.size() == sc.size()) {
+        return th.entrySet().equals(sc.entrySet());
+      }
+    }
+    return false;
+  }
+
   @Override
   public String toString() {
     return ConfigurationUtils.toString(internalConfig);
diff --git a/modules/api/src/test/java/org/apache/fluo/api/config/SimpleConfigurationTest.java
b/modules/api/src/test/java/org/apache/fluo/api/config/SimpleConfigurationTest.java
new file mode 100644
index 0000000..e905a5f
--- /dev/null
+++ b/modules/api/src/test/java/org/apache/fluo/api/config/SimpleConfigurationTest.java
@@ -0,0 +1,90 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more contributor license
+ * agreements. See the NOTICE file distributed with this work for additional information
regarding
+ * copyright ownership. The ASF licenses this file to you under the Apache License, Version
2.0 (the
+ * "License"); you may not use this file except in compliance with the License. You may obtain
a
+ * copy of the License at
+ * 
+ * http://www.apache.org/licenses/LICENSE-2.0
+ * 
+ * Unless required by applicable law or agreed to in writing, software distributed under
the License
+ * is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either
express
+ * or implied. See the License for the specific language governing permissions and limitations
under
+ * the License.
+ */
+package org.apache.fluo.api.config;
+
+import org.junit.Assert;
+import org.junit.Test;
+
+/**
+ * @since 1.2.0
+ */
+public class SimpleConfigurationTest {
+
+  @Test
+  public void testMerge() {
+    SimpleConfiguration empty = new SimpleConfiguration();
+    SimpleConfiguration sc1 = new SimpleConfiguration();
+    SimpleConfiguration sc2 = new SimpleConfiguration();
+    SimpleConfiguration sc3 = new SimpleConfiguration();
+    SimpleConfiguration testEmpty = sc1.orElse(sc2).orElse(sc3);
+
+    Assert.assertEquals(empty, testEmpty);
+
+    sc1.setProperty("set1", "value1");
+    sc1.setProperty("set1", "value2");
+    sc1.setProperty("set3", "value3");
+    sc2.setProperty("set4", "value4");
+    sc2.setProperty("set5", "value5");
+    sc2.setProperty("set1", "value6"); // wont include as sc1 already has set1
+    sc3.setProperty("set7", "value7");
+    sc3.setProperty("set5", "value8"); // wont include as sc2 already has set5
+
+    SimpleConfiguration msc = sc1.orElse(sc2).orElse(sc3);
+
+    SimpleConfiguration expected = new SimpleConfiguration();
+    expected.setProperty("set1", "value2");
+    expected.setProperty("set3", "value3");
+    expected.setProperty("set4", "value4");
+    expected.setProperty("set5", "value5");
+    expected.setProperty("set7", "value7");
+
+    SimpleConfiguration expNoOrder = new SimpleConfiguration();
+    expNoOrder.setProperty("set7", "value7");
+    expNoOrder.setProperty("set4", "value4");
+    expNoOrder.setProperty("set5", "value5");
+    expNoOrder.setProperty("set1", "value2");
+    expNoOrder.setProperty("set3", "value3");
+
+    SimpleConfiguration diff = new SimpleConfiguration();
+    diff.setProperty("set7", "value7");
+    diff.setProperty("set4", "value11");
+    diff.setProperty("set5", "value12");
+    diff.setProperty("set1", "value13");
+    diff.setProperty("set3", "value14");
+
+    SimpleConfiguration sc4 = sc3.orElse(sc1).orElse(sc2);
+    SimpleConfiguration noChange = new SimpleConfiguration(sc4);
+    sc3.setProperty("set25", "value25");
+    sc1.setProperty("set26", "value26");
+    sc2.setProperty("set27", "value27");
+
+    Assert.assertEquals(noChange, sc4);
+
+    Assert.assertEquals(expected, msc);
+    Assert.assertEquals(expNoOrder, msc);
+    Assert.assertNotEquals(diff, msc);
+
+    Assert.assertNotEquals(expected, sc1);
+    Assert.assertNotEquals(expNoOrder, sc1);
+
+    Assert.assertNotEquals(expected, sc2);
+    Assert.assertNotEquals(expected, sc3);
+
+    Assert.assertNotEquals(expNoOrder, sc2);
+    Assert.assertNotEquals(expNoOrder, sc3);
+
+    Assert.assertNotEquals(expNoOrder.toString(), msc.toString());
+  }
+}

-- 
To stop receiving notification emails like this one, please contact
['"commits@fluo.apache.org" <commits@fluo.apache.org>'].

Mime
View raw message