com.github.lontime.shaded.org.redisson.misc.BiHashMap Maven / Gradle / Ivy
The newest version!
/**
* Copyright (c) 2013-2021 Nikita Koksharov
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.github.lontime.shaded.org.redisson.misc;
import java.util.Collection;
import java.util.Collections;
import java.util.HashMap;
import java.util.Map;
import java.util.Set;
/**
* This is not a concurrent map.
*
* @author Rui Gu (https://github.com/jackygurui)
* @param key
* @param value
*/
public class BiHashMap implements Map {
private Map keyValueMap = new HashMap();
private Map valueKeyMap = new HashMap();
@Override
public int size() {
return keyValueMap.size();
}
@Override
public boolean isEmpty() {
return keyValueMap.isEmpty();
}
@Override
public boolean containsKey(Object key) {
return keyValueMap.containsKey(key);
}
@Override
public boolean containsValue(Object value) {
return valueKeyMap.containsKey(value);
}
@Override
public V get(Object key) {
return keyValueMap.get(key);
}
public K reverseGet(Object key) {
return valueKeyMap.get(key);
}
@Override
public V put(K key, V value) {
if (keyValueMap.containsKey(key)) {
valueKeyMap.remove(keyValueMap.get(key));
}
valueKeyMap.put(value, key);
return keyValueMap.put(key, value);
}
@Override
public V remove(Object key) {
V removed = keyValueMap.remove(key);
if (removed != null) {
valueKeyMap.remove(removed);
}
return removed;
}
@Override
public void putAll(Map extends K, ? extends V> m) {
for (Entry extends K, ? extends V> entry : m.entrySet()) {
put(entry.getKey(), entry.getValue());
}
}
@Override
public void clear() {
keyValueMap.clear();
valueKeyMap.clear();
}
@Override
public Set keySet() {
return keyValueMap.keySet();
}
public Set valueSet() {
return valueKeyMap.keySet();
}
@Override
public Collection values() {
return keyValueMap.values();
}
public Collection keys() {
return valueKeyMap.values();
}
@Override
public Set> entrySet() {
return keyValueMap.entrySet();
}
public Set> reverseEntrySet() {
return valueKeyMap.entrySet();
}
public void makeImmutable() {
keyValueMap = Collections.unmodifiableMap(keyValueMap);
valueKeyMap = Collections.unmodifiableMap(valueKeyMap);
}
}