From 22b97d6194133a55c81dd11498c9eb507be73af2 Mon Sep 17 00:00:00 2001 From: huangsimin Date: Fri, 1 Jul 2022 12:01:21 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BF=AE=E6=94=B9=E4=B8=80=E4=BA=9B=E6=95=B0?= =?UTF-8?q?=E7=BB=84=E5=8F=AF=E8=83=BD=E4=BA=A7=E7=94=9Fbug,=20get?= =?UTF-8?q?=E6=94=B9=E6=88=90=E8=8C=83=E6=80=A7=E8=BF=94=E5=9B=9E=E5=80=BC?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- README.md | 6 +- src/main/java/com/yuandian/common/Config.java | 126 ++++++++++++++---- .../java/com/yuandian/common/ConfigTest.java | 19 ++- 3 files changed, 119 insertions(+), 32 deletions(-) diff --git a/README.md b/README.md index fdfe3a5..ee076d4 100644 --- a/README.md +++ b/README.md @@ -91,7 +91,11 @@ cnf.seek("key1", "key2").set("do_set"); // seek先定位keys #### setWithCreate方法 赋值. 如果key不存在就创建 ```java Config.UseConfig((cnf) -> { -int[] values = new int[]{1,2,3} ; // 数组配置 +ArrayList values = new ArrayList(); // 数组配置1 2 3 +values.add(1); +values.add(2); +values.add(3); + cnf.seek("listKeys1", "listKeys2").setWithCreate( values ); // 如果不存在key创建. // listKeys1: // listKeys2: diff --git a/src/main/java/com/yuandian/common/Config.java b/src/main/java/com/yuandian/common/Config.java index 18d47fb..21b5470 100644 --- a/src/main/java/com/yuandian/common/Config.java +++ b/src/main/java/com/yuandian/common/Config.java @@ -164,7 +164,7 @@ public class Config { */ public boolean exists(Object... keys) { Object cur = data; - for (var i = 0; i < keys.length - 1; i++) { + for (var i = 0; i < keys.length; i++) { if(cur == null) { return false; } @@ -176,9 +176,7 @@ public class Config { var opcur = (Map)cur ; cur = opcur.get(key); } - } - return true; } @@ -191,9 +189,9 @@ public class Config { */ public class Operator { Config config; - String[] keys; + Object[] keys; - Operator(Config config, String[] keys) { + Operator(Config config, Object[] keys) { this.config = config; this.keys = keys; } @@ -204,19 +202,34 @@ public class Config { * @return 返回自身。 */ public Operator createKeys() { - var cur = config.data; + Object cur = config.data; for (var i = 0;;) { var key = keys[i]; - var vobj = cur.get(key); - if (vobj == null) { - vobj = new LinkedHashMap<>(); - cur.put(key, vobj); + Object vobj; + // var vobj = cur.get(key); + + if(key.getClass() != String.class ) { + var opcur = (ArrayList)cur ; + vobj = opcur.get((int)key); + if (vobj == null) { + vobj = new ArrayList(); + opcur.set((int)key, vobj); + } + + } else { + var opcur = (Map)cur ; + vobj = opcur.get(key); + if (vobj == null) { + vobj = new LinkedHashMap<>(); + opcur.put((String)key, vobj); + } } + i++; if (i >= keys.length) { break; } - cur = (Map) vobj; + cur = vobj; } return this; } @@ -227,12 +240,28 @@ public class Config { * @param value 赋值 */ public void set(Object value) { - var cur = config.data; + Object cur = config.data; + for (var i = 0; i < keys.length - 1; i++) { var key = keys[i]; - cur = (Map) cur.get(key); + + if(key.getClass() != String.class ) { + var opcur = (ArrayList)cur ; + cur = opcur.get((int)key); + } else { + var opcur = (Map)cur ; + cur = opcur.get(key); + } + } + + var key = keys[keys.length - 1]; + if(key.getClass() != String.class ) { + var opcur = (ArrayList)cur ; + opcur.set((int)key, value); + } else { + var opcur = (Map)cur ; + opcur.put((String)key, value); } - cur.put(keys[keys.length - 1], value); } /** @@ -241,18 +270,48 @@ public class Config { * @param value 赋值 */ public void setWithCreate(Object value) { - var cur = config.data; + Object cur = config.data; for (var i = 0; i < keys.length - 1; i++) { var key = keys[i]; - var curValue = cur.get(key); - if(curValue == null) { - curValue = new LinkedHashMap<>(); - cur.put(key, curValue); + + + if(key.getClass() != String.class ) { + var opcur = (ArrayList)cur ; + Object vobj = opcur.get((int)key); + if (vobj == null) { + vobj = new ArrayList(); + opcur.set((int)key, vobj); + } + cur = vobj; + } else { + var opcur = (Map)cur ; + Object vobj = opcur.get(key); + if (vobj == null) { + vobj = new LinkedHashMap<>(); + opcur.put((String)key, vobj); + } + cur = vobj; } - cur = (Map)curValue; } - - cur.put(keys[keys.length - 1], value); + + var key = keys[keys.length - 1]; + if(key.getClass() != String.class ) { + var opcur = (ArrayList)cur ; + Object vobj = opcur.get((int)key); + if (vobj == null) { + vobj = new ArrayList(); + opcur.set((int)key, vobj); + } + + } else { + var opcur = (Map)cur ; + Object vobj = opcur.get(key); + if (vobj == null) { + vobj = new LinkedHashMap<>(); + opcur.put((String)key, vobj); + } + + } } } @@ -273,13 +332,28 @@ public class Config { * @param keys * @return */ - public Object remove(String... keys) { - var cur = data; + public T remove(Object... keys) { + Object cur = data; + for (var i = 0; i < keys.length - 1; i++) { var key = keys[i]; - cur = (Map) cur.get(key); + if(key.getClass() != String.class ) { + var opcur = (ArrayList)cur ; + cur = opcur.get((int)key); + } else { + var opcur = (Map)cur ; + cur = opcur.get(key); + } + } + + var key = keys[keys.length - 1]; + if(key.getClass() != String.class ) { + var opcur = (ArrayList)cur; + return (T)opcur.remove((int)key); + } else { + var opcur = (Map)cur ; + return (T)opcur.remove(key); } - return cur.remove(keys[keys.length - 1]); } /** diff --git a/src/test/java/com/yuandian/common/ConfigTest.java b/src/test/java/com/yuandian/common/ConfigTest.java index 63f44a1..2997b23 100644 --- a/src/test/java/com/yuandian/common/ConfigTest.java +++ b/src/test/java/com/yuandian/common/ConfigTest.java @@ -94,8 +94,6 @@ public class ConfigTest { Instant now = Instant.now(); cnf.data.put("use_config", now.toString()); - - try { log.info("{}",cnf.update()); } catch (NacosException e) { @@ -210,7 +208,11 @@ public class ConfigTest { void testSetListValues() throws Exception { Config.UseConfig((cnf) -> { - int[] values = new int[]{1,2,3} ; + ArrayList values = new ArrayList(); + values.add(1); + values.add(2); + values.add(3); + cnf.seek("listKeys1", "listKeys2").setWithCreate( values ); // 如果不存在key创建. // listKeys1: // listKeys2: @@ -233,7 +235,11 @@ public class ConfigTest { void testMapData() throws Exception { Config.UseConfig((cnf)->{ - int[] values = new int[]{1,2,3} ; + ArrayList values = new ArrayList() ; + values.add(1); + values.add(2); + values.add(3); + cnf.seek("listKeys1", "listKeys2").setWithCreate( values ); // 如果不存在key创建. // listKeys1: // listKeys2: @@ -249,9 +255,12 @@ public class ConfigTest { }); Config.UseConfig((cnf) -> { + + log.info("{}", cnf.data); // Map data var key2 = (Map) (cnf.data.get("listKeys1")); - ((int[])key2.get("listKeys2"))[1] = 10; // 数组 1 2 3 的2为 10 + ArrayList listKeys2 = (ArrayList)key2.get("listKeys2"); + listKeys2.set(1,10); // 数组 1 2 3 的2为 10 try { log.info("{}",cnf.update()); } catch (NacosException e) {