This commit is contained in:
Looly 2023-04-18 22:40:05 +08:00
parent ff382aff85
commit 9d3551389e
5 changed files with 47 additions and 16 deletions

View File

@ -321,7 +321,7 @@ public class Dict extends CustomKeyMap<String, Object> implements TypeGetter<Str
* @return Dict 结果
* @since 4.0.10
*/
public Dict filter(final String... keys) {
public Dict filterNew(final String... keys) {
final Dict result = new Dict(keys.length, 1);
for (final String key : keys) {
@ -332,6 +332,17 @@ public class Dict extends CustomKeyMap<String, Object> implements TypeGetter<Str
return result;
}
/**
* 过滤Map去除指定键值对如果键不存在跳过
*
* @param keys 键列表
* @return Dict 结果
* @since 4.0.10
*/
public Dict removeNew(final String... keys) {
return MapUtil.removeAny(this.clone(), keys);
}
// -------------------------------------------------------------------- Set start
/**

View File

@ -437,7 +437,7 @@ public class MapUtil extends MapGetUtil {
for (final Map<K, V> map : mapList) {
for (final Entry<K, V> entry : map.entrySet()) {
resultMap.computeIfAbsent(entry.getKey(), k -> new ArrayList<>())
.add(entry.getValue());
.add(entry.getValue());
}
}
@ -625,9 +625,9 @@ public class MapUtil extends MapGetUtil {
public static <K, V> String join(final Map<K, V> map, final String separator, final String keyValueSeparator,
final Predicate<Entry<K, V>> predicate, final String... otherParams) {
return MapJoiner.of(separator, keyValueSeparator)
.append(map, predicate)
.append(otherParams)
.toString();
.append(map, predicate)
.append(otherParams)
.toString();
}
// ----------------------------------------------------------------------------------------------- filter
@ -951,13 +951,13 @@ public class MapUtil extends MapGetUtil {
*
* @param <K> Key类型
* @param <V> Value类型
* @param <T> Map类型
* @param map Map
* @param keys 键列表
* @return 修改后的key
* @since 5.0.5
*/
@SuppressWarnings("unchecked")
public static <K, V> Map<K, V> removeAny(final Map<K, V> map, final K... keys) {
public static <K, V, T extends Map<K, V>> T removeAny(final T map, final K... keys) {
for (final K key : keys) {
map.remove(key);
}
@ -1163,8 +1163,8 @@ public class MapUtil extends MapGetUtil {
*/
public static <K, V> Map.Entry<K, V> entry(final K key, final V value, final boolean isImmutable) {
return isImmutable ?
new AbstractMap.SimpleImmutableEntry<>(key, value) :
new AbstractMap.SimpleEntry<>(key, value);
new AbstractMap.SimpleImmutableEntry<>(key, value) :
new AbstractMap.SimpleEntry<>(key, value);
}
/**
@ -1272,7 +1272,7 @@ public class MapUtil extends MapGetUtil {
* This class should be removed once we drop Java 8 support.
*
* <p>
* 注意此方法只能用于JDK8
* 注意此方法只能用于JDK8
* </p>
*
* @param <K> 键类型
@ -1288,7 +1288,7 @@ public class MapUtil extends MapGetUtil {
if (null == value) {
value = mappingFunction.apply(key);
final V res = map.putIfAbsent(key, value);
if(null != res){
if (null != res) {
// issues#I6RVMY
// 如果旧值存在说明其他线程已经赋值成功putIfAbsent没有执行返回旧值
return res;

View File

@ -83,4 +83,16 @@ public class DictTest {
private String username;
private String nickname;
}
@Test
public void removeNewTest(){
final Dict dict = Dict.of()
.set("key1", 1)//int
.set("key2", 1000L)//long
.set("key3", DateTime.now());//Date
final Dict dict2 = dict.removeNew("key1");
Assertions.assertEquals(3, dict.size());
Assertions.assertEquals(2, dict2.size());
}
}

View File

@ -145,9 +145,9 @@ public class DialectRunner implements Serializable {
* @throws DbRuntimeException SQL执行异常
*/
public int insertOrUpdate(final Connection conn, final Entity record, final String... keys) throws DbRuntimeException {
final Entity where = record.filter(keys);
final Entity where = record.filterNew(keys);
if (MapUtil.isNotEmpty(where) && count(conn, Query.of(where)) > 0) {
return update(conn, record, where);
return update(conn, record.removeNew(keys), where);
} else {
return insert(conn, record)[0];
}

View File

@ -107,7 +107,10 @@ public class Entity extends Dict {
/* 字段名列表,用于限制加入的字段的值 */
private Set<String> fieldNames;
// --------------------------------------------------------------- Constructor start
// region ----- Constructor
/**
* 构造
*/
public Entity() {
}
@ -132,7 +135,7 @@ public class Entity extends Dict {
super(caseInsensitive);
this.tableName = tableName;
}
// --------------------------------------------------------------- Constructor end
// endregion
// --------------------------------------------------------------- Getters and Setters start
@ -259,7 +262,7 @@ public class Entity extends Dict {
* @since 4.0.10
*/
@Override
public Entity filter(final String... keys) {
public Entity filterNew(final String... keys) {
final Entity result = new Entity(this.tableName);
result.setFieldNames(this.fieldNames);
@ -271,6 +274,11 @@ public class Entity extends Dict {
return result;
}
@Override
public Entity removeNew(final String... keys) {
return (Entity) super.removeNew(keys);
}
// -------------------------------------------------------------------- Put and Set start
@Override
public Entity set(final String field, final Object value) {