mirror of
https://gitee.com/dromara/hutool.git
synced 2025-05-02 20:02:49 +08:00
add test
This commit is contained in:
parent
be65a142b4
commit
70c0f4cacc
@ -84,6 +84,20 @@ public class TableMap<K, V> implements Map<K, V>, Iterable<Map.Entry<K, V>>, Ser
|
||||
return null;
|
||||
}
|
||||
|
||||
/**
|
||||
* 根据value获得对应的key,只返回找到的第一个value对应的key值
|
||||
* @param value 值
|
||||
* @return 键
|
||||
* @since 5.3.3
|
||||
*/
|
||||
public K getKey(V value){
|
||||
final int index = values.indexOf(value);
|
||||
if (index > -1 && index < keys.size()) {
|
||||
return keys.get(index);
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取指定key对应的所有值
|
||||
*
|
||||
@ -145,19 +159,16 @@ public class TableMap<K, V> implements Map<K, V>, Iterable<Map.Entry<K, V>>, Ser
|
||||
values.clear();
|
||||
}
|
||||
|
||||
@SuppressWarnings("NullableProblems")
|
||||
@Override
|
||||
public Set<K> keySet() {
|
||||
return new HashSet<>(keys);
|
||||
}
|
||||
|
||||
@SuppressWarnings("NullableProblems")
|
||||
@Override
|
||||
public Collection<V> values() {
|
||||
return Collections.unmodifiableList(this.values);
|
||||
}
|
||||
|
||||
@SuppressWarnings("NullableProblems")
|
||||
@Override
|
||||
public Set<Map.Entry<K, V>> entrySet() {
|
||||
final Set<Map.Entry<K, V>> hashSet = new LinkedHashSet<>();
|
||||
|
@ -1,166 +1,80 @@
|
||||
package cn.hutool.core.map;
|
||||
|
||||
import cn.hutool.core.util.ObjectUtil;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.io.ObjectInputStream;
|
||||
import java.io.ObjectOutputStream;
|
||||
import java.io.Serializable;
|
||||
import java.util.*;
|
||||
import java.util.function.BiConsumer;
|
||||
import java.util.function.BiFunction;
|
||||
import java.util.function.Function;
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
import java.util.Objects;
|
||||
|
||||
/**
|
||||
* 一个可以提供默认值的Map
|
||||
*
|
||||
* @author pantao
|
||||
* @since 2020/1/3
|
||||
* @param <K> 键类型
|
||||
* @param <V> 值类型
|
||||
* @author pantao, looly
|
||||
*/
|
||||
public class TolerantMap<K, V> extends AbstractMap<K, V> implements Map<K, V>, Cloneable, Serializable {
|
||||
|
||||
public class TolerantMap<K, V> extends MapWrapper<K, V> {
|
||||
private static final long serialVersionUID = -4158133823263496197L;
|
||||
|
||||
private transient Map<K, V> map;
|
||||
|
||||
private transient V defaultValue;
|
||||
private final transient V defaultValue;
|
||||
|
||||
/**
|
||||
* 构造
|
||||
*
|
||||
* @param defaultValue 默认值
|
||||
*/
|
||||
public TolerantMap(V defaultValue) {
|
||||
this(new HashMap<>(), defaultValue);
|
||||
}
|
||||
|
||||
/**
|
||||
* 构造
|
||||
*
|
||||
* @param initialCapacity 初始容量
|
||||
* @param loadFactor 增长因子
|
||||
* @param defaultValue 默认值
|
||||
*/
|
||||
public TolerantMap(int initialCapacity, float loadFactor, V defaultValue) {
|
||||
this(new HashMap<>(initialCapacity, loadFactor), defaultValue);
|
||||
}
|
||||
|
||||
/**
|
||||
* 构造
|
||||
*
|
||||
* @param initialCapacity 初始容量
|
||||
* @param defaultValue 默认值
|
||||
*/
|
||||
public TolerantMap(int initialCapacity, V defaultValue) {
|
||||
this(new HashMap<>(initialCapacity), defaultValue);
|
||||
}
|
||||
|
||||
/**
|
||||
* 构造
|
||||
*
|
||||
* @param map Map实现
|
||||
* @param defaultValue 默认值
|
||||
*/
|
||||
public TolerantMap(Map<K, V> map, V defaultValue) {
|
||||
this.map = map;
|
||||
super(map);
|
||||
this.defaultValue = defaultValue;
|
||||
}
|
||||
|
||||
/**
|
||||
* 构建TolerantMap
|
||||
*
|
||||
* @param map map实现
|
||||
* @param defaultValue 默认值
|
||||
* @param <K> 键类型
|
||||
* @param <V> 值类型
|
||||
* @return TolerantMap
|
||||
*/
|
||||
public static <K, V> TolerantMap<K, V> of(Map<K, V> map, V defaultValue) {
|
||||
return new TolerantMap<>(map, defaultValue);
|
||||
}
|
||||
|
||||
@Override
|
||||
public int size() {
|
||||
return map.size();
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean isEmpty() {
|
||||
return map.isEmpty();
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean containsValue(Object value) {
|
||||
return map.containsValue(value);
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean containsKey(Object key) {
|
||||
return map.containsKey(key);
|
||||
}
|
||||
|
||||
@Override
|
||||
public V get(Object key) {
|
||||
return getOrDefault(key, defaultValue);
|
||||
}
|
||||
|
||||
@Override
|
||||
public V put(K key, V value) {
|
||||
return map.put(key, value);
|
||||
}
|
||||
|
||||
@Override
|
||||
public V remove(Object key) {
|
||||
return map.remove(key);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void putAll(Map<? extends K, ? extends V> m) {
|
||||
map.putAll(m);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void clear() {
|
||||
map.clear();
|
||||
}
|
||||
|
||||
@Override
|
||||
public Set<K> keySet() {
|
||||
return map.keySet();
|
||||
}
|
||||
|
||||
@Override
|
||||
public Collection<V> values() {
|
||||
return map.values();
|
||||
}
|
||||
|
||||
@Override
|
||||
public Set<Entry<K, V>> entrySet() {
|
||||
return map.entrySet();
|
||||
}
|
||||
|
||||
@Override
|
||||
public V getOrDefault(Object key, V defaultValue) {
|
||||
return map.getOrDefault(key, defaultValue);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void forEach(BiConsumer<? super K, ? super V> action) {
|
||||
map.forEach(action);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void replaceAll(BiFunction<? super K, ? super V, ? extends V> function) {
|
||||
map.replaceAll(function);
|
||||
}
|
||||
|
||||
@Override
|
||||
public V putIfAbsent(K key, V value) {
|
||||
return map.putIfAbsent(key, value);
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean remove(Object key, Object value) {
|
||||
return map.remove(key, value);
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean replace(K key, V oldValue, V newValue) {
|
||||
return map.replace(key, oldValue, newValue);
|
||||
}
|
||||
|
||||
@Override
|
||||
public V replace(K key, V value) {
|
||||
return map.replace(key, value);
|
||||
}
|
||||
|
||||
@Override
|
||||
public V computeIfAbsent(K key, Function<? super K, ? extends V> mappingFunction) {
|
||||
return map.computeIfAbsent(key, mappingFunction);
|
||||
}
|
||||
|
||||
@Override
|
||||
public V computeIfPresent(K key, BiFunction<? super K, ? super V, ? extends V> remappingFunction) {
|
||||
return map.computeIfPresent(key, remappingFunction);
|
||||
}
|
||||
|
||||
@Override
|
||||
public V compute(K key, BiFunction<? super K, ? super V, ? extends V> remappingFunction) {
|
||||
return map.compute(key, remappingFunction);
|
||||
}
|
||||
|
||||
@Override
|
||||
public V merge(K key, V value, BiFunction<? super V, ? super V, ? extends V> remappingFunction) {
|
||||
return map.merge(key, value, remappingFunction);
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean equals(Object o) {
|
||||
if (this == o) {
|
||||
@ -173,31 +87,16 @@ public class TolerantMap<K, V> extends AbstractMap<K, V> implements Map<K, V>, C
|
||||
return false;
|
||||
}
|
||||
TolerantMap<?, ?> that = (TolerantMap<?, ?>) o;
|
||||
return map.equals(that.map) && Objects.equals(defaultValue, that.defaultValue);
|
||||
return getRaw().equals(that.getRaw()) && Objects.equals(defaultValue, that.defaultValue);
|
||||
}
|
||||
|
||||
@Override
|
||||
public int hashCode() {
|
||||
return Objects.hash(map, defaultValue);
|
||||
return Objects.hash(getRaw(), defaultValue);
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
return "TolerantMap{" + "map=" + map + ", defaultValue=" + defaultValue + '}';
|
||||
}
|
||||
|
||||
private void writeObject(ObjectOutputStream s) throws IOException {
|
||||
s.writeObject(ObjectUtil.serialize(map));
|
||||
s.writeObject(ObjectUtil.serialize(defaultValue));
|
||||
}
|
||||
|
||||
private void readObject(ObjectInputStream s) throws IOException, ClassNotFoundException {
|
||||
map = ObjectUtil.deserialize((byte[]) s.readObject());
|
||||
defaultValue = ObjectUtil.deserialize((byte[]) s.readObject());
|
||||
}
|
||||
|
||||
@Override
|
||||
protected Object clone() throws CloneNotSupportedException {
|
||||
return super.clone();
|
||||
return "TolerantMap{" + "map=" + getRaw() + ", defaultValue=" + defaultValue + '}';
|
||||
}
|
||||
}
|
||||
|
@ -1,15 +1,13 @@
|
||||
package cn.hutool.core.convert;
|
||||
|
||||
import cn.hutool.core.map.MapBuilder;
|
||||
import org.junit.Assert;
|
||||
import org.junit.Test;
|
||||
|
||||
import java.util.HashMap;
|
||||
import java.util.LinkedHashMap;
|
||||
import java.util.Map;
|
||||
|
||||
import org.junit.Assert;
|
||||
import org.junit.Test;
|
||||
|
||||
import cn.hutool.core.convert.Convert;
|
||||
import cn.hutool.core.map.MapBuilder;
|
||||
|
||||
/**
|
||||
* Map转换单元测试
|
||||
*
|
||||
@ -31,7 +29,10 @@ public class MapConvertTest {
|
||||
|
||||
@Test
|
||||
public void mapToMapTest() {
|
||||
Map<String, Object> srcMap = MapBuilder.create(new HashMap<String, Object>()).put("name", "AAA").put("age", 45).map();
|
||||
Map<String, Object> srcMap = MapBuilder
|
||||
.create(new HashMap<String, Object>())
|
||||
.put("name", "AAA")
|
||||
.put("age", 45).map();
|
||||
|
||||
LinkedHashMap<?, ?> map = Convert.convert(LinkedHashMap.class, srcMap);
|
||||
Assert.assertEquals("AAA", map.get("name"));
|
||||
|
22
hutool-core/src/test/java/cn/hutool/core/map/BiMapTest.java
Normal file
22
hutool-core/src/test/java/cn/hutool/core/map/BiMapTest.java
Normal file
@ -0,0 +1,22 @@
|
||||
package cn.hutool.core.map;
|
||||
|
||||
import org.junit.Assert;
|
||||
import org.junit.Test;
|
||||
|
||||
import java.util.HashMap;
|
||||
|
||||
public class BiMapTest {
|
||||
|
||||
@Test
|
||||
public void getTest(){
|
||||
BiMap<String, Integer> biMap = new BiMap<>(new HashMap<>());
|
||||
biMap.put("aaa", 111);
|
||||
biMap.put("bbb", 222);
|
||||
|
||||
Assert.assertEquals(new Integer(111), biMap.get("aaa"));
|
||||
Assert.assertEquals(new Integer(222), biMap.get("bbb"));
|
||||
|
||||
Assert.assertEquals("aaa", biMap.getKey(111));
|
||||
Assert.assertEquals("bbb", biMap.getKey(222));
|
||||
}
|
||||
}
|
@ -0,0 +1,20 @@
|
||||
package cn.hutool.core.map;
|
||||
|
||||
import org.junit.Assert;
|
||||
import org.junit.Test;
|
||||
|
||||
public class TableMapTest {
|
||||
|
||||
@Test
|
||||
public void getTest(){
|
||||
TableMap<String, Integer> tableMap = new TableMap<>(16);
|
||||
tableMap.put("aaa", 111);
|
||||
tableMap.put("bbb", 222);
|
||||
|
||||
Assert.assertEquals(new Integer(111), tableMap.get("aaa"));
|
||||
Assert.assertEquals(new Integer(222), tableMap.get("bbb"));
|
||||
|
||||
Assert.assertEquals("aaa", tableMap.getKey(111));
|
||||
Assert.assertEquals("bbb", tableMap.getKey(222));
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue
Block a user