org.bson.util.ComputingMap Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of mongo-java-driver Show documentation
Show all versions of mongo-java-driver Show documentation
The MongoDB Java Driver uber-artifact, containing mongodb-driver, mongodb-driver-core, and bson
/*
* Copyright 2008-present MongoDB, Inc.
*
* 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 org.bson.util;
import java.util.Collection;
import java.util.Map;
import java.util.Set;
import java.util.concurrent.ConcurrentMap;
import static org.bson.assertions.Assertions.notNull;
@SuppressWarnings("deprecation")
final class ComputingMap implements Map, Function {
public static Map create(final Function function) {
return new ComputingMap(CopyOnWriteMap.newHashMap(), function);
}
private final ConcurrentMap map;
private final Function function;
ComputingMap(final ConcurrentMap map, final Function function) {
this.map = notNull("map", map);
this.function = notNull("function", function);
}
public V get(final Object key) {
while (true) {
V v = map.get(key);
if (v != null) {
return v;
}
@SuppressWarnings("unchecked")
K k = (K) key;
V value = function.apply(k);
if (value == null) {
return null;
}
map.putIfAbsent(k, value);
}
}
public V apply(final K k) {
return get(k);
}
public V putIfAbsent(final K key, final V value) {
return map.putIfAbsent(key, value);
}
public boolean remove(final Object key, final Object value) {
return map.remove(key, value);
}
public boolean replace(final K key, final V oldValue, final V newValue) {
return map.replace(key, oldValue, newValue);
}
public V replace(final K key, final V value) {
return map.replace(key, value);
}
public int size() {
return map.size();
}
public boolean isEmpty() {
return map.isEmpty();
}
public boolean containsKey(final Object key) {
return map.containsKey(key);
}
public boolean containsValue(final Object value) {
return map.containsValue(value);
}
public V put(final K key, final V value) {
return map.put(key, value);
}
public V remove(final Object key) {
return map.remove(key);
}
public void putAll(final Map extends K, ? extends V> m) {
map.putAll(m);
}
public void clear() {
map.clear();
}
public Set keySet() {
return map.keySet();
}
public Collection values() {
return map.values();
}
public Set> entrySet() {
return map.entrySet();
}
public boolean equals(final Object o) {
return map.equals(o);
}
public int hashCode() {
return map.hashCode();
}
}
© 2015 - 2024 Weber Informatics LLC | Privacy Policy