AnyValue增加toProperties方法
This commit is contained in:
@@ -1166,6 +1166,35 @@ public abstract class AnyValue {
|
|||||||
}
|
}
|
||||||
|
|
||||||
public Properties toProperties() {
|
public Properties toProperties() {
|
||||||
throw new UnsupportedOperationException("Not supported yet.");
|
Properties props = new Properties();
|
||||||
|
toProperties(props, new HashMap<>(), "", this);
|
||||||
|
return props;
|
||||||
|
}
|
||||||
|
|
||||||
|
private static void toProperties(Properties props, Map<String, Integer> keyPrefixs, String parent, AnyValue conf) {
|
||||||
|
conf.forEach(
|
||||||
|
(k, v) -> {
|
||||||
|
final String prefix = parent + k;
|
||||||
|
if (keyPrefixs.containsKey(prefix)) {
|
||||||
|
String key = prefix;
|
||||||
|
int index = keyPrefixs.get(key);
|
||||||
|
if (index == -1) {
|
||||||
|
key = parent + k + "[" + (++index) + "]";
|
||||||
|
props.put(key, props.remove(prefix));
|
||||||
|
}
|
||||||
|
key = parent + k + "[" + (++index) + "]";
|
||||||
|
while (props.containsKey(key)) {
|
||||||
|
key = parent + k + "[" + (++index) + "]";
|
||||||
|
}
|
||||||
|
props.put(key, v);
|
||||||
|
keyPrefixs.put(prefix, index);
|
||||||
|
} else {
|
||||||
|
props.put(prefix, v);
|
||||||
|
keyPrefixs.put(prefix, -1);
|
||||||
|
}
|
||||||
|
},
|
||||||
|
(k, c) -> {
|
||||||
|
toProperties(props, keyPrefixs, parent + k + ".", c);
|
||||||
|
});
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -15,7 +15,11 @@ public class AnyValuePropertiesTest {
|
|||||||
|
|
||||||
public static void main(String[] args) throws Throwable {
|
public static void main(String[] args) throws Throwable {
|
||||||
AnyValuePropertiesTest test = new AnyValuePropertiesTest();
|
AnyValuePropertiesTest test = new AnyValuePropertiesTest();
|
||||||
|
test.run1();
|
||||||
|
test.run2();
|
||||||
|
test.run3();
|
||||||
test.run4();
|
test.run4();
|
||||||
|
test.run5();
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
@@ -84,6 +88,7 @@ public class AnyValuePropertiesTest {
|
|||||||
+ " }\r\n"
|
+ " }\r\n"
|
||||||
+ "}";
|
+ "}";
|
||||||
Assertions.assertEquals(result, AnyValue.loadFromProperties(properties).toString());
|
Assertions.assertEquals(result, AnyValue.loadFromProperties(properties).toString());
|
||||||
|
System.out.println("------------------------ 01 ------------------------");
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
@@ -111,6 +116,7 @@ public class AnyValuePropertiesTest {
|
|||||||
|
|
||||||
// System.out.println(conf.copy().merge(conf2));
|
// System.out.println(conf.copy().merge(conf2));
|
||||||
// System.out.println(conf);
|
// System.out.println(conf);
|
||||||
|
System.out.println("------------------------ 02 ------------------------");
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
@@ -125,6 +131,7 @@ public class AnyValuePropertiesTest {
|
|||||||
.addValue("desc", "nothing !!!"));
|
.addValue("desc", "nothing !!!"));
|
||||||
String json = "{\"name\":\"haha\",\"value\":{\"id\":\"1234\",\"key\":null,\"desc\":\"nothing !!!\"}}";
|
String json = "{\"name\":\"haha\",\"value\":{\"id\":\"1234\",\"key\":null,\"desc\":\"nothing !!!\"}}";
|
||||||
Assertions.assertEquals(json, conf.toJsonString());
|
Assertions.assertEquals(json, conf.toJsonString());
|
||||||
|
System.out.println("------------------------ 03 ------------------------");
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
@@ -139,5 +146,30 @@ public class AnyValuePropertiesTest {
|
|||||||
|
|
||||||
AnyValue conf = AnyValue.loadFromProperties(prop);
|
AnyValue conf = AnyValue.loadFromProperties(prop);
|
||||||
System.out.println(conf);
|
System.out.println(conf);
|
||||||
|
System.out.println("------------------------ 04 ------------------------");
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void run5() {
|
||||||
|
AnyValueWriter conf = AnyValue.create();
|
||||||
|
conf.addValue("name", "haha");
|
||||||
|
conf.addValue("name", "hehe");
|
||||||
|
conf.addValue("status", 45);
|
||||||
|
conf.addValue("name", AnyValueWriter.create("id", 123).addValue("desc", "test"));
|
||||||
|
conf.addValue("nodes", AnyValueWriter.create("time", 123).addValue("time", 456));
|
||||||
|
|
||||||
|
Properties props1 = conf.toProperties();
|
||||||
|
props1.forEach((k, v) -> System.out.println(k + " = " + v));
|
||||||
|
Properties props2 = new Properties();
|
||||||
|
props2.put("name[0]", "haha");
|
||||||
|
props2.put("name[1]", "hehe");
|
||||||
|
props2.put("status", "45");
|
||||||
|
props2.put("name.desc", "test");
|
||||||
|
props2.put("name.id", "123");
|
||||||
|
props2.put("nodes.time[0]", "123");
|
||||||
|
props2.put("nodes.time[1]", "456");
|
||||||
|
|
||||||
|
Assertions.assertEquals(props1.toString(), props2.toString());
|
||||||
|
System.out.println("------------------------ 05 ------------------------");
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user