Skip to content

Commit a349a9d

Browse files
cjmctaguekeith-turner
authored andcommitted
fixes #894 merge SimpleConfiguration Objects (#902)
1 parent db0bdad commit a349a9d

File tree

2 files changed

+129
-1
lines changed

2 files changed

+129
-1
lines changed

Diff for: modules/api/src/main/java/org/apache/fluo/api/config/SimpleConfiguration.java

+39-1
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@
2727
import java.util.Iterator;
2828
import java.util.Map;
2929
import java.util.Map.Entry;
30+
import java.util.Objects;
3031

3132
import com.google.common.collect.ImmutableMap;
3233
import com.google.common.collect.ImmutableMap.Builder;
@@ -213,7 +214,6 @@ public void save(File file) {
213214
}
214215
}
215216

216-
217217
public void save(OutputStream out) {
218218
PropertiesConfiguration pconf = new PropertiesConfiguration();
219219
pconf.setDelimiterParsingDisabled(true);
@@ -250,6 +250,44 @@ public SimpleConfiguration subset(String prefix) {
250250
return new SimpleConfiguration(internalConfig.subset(prefix));
251251
}
252252

253+
/**
254+
* @param fallback SimpleConfiguration to join together
255+
* @return a new simple configuration that contains all of the current properties from this plus
256+
* the properties from fallback that are not present in this.
257+
*
258+
* @since 1.2.0
259+
*/
260+
public SimpleConfiguration orElse(SimpleConfiguration fallback) {
261+
SimpleConfiguration copy = new SimpleConfiguration(this);
262+
for (Map.Entry<String, String> entry : fallback.toMap().entrySet()) {
263+
if (!copy.containsKey(entry.getKey())) {
264+
copy.setProperty(entry.getKey(), entry.getValue());
265+
}
266+
}
267+
return copy;
268+
}
269+
270+
@Override
271+
public int hashCode() {
272+
return Objects.hashCode(this.toMap().entrySet());
273+
}
274+
275+
@Override
276+
public boolean equals(Object o) {
277+
if (o == this) {
278+
return true;
279+
}
280+
281+
if (o instanceof SimpleConfiguration) {
282+
Map<String, String> th = this.toMap();
283+
Map<String, String> sc = ((SimpleConfiguration) o).toMap();
284+
if (th.size() == sc.size()) {
285+
return th.entrySet().equals(sc.entrySet());
286+
}
287+
}
288+
return false;
289+
}
290+
253291
@Override
254292
public String toString() {
255293
return ConfigurationUtils.toString(internalConfig);
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,90 @@
1+
/*
2+
* Licensed to the Apache Software Foundation (ASF) under one or more contributor license
3+
* agreements. See the NOTICE file distributed with this work for additional information regarding
4+
* copyright ownership. The ASF licenses this file to you under the Apache License, Version 2.0 (the
5+
* "License"); you may not use this file except in compliance with the License. You may obtain a
6+
* copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software distributed under the License
11+
* is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express
12+
* or implied. See the License for the specific language governing permissions and limitations under
13+
* the License.
14+
*/
15+
package org.apache.fluo.api.config;
16+
17+
import org.junit.Assert;
18+
import org.junit.Test;
19+
20+
/**
21+
* @since 1.2.0
22+
*/
23+
public class SimpleConfigurationTest {
24+
25+
@Test
26+
public void testMerge() {
27+
SimpleConfiguration empty = new SimpleConfiguration();
28+
SimpleConfiguration sc1 = new SimpleConfiguration();
29+
SimpleConfiguration sc2 = new SimpleConfiguration();
30+
SimpleConfiguration sc3 = new SimpleConfiguration();
31+
SimpleConfiguration testEmpty = sc1.orElse(sc2).orElse(sc3);
32+
33+
Assert.assertEquals(empty, testEmpty);
34+
35+
sc1.setProperty("set1", "value1");
36+
sc1.setProperty("set1", "value2");
37+
sc1.setProperty("set3", "value3");
38+
sc2.setProperty("set4", "value4");
39+
sc2.setProperty("set5", "value5");
40+
sc2.setProperty("set1", "value6"); // wont include as sc1 already has set1
41+
sc3.setProperty("set7", "value7");
42+
sc3.setProperty("set5", "value8"); // wont include as sc2 already has set5
43+
44+
SimpleConfiguration msc = sc1.orElse(sc2).orElse(sc3);
45+
46+
SimpleConfiguration expected = new SimpleConfiguration();
47+
expected.setProperty("set1", "value2");
48+
expected.setProperty("set3", "value3");
49+
expected.setProperty("set4", "value4");
50+
expected.setProperty("set5", "value5");
51+
expected.setProperty("set7", "value7");
52+
53+
SimpleConfiguration expNoOrder = new SimpleConfiguration();
54+
expNoOrder.setProperty("set7", "value7");
55+
expNoOrder.setProperty("set4", "value4");
56+
expNoOrder.setProperty("set5", "value5");
57+
expNoOrder.setProperty("set1", "value2");
58+
expNoOrder.setProperty("set3", "value3");
59+
60+
SimpleConfiguration diff = new SimpleConfiguration();
61+
diff.setProperty("set7", "value7");
62+
diff.setProperty("set4", "value11");
63+
diff.setProperty("set5", "value12");
64+
diff.setProperty("set1", "value13");
65+
diff.setProperty("set3", "value14");
66+
67+
SimpleConfiguration sc4 = sc3.orElse(sc1).orElse(sc2);
68+
SimpleConfiguration noChange = new SimpleConfiguration(sc4);
69+
sc3.setProperty("set25", "value25");
70+
sc1.setProperty("set26", "value26");
71+
sc2.setProperty("set27", "value27");
72+
73+
Assert.assertEquals(noChange, sc4);
74+
75+
Assert.assertEquals(expected, msc);
76+
Assert.assertEquals(expNoOrder, msc);
77+
Assert.assertNotEquals(diff, msc);
78+
79+
Assert.assertNotEquals(expected, sc1);
80+
Assert.assertNotEquals(expNoOrder, sc1);
81+
82+
Assert.assertNotEquals(expected, sc2);
83+
Assert.assertNotEquals(expected, sc3);
84+
85+
Assert.assertNotEquals(expNoOrder, sc2);
86+
Assert.assertNotEquals(expNoOrder, sc3);
87+
88+
Assert.assertNotEquals(expNoOrder.toString(), msc.toString());
89+
}
90+
}

0 commit comments

Comments
 (0)