Remove explicit default super-constructor calls
[yangtools.git] / common / util / src / main / java / org / opendaylight / yangtools / util / ReadOnlyTrieMap.java
index 4d429f1aaa11c899f6759cea472d48dabde75171..547ed99d290831459cd94efa126e8fed7155588e 100644 (file)
@@ -7,15 +7,15 @@
  */
 package org.opendaylight.yangtools.util;
 
+import com.google.common.base.Preconditions;
+import com.google.common.collect.ForwardingMap;
 import java.util.Map;
-
+import java.util.concurrent.atomic.AtomicReferenceFieldUpdater;
+import org.opendaylight.yangtools.triemap.ImmutableTrieMap;
+import org.opendaylight.yangtools.triemap.MutableTrieMap;
 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;
-
 /**
  * A read-only facade in front of a TrieMap. This is what we give out from
  * MapAdaptor.optimize(). The idea is that we want our read-only users to
@@ -24,36 +24,34 @@ import com.romix.scala.collection.concurrent.TrieMap;
  * changes, we can cache it for future reference.
  */
 final class ReadOnlyTrieMap<K, V> extends ForwardingMap<K, V> {
+    @SuppressWarnings("rawtypes")
+    private static final AtomicReferenceFieldUpdater<ReadOnlyTrieMap, ImmutableTrieMap> UPDATER =
+            AtomicReferenceFieldUpdater.newUpdater(ReadOnlyTrieMap.class, ImmutableTrieMap.class, "readOnly");
     private static final Logger LOG = LoggerFactory.getLogger(ReadOnlyTrieMap.class);
-    private final TrieMap<K, V> readWrite;
+    private final MutableTrieMap<K, V> readWrite;
     private final int size;
-    private volatile TrieMap<K, V> readOnly;
+    private volatile ImmutableTrieMap<K, V> readOnly;
 
-    ReadOnlyTrieMap(final TrieMap<K, V> map, final int size) {
-        super();
+    ReadOnlyTrieMap(final MutableTrieMap<K, V> map, final int size) {
         this.readWrite = Preconditions.checkNotNull(map);
         this.size = size;
     }
 
     Map<K, V> toReadWrite() {
-        final Map<K, V> ret = new ReadWriteTrieMap<>(readWrite.snapshot(), size);
+        final Map<K, V> ret = new ReadWriteTrieMap<>(readWrite.mutableSnapshot(), size);
         LOG.trace("Converted read-only TrieMap {} to read-write {}", this, ret);
         return ret;
     }
 
     @Override
     protected Map<K, V> delegate() {
-        TrieMap<K, V> ret = readOnly;
+        ImmutableTrieMap<K, V> ret = readOnly;
         if (ret == null) {
-            synchronized (this) {
+            ret = readWrite.immutableSnapshot();
+            if (!UPDATER.compareAndSet(this, null, ret)) {
                 ret = readOnly;
-                if (ret == null) {
-                    ret = readWrite.readOnlySnapshot();
-                    readOnly = ret;
-                }
             }
         }
-
         return ret;
     }