Kv工具类名更新为Cfg
This commit is contained in:
+15
-15
@@ -22,51 +22,51 @@ import java.util.*;
|
||||
* @author: aoshiguchen
|
||||
* @date: 2023/3/1
|
||||
*/
|
||||
public class Kv<K,V> implements Map<K,V> , Serializable, Cloneable {
|
||||
public class Cfg<K,V> implements Map<K,V> , Serializable, Cloneable {
|
||||
private HashMap<K,K> aliasMap;
|
||||
private LinkedHashMap<K, V> _m;
|
||||
private HashMap<K, K> _k;
|
||||
private Locale locale;
|
||||
private String SEPARATOR = "_|-";
|
||||
private Kv<K,V> parent;
|
||||
private Cfg<K,V> parent;
|
||||
|
||||
private Kv() {
|
||||
private Cfg() {
|
||||
this(null, 16, null);
|
||||
}
|
||||
|
||||
private Kv(Kv<K,V> parent) {
|
||||
private Cfg(Cfg<K,V> parent) {
|
||||
this(parent, 16, null);
|
||||
}
|
||||
|
||||
public static <K,V> Kv<K,V> of() {
|
||||
return new Kv<>();
|
||||
public static <K,V> Cfg<K,V> of() {
|
||||
return new Cfg<>();
|
||||
}
|
||||
|
||||
public static <K,V> Kv<K,V> of(Kv<K,V> parent) {
|
||||
return new Kv(parent);
|
||||
public static <K,V> Cfg<K,V> of(Cfg<K,V> parent) {
|
||||
return new Cfg(parent);
|
||||
}
|
||||
|
||||
public Kv<K,V> set(K k, V v) {
|
||||
public Cfg<K,V> set(K k, V v) {
|
||||
this.put(k, v);
|
||||
return this;
|
||||
}
|
||||
|
||||
public Kv<K,V> setAlias(K k, K alias) {
|
||||
public Cfg<K,V> setAlias(K k, K alias) {
|
||||
this.aliasMap.put(convertKey(alias), convertKey(k));
|
||||
return this;
|
||||
}
|
||||
|
||||
public Kv(Kv<K,V> parent, int initialCapacity, Locale locale) {
|
||||
public Cfg(Cfg<K,V> parent, int initialCapacity, Locale locale) {
|
||||
this.parent = parent;
|
||||
this._m = new LinkedHashMap<K, V>(initialCapacity) {
|
||||
@Override
|
||||
public boolean containsKey(Object key) {
|
||||
return Kv.this.containsKey(key);
|
||||
return Cfg.this.containsKey(key);
|
||||
}
|
||||
|
||||
@Override
|
||||
protected boolean removeEldestEntry(Map.Entry<K, V> eldest) {
|
||||
boolean doRemove = Kv.this.removeEldestEntry(eldest);
|
||||
boolean doRemove = Cfg.this.removeEldestEntry(eldest);
|
||||
if (doRemove) {
|
||||
_k.remove(convertKey(eldest.getKey()));
|
||||
}
|
||||
@@ -220,8 +220,8 @@ public class Kv<K,V> implements Map<K,V> , Serializable, Cloneable {
|
||||
}
|
||||
|
||||
@Override
|
||||
protected Kv<K,V> clone() {
|
||||
Kv<K,V> res = new Kv<>();
|
||||
protected Cfg<K,V> clone() {
|
||||
Cfg<K,V> res = new Cfg<>();
|
||||
if (null != parent) {
|
||||
res.parent = parent.clone();
|
||||
}
|
||||
@@ -0,0 +1,83 @@
|
||||
package fun.asgc.neutrino.core.base;
|
||||
|
||||
import org.junit.Assert;
|
||||
import org.junit.Test;
|
||||
|
||||
/**
|
||||
* @author: aoshiguchen
|
||||
* @date: 2023/3/1
|
||||
*/
|
||||
public class CfgTest {
|
||||
@Test
|
||||
public void test1() {
|
||||
Object key1 = new Object();
|
||||
Cfg cfg0 = Cfg.of()
|
||||
.setAlias("app.server.http.http-port", "aaa")
|
||||
;
|
||||
|
||||
Cfg cfg1 = Cfg.of(cfg0)
|
||||
.set("app.server.http.http-port", 8080)
|
||||
.set("app.server.http.jks_Path", "/123/456")
|
||||
.set(key1, 1)
|
||||
.setAlias("app.server.http.http-port", "http-port")
|
||||
;
|
||||
|
||||
|
||||
Cfg cfg2 = Cfg.of(cfg1)
|
||||
.set("app.server.http.httpPort", 8081)
|
||||
.set("app.server.http.context-path", "/")
|
||||
;
|
||||
|
||||
Cfg cfg3 = Cfg.of(cfg2)
|
||||
.setAlias("app.server.http.http-port", "port")
|
||||
.setAlias("http-port", "p");
|
||||
|
||||
Assert.assertTrue(!cfg1.isEmpty());
|
||||
Assert.assertTrue(cfg1.size() == 3);
|
||||
Assert.assertTrue(cfg1.stackSize() == 3);
|
||||
Assert.assertTrue(cfg1.get("app.server.http.http-port").equals(8080));
|
||||
Assert.assertTrue(cfg1.get("app.server.http.httpPort").equals(8080));
|
||||
Assert.assertTrue(cfg1.idx(0).equals(8080));
|
||||
Assert.assertTrue(cfg1.get("app.server.http.jks_Path").equals("/123/456"));
|
||||
Assert.assertTrue(cfg1.get("app.server.http.jksPath").equals("/123/456"));
|
||||
Assert.assertTrue(cfg1.idx(1).equals("/123/456"));
|
||||
Assert.assertTrue(cfg1.containsKey("app.server.http.http-port"));
|
||||
Assert.assertTrue(cfg1.containsKey(key1));
|
||||
Assert.assertFalse(cfg1.containsKey(new Object()));
|
||||
|
||||
Assert.assertTrue(!cfg2.isEmpty());
|
||||
Assert.assertTrue(cfg2.size() == 2);
|
||||
Assert.assertTrue(cfg2.stackSize() == 5);
|
||||
Assert.assertTrue(cfg2.get("app.server.http.http-port").equals(8081));
|
||||
Assert.assertTrue(cfg2.get("app.server.http.httpPort").equals(8081));
|
||||
Assert.assertTrue(cfg2.get("app.server.http.jks-path") == null);
|
||||
Assert.assertTrue(cfg2.get("app.server.http.jksPath") == null);
|
||||
Assert.assertTrue(cfg2.get("app.server.http.context-path").equals("/"));
|
||||
|
||||
Assert.assertTrue(cfg2.stackGet("app.server.http.jks-path").equals("/123/456"));
|
||||
Assert.assertTrue(cfg2.stackGet("app.server.http.jksPath").equals("/123/456"));
|
||||
Assert.assertTrue(cfg2.stackGet("app.server.http.context-path").equals("/"));
|
||||
|
||||
Assert.assertTrue(cfg3.isEmpty());
|
||||
Assert.assertTrue(!cfg3.isStackEmpty());
|
||||
Assert.assertTrue(cfg3.size() == 0);
|
||||
Assert.assertTrue(cfg3.stackSize() == 5);
|
||||
Assert.assertTrue(cfg3.get("app.server.http.jks-path") == null);
|
||||
Assert.assertTrue(cfg3.get("app.server.http.jksPath") == null);
|
||||
Assert.assertTrue(cfg3.get("app.server.http.context-path") == null);
|
||||
Assert.assertTrue(cfg3.stackGet("app.server.http.jks-path").equals("/123/456"));
|
||||
Assert.assertTrue(cfg3.stackGet("app.server.http.jksPath").equals("/123/456"));
|
||||
Assert.assertTrue(cfg3.stackGet("app.server.http.context-path").equals("/"));
|
||||
|
||||
Assert.assertTrue(cfg3.stackGetOrDefault("app.server.http.timeout", 60000).equals(60000));
|
||||
|
||||
Assert.assertTrue(cfg3.takeStr("app.server.http.http-port").equals("8081"));
|
||||
Assert.assertNull(cfg3.take(null));
|
||||
Assert.assertTrue(cfg3.takeStr("httpPort").equals("8081"));
|
||||
Assert.assertTrue(cfg3.takeInt("http-port").equals(8081));
|
||||
Assert.assertTrue(cfg3.takeInt("port").equals(8081));
|
||||
// 注意:第3层的别名p,指向了第1层的别名http-port
|
||||
Assert.assertTrue(cfg3.takeInt("p").equals(8080));
|
||||
Assert.assertTrue(cfg3.takeStr("aaa").equals("8081"));
|
||||
}
|
||||
}
|
||||
@@ -1,83 +0,0 @@
|
||||
package fun.asgc.neutrino.core.base;
|
||||
|
||||
import org.junit.Assert;
|
||||
import org.junit.Test;
|
||||
|
||||
/**
|
||||
* @author: aoshiguchen
|
||||
* @date: 2023/3/1
|
||||
*/
|
||||
public class KvTest {
|
||||
@Test
|
||||
public void test1() {
|
||||
Object key1 = new Object();
|
||||
Kv kv0 = Kv.of()
|
||||
.setAlias("app.server.http.http-port", "aaa")
|
||||
;
|
||||
|
||||
Kv kv1 = Kv.of(kv0)
|
||||
.set("app.server.http.http-port", 8080)
|
||||
.set("app.server.http.jks_Path", "/123/456")
|
||||
.set(key1, 1)
|
||||
.setAlias("app.server.http.http-port", "http-port")
|
||||
;
|
||||
|
||||
|
||||
Kv kv2 = Kv.of(kv1)
|
||||
.set("app.server.http.httpPort", 8081)
|
||||
.set("app.server.http.context-path", "/")
|
||||
;
|
||||
|
||||
Kv kv3 = Kv.of(kv2)
|
||||
.setAlias("app.server.http.http-port", "port")
|
||||
.setAlias("http-port", "p");
|
||||
|
||||
Assert.assertTrue(!kv1.isEmpty());
|
||||
Assert.assertTrue(kv1.size() == 3);
|
||||
Assert.assertTrue(kv1.stackSize() == 3);
|
||||
Assert.assertTrue(kv1.get("app.server.http.http-port").equals(8080));
|
||||
Assert.assertTrue(kv1.get("app.server.http.httpPort").equals(8080));
|
||||
Assert.assertTrue(kv1.idx(0).equals(8080));
|
||||
Assert.assertTrue(kv1.get("app.server.http.jks_Path").equals("/123/456"));
|
||||
Assert.assertTrue(kv1.get("app.server.http.jksPath").equals("/123/456"));
|
||||
Assert.assertTrue(kv1.idx(1).equals("/123/456"));
|
||||
Assert.assertTrue(kv1.containsKey("app.server.http.http-port"));
|
||||
Assert.assertTrue(kv1.containsKey(key1));
|
||||
Assert.assertFalse(kv1.containsKey(new Object()));
|
||||
|
||||
Assert.assertTrue(!kv2.isEmpty());
|
||||
Assert.assertTrue(kv2.size() == 2);
|
||||
Assert.assertTrue(kv2.stackSize() == 5);
|
||||
Assert.assertTrue(kv2.get("app.server.http.http-port").equals(8081));
|
||||
Assert.assertTrue(kv2.get("app.server.http.httpPort").equals(8081));
|
||||
Assert.assertTrue(kv2.get("app.server.http.jks-path") == null);
|
||||
Assert.assertTrue(kv2.get("app.server.http.jksPath") == null);
|
||||
Assert.assertTrue(kv2.get("app.server.http.context-path").equals("/"));
|
||||
|
||||
Assert.assertTrue(kv2.stackGet("app.server.http.jks-path").equals("/123/456"));
|
||||
Assert.assertTrue(kv2.stackGet("app.server.http.jksPath").equals("/123/456"));
|
||||
Assert.assertTrue(kv2.stackGet("app.server.http.context-path").equals("/"));
|
||||
|
||||
Assert.assertTrue(kv3.isEmpty());
|
||||
Assert.assertTrue(!kv3.isStackEmpty());
|
||||
Assert.assertTrue(kv3.size() == 0);
|
||||
Assert.assertTrue(kv3.stackSize() == 5);
|
||||
Assert.assertTrue(kv3.get("app.server.http.jks-path") == null);
|
||||
Assert.assertTrue(kv3.get("app.server.http.jksPath") == null);
|
||||
Assert.assertTrue(kv3.get("app.server.http.context-path") == null);
|
||||
Assert.assertTrue(kv3.stackGet("app.server.http.jks-path").equals("/123/456"));
|
||||
Assert.assertTrue(kv3.stackGet("app.server.http.jksPath").equals("/123/456"));
|
||||
Assert.assertTrue(kv3.stackGet("app.server.http.context-path").equals("/"));
|
||||
|
||||
Assert.assertTrue(kv3.stackGetOrDefault("app.server.http.timeout", 60000).equals(60000));
|
||||
|
||||
Assert.assertTrue(kv3.takeStr("app.server.http.http-port").equals("8081"));
|
||||
Assert.assertNull(kv3.take(null));
|
||||
Assert.assertTrue(kv3.takeStr("httpPort").equals("8081"));
|
||||
Assert.assertTrue(kv3.takeInt("http-port").equals(8081));
|
||||
Assert.assertTrue(kv3.takeInt("port").equals(8081));
|
||||
// 注意:第3层的别名p,指向了第1层的别名http-port
|
||||
Assert.assertTrue(kv3.takeInt("p").equals(8080));
|
||||
Assert.assertTrue(kv3.takeStr("aaa").equals("8081"));
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user