This commit is contained in:
Looly 2020-05-03 09:10:06 +08:00
parent a4c8ebc572
commit 0321ce1120
4 changed files with 121 additions and 36 deletions

View File

@ -18,6 +18,7 @@
### Bug修复 ### Bug修复
* 【core 】 修复URLBuilder中请求参数有`&`导致的问题issue#850@Github * 【core 】 修复URLBuilder中请求参数有`&`导致的问题issue#850@Github
* 【core 】 修复URLBuilder中路径以`/`结尾导致的问题issue#I1G44J@Gitee
* 【db 】 修复SqlBuilder中orderBy无效问题issue#856@Github * 【db 】 修复SqlBuilder中orderBy无效问题issue#856@Github
------------------------------------------------------------------------------------------------------------- -------------------------------------------------------------------------------------------------------------

View File

@ -4,42 +4,50 @@ import java.io.Serializable;
import java.util.Collection; import java.util.Collection;
import java.util.Iterator; import java.util.Iterator;
import java.util.Map; import java.util.Map;
import java.util.Objects;
import java.util.Set; import java.util.Set;
import java.util.function.BiConsumer;
import java.util.function.BiFunction;
import java.util.function.Function;
/** /**
* Map包装类通过包装一个已有Map实现特定功能例如自定义Key的规则或Value规则 * Map包装类通过包装一个已有Map实现特定功能例如自定义Key的规则或Value规则
*
* @author looly
* *
* @param <K> 键类型 * @param <K> 键类型
* @param <V> 值类型 * @param <V> 值类型
* @author looly * @author looly
* @author looly
* @since 4.3.3 * @since 4.3.3
*/ */
public class MapWrapper<K, V> implements Map<K, V>, Iterable<Map.Entry<K, V>>, Serializable, Cloneable { public class MapWrapper<K, V> implements Map<K, V>, Iterable<Map.Entry<K, V>>, Serializable, Cloneable {
private static final long serialVersionUID = -7524578042008586382L; private static final long serialVersionUID = -7524578042008586382L;
/** 默认增长因子 */ /**
* 默认增长因子
*/
protected static final float DEFAULT_LOAD_FACTOR = 0.75f; protected static final float DEFAULT_LOAD_FACTOR = 0.75f;
/** 默认初始大小 */ /**
* 默认初始大小
*/
protected static final int DEFAULT_INITIAL_CAPACITY = 1 << 4; // aka 16 protected static final int DEFAULT_INITIAL_CAPACITY = 1 << 4; // aka 16
private final Map<K, V> raw; private final Map<K, V> raw;
/** /**
* 构造 * 构造
* *
* @param raw 被包装的Map * @param raw 被包装的Map
*/ */
public MapWrapper(Map<K, V> raw) { public MapWrapper(Map<K, V> raw) {
this.raw = raw; this.raw = raw;
} }
/** /**
* 获取原始的Map * 获取原始的Map
*
* @return Map * @return Map
*/ */
public Map<K, V> getRaw(){ public Map<K, V> getRaw() {
return this.raw; return this.raw;
} }
@ -113,8 +121,83 @@ public class MapWrapper<K, V> implements Map<K, V>, Iterable<Map.Entry<K, V>>, S
return this.entrySet().iterator(); return this.entrySet().iterator();
} }
@Override
public boolean equals(Object o) {
if (this == o) {
return true;
}
if (o == null || getClass() != o.getClass()) {
return false;
}
MapWrapper<?, ?> that = (MapWrapper<?, ?>) o;
return Objects.equals(raw, that.raw);
}
@Override
public int hashCode() {
return Objects.hash(raw);
}
@Override @Override
public String toString() { public String toString() {
return raw.toString(); return raw.toString();
} }
@Override
public void forEach(BiConsumer<? super K, ? super V> action) {
raw.forEach(action);
}
@Override
public void replaceAll(BiFunction<? super K, ? super V, ? extends V> function) {
raw.replaceAll(function);
}
@Override
public V putIfAbsent(K key, V value) {
return raw.putIfAbsent(key, value);
}
@Override
public boolean remove(Object key, Object value) {
return raw.remove(key, value);
}
@Override
public boolean replace(K key, V oldValue, V newValue) {
return raw.replace(key, oldValue, newValue);
}
@Override
public V replace(K key, V value) {
return raw.replace(key, value);
}
@Override
public V computeIfAbsent(K key, Function<? super K, ? extends V> mappingFunction) {
return raw.computeIfAbsent(key, mappingFunction);
}
//---------------------------------------------------------------------------- Override default methods start
@Override
public V getOrDefault(Object key, V defaultValue) {
return raw.getOrDefault(key, defaultValue);
}
@Override
public V computeIfPresent(K key, BiFunction<? super K, ? super V, ? extends V> remappingFunction) {
return raw.computeIfPresent(key, remappingFunction);
}
@Override
public V compute(K key, BiFunction<? super K, ? super V, ? extends V> remappingFunction) {
return raw.compute(key, remappingFunction);
}
@Override
public V merge(K key, V value, BiFunction<? super V, ? super V, ? extends V> remappingFunction) {
return raw.merge(key, value, remappingFunction);
}
//---------------------------------------------------------------------------- Override default methods end
} }

View File

@ -14,7 +14,7 @@ import java.util.Objects;
public class TolerantMap<K, V> extends MapWrapper<K, V> { public class TolerantMap<K, V> extends MapWrapper<K, V> {
private static final long serialVersionUID = -4158133823263496197L; private static final long serialVersionUID = -4158133823263496197L;
private final transient V defaultValue; private final V defaultValue;
/** /**
* 构造 * 构造
@ -83,11 +83,12 @@ public class TolerantMap<K, V> extends MapWrapper<K, V> {
if (o == null || getClass() != o.getClass()) { if (o == null || getClass() != o.getClass()) {
return false; return false;
} }
if (!super.equals(o)) { if (false == super.equals(o)) {
return false; return false;
} }
TolerantMap<?, ?> that = (TolerantMap<?, ?>) o; final TolerantMap<?, ?> that = (TolerantMap<?, ?>) o;
return getRaw().equals(that.getRaw()) && Objects.equals(defaultValue, that.defaultValue); return getRaw().equals(that.getRaw())
&& Objects.equals(defaultValue, that.defaultValue);
} }
@Override @Override

View File

@ -9,32 +9,32 @@ import java.util.HashMap;
public class TolerantMapTest { public class TolerantMapTest {
private final TolerantMap<String, String> map = TolerantMap.of(new HashMap<>(), "default"); private final TolerantMap<String, String> map = TolerantMap.of(new HashMap<>(), "default");
@Before @Before
public void before() { public void before() {
map.put("monday", "星期一"); map.put("monday", "星期一");
map.put("tuesday", "星期二"); map.put("tuesday", "星期二");
} }
@Test @Test
public void testSerialize() { public void testSerialize() {
byte[] bytes = ObjectUtil.serialize(map); byte[] bytes = ObjectUtil.serialize(map);
TolerantMap<String, String> serializedMap = ObjectUtil.deserialize(bytes); TolerantMap<String, String> serializedMap = ObjectUtil.deserialize(bytes);
assert serializedMap != map; assert serializedMap != map;
assert map.equals(serializedMap); assert map.equals(serializedMap);
} }
@Test @Test
public void testClone() { public void testClone() {
TolerantMap<String, String> clonedMap = ObjectUtil.clone(map); TolerantMap<String, String> clonedMap = ObjectUtil.clone(map);
assert clonedMap != map; assert clonedMap != map;
assert map.equals(clonedMap); assert map.equals(clonedMap);
} }
@Test @Test
public void testGet() { public void testGet() {
assert "星期二".equals(map.get("tuesday")); assert "星期二".equals(map.get("tuesday"));
assert "default".equals(map.get(RandomUtil.randomString(6))); assert "default".equals(map.get(RandomUtil.randomString(6)));
} }
} }