mirror of
https://gitee.com/chinabugotech/hutool.git
synced 2025-04-19 03:01:48 +08:00
fix code
This commit is contained in:
parent
69fed967e5
commit
3aac1e9af6
@ -1,6 +1,8 @@
|
||||
package cn.hutool.core.map;
|
||||
|
||||
import java.util.Map;
|
||||
import java.util.function.BiFunction;
|
||||
import java.util.function.Function;
|
||||
|
||||
/**
|
||||
* 双向Map<br>
|
||||
@ -83,4 +85,49 @@ public class BiMap<K, V> extends MapWrapper<K, V> {
|
||||
public K getKey(final V value) {
|
||||
return getInverse().get(value);
|
||||
}
|
||||
|
||||
@Override
|
||||
public V putIfAbsent(final K key, final V value) {
|
||||
if (null != this.inverse) {
|
||||
this.inverse.putIfAbsent(value, key);
|
||||
}
|
||||
return super.putIfAbsent(key, value);
|
||||
}
|
||||
|
||||
@Override
|
||||
public V computeIfAbsent(final K key, final Function<? super K, ? extends V> mappingFunction) {
|
||||
final V result = super.computeIfAbsent(key, mappingFunction);
|
||||
resetInverseMap();
|
||||
return result;
|
||||
}
|
||||
|
||||
@Override
|
||||
public V computeIfPresent(final K key, final BiFunction<? super K, ? super V, ? extends V> remappingFunction) {
|
||||
final V result = super.computeIfPresent(key, remappingFunction);
|
||||
resetInverseMap();
|
||||
return result;
|
||||
}
|
||||
|
||||
@Override
|
||||
public V compute(final K key, final BiFunction<? super K, ? super V, ? extends V> remappingFunction) {
|
||||
final V result = super.compute(key, remappingFunction);
|
||||
resetInverseMap();
|
||||
return result;
|
||||
}
|
||||
|
||||
@Override
|
||||
public V merge(final K key, final V value, final BiFunction<? super V, ? super V, ? extends V> remappingFunction) {
|
||||
final V result = super.merge(key, value, remappingFunction);
|
||||
resetInverseMap();
|
||||
return result;
|
||||
}
|
||||
|
||||
/**
|
||||
* 重置反转的Map,如果反转map为空,则不操作。
|
||||
*/
|
||||
private void resetInverseMap() {
|
||||
if (null != this.inverse) {
|
||||
inverse = null;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -174,8 +174,8 @@ public class SyncFinisher implements Closeable {
|
||||
} else {
|
||||
this.executorService.shutdown();
|
||||
}
|
||||
this.executorService = null;
|
||||
}
|
||||
this.executorService = null;
|
||||
|
||||
clearWorker();
|
||||
}
|
||||
|
@ -19,4 +19,26 @@ public class BiMapTest {
|
||||
Assert.assertEquals("aaa", biMap.getKey(111));
|
||||
Assert.assertEquals("bbb", biMap.getKey(222));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void computeIfAbsentTest(){
|
||||
final BiMap<String, Integer> biMap = new BiMap<>(new HashMap<>());
|
||||
biMap.put("aaa", 111);
|
||||
biMap.put("bbb", 222);
|
||||
|
||||
biMap.computeIfAbsent("ccc", s -> 333);
|
||||
Assert.assertEquals(new Integer(333), biMap.get("ccc"));
|
||||
Assert.assertEquals("ccc", biMap.getKey(333));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void putIfAbsentTest(){
|
||||
final BiMap<String, Integer> biMap = new BiMap<>(new HashMap<>());
|
||||
biMap.put("aaa", 111);
|
||||
biMap.put("bbb", 222);
|
||||
|
||||
biMap.putIfAbsent("ccc", 333);
|
||||
Assert.assertEquals(new Integer(333), biMap.get("ccc"));
|
||||
Assert.assertEquals("ccc", biMap.getKey(333));
|
||||
}
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user