package org.opendaylight.yangtools.util;
import com.google.common.base.Preconditions;
-import com.google.common.collect.Iterables;
import com.romix.scala.collection.concurrent.TrieMap;
-
import java.util.Collections;
import java.util.HashMap;
import java.util.Map;
import java.util.Map.Entry;
-
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
*/
if (size == 0) {
LOG.trace("Reducing input {} to an empty map", input);
- return Collections.<K, V>emptyMap();
+ return Collections.emptyMap();
}
/*
* map.
*/
if (useSingleton && size == 1) {
- final Entry<K, V> e = Iterables.getOnlyElement(input.entrySet());
+ final Entry<K, V> e = input.entrySet().iterator().next();
final Map<K, V> ret = Collections.singletonMap(e.getKey(), e.getValue());
LOG.trace("Reducing input {} to singleton map {}", input, ret);
return ret;
*/
package org.opendaylight.yangtools.util;
-import java.util.Map;
-
-import org.slf4j.Logger;
-import org.slf4j.LoggerFactory;
-
import com.google.common.base.Preconditions;
import com.google.common.collect.ForwardingMap;
import com.romix.scala.collection.concurrent.TrieMap;
+import java.util.Map;
+import java.util.concurrent.atomic.AtomicReferenceFieldUpdater;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
/**
* A read-only facade in front of a TrieMap. This is what we give out from
* changes, we can cache it for future reference.
*/
final class ReadOnlyTrieMap<K, V> extends ForwardingMap<K, V> {
+ @SuppressWarnings("rawtypes")
+ private static final AtomicReferenceFieldUpdater<ReadOnlyTrieMap, TrieMap> UPDATER =
+ AtomicReferenceFieldUpdater.newUpdater(ReadOnlyTrieMap.class, TrieMap.class, "readOnly");
private static final Logger LOG = LoggerFactory.getLogger(ReadOnlyTrieMap.class);
private final TrieMap<K, V> readWrite;
private final int size;
protected Map<K, V> delegate() {
TrieMap<K, V> ret = readOnly;
if (ret == null) {
- synchronized (this) {
+ ret = readWrite.readOnlySnapshot();
+ if (!UPDATER.compareAndSet(this, null, ret)) {
ret = readOnly;
- if (ret == null) {
- ret = readWrite.readOnlySnapshot();
- readOnly = ret;
- }
}
}
-
return ret;
}