/*
 * Copyright (c) 2014 Cisco Systems, Inc. and others.  All rights reserved.
 *
 * This program and the accompanying materials are made available under the
 * terms of the Eclipse Public License v1.0 which accompanies this distribution,
 * and is available at http://www.eclipse.org/legal/epl-v10.html
 */
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;

/**
 * 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
 * share a single snapshot. That snapshot is instantiated lazily either on
 * first access. Since we never leak the TrieMap and track its size as it
 * changes, we can cache it for future reference.
 */
final class ReadOnlyTrieMap<K, V> extends ForwardingMap<K, V> {
    private static final Logger LOG = LoggerFactory.getLogger(ReadOnlyTrieMap.class);
    private final TrieMap<K, V> readWrite;
    private final int size;
    private volatile TrieMap<K, V> readOnly;

    ReadOnlyTrieMap(final TrieMap<K, V> map, final int size) {
        super();
        this.readWrite = Preconditions.checkNotNull(map);
        this.size = size;
    }

    Map<K, V> toReadWrite() {
        final Map<K, V> ret = new ReadWriteTrieMap<>(readWrite.snapshot(), 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;
        if (ret == null) {
            synchronized (this) {
                ret = readOnly;
                if (ret == null) {
                    ret = readWrite.readOnlySnapshot();
                    readOnly = ret;
                }
            }
        }

        return ret;
    }

    @Override
    public int size() {
        return size;
    }
}

@@ -347,89 +347,8 @@
 l;%0A%0A
-import java.util.Map;%0A%0Aimport org.slf4j.Logger;%0Aimport org.slf4j.LoggerFactory;%0A%0A
 impo
@@ -489,16 +489,159 @@
 TrieMap;
+%0Aimport java.util.Map;%0Aimport java.util.concurrent.atomic.AtomicReferenceFieldUpdater;%0Aimport org.slf4j.Logger;%0Aimport org.slf4j.LoggerFactory;
 %0A%0A/**%0A *
@@ -1054,16 +1054,241 @@
 K, V%3E %7B%0A
+    @SuppressWarnings(%22rawtypes%22)%0A    private static final AtomicReferenceFieldUpdater%3CReadOnlyTrieMap, TrieMap%3E UPDATER =%0A            AtomicReferenceFieldUpdater.newUpdater(ReadOnlyTrieMap.class, TrieMap.class, %22readOnly%22);%0A
     priv
@@ -2005,206 +2005,134 @@
     
-synchronized (this) %7B%0A                ret = readOnly;%0A                if (ret == null) %7B%0A                    ret = readWrite.readOnlySnapshot();%0A                    readOnly = ret;%0A                %7D
+ret = readWrite.readOnlySnapshot();%0A            if (!UPDATER.compareAndSet(this, null, ret)) %7B%0A                ret = readOnly;
 %0A   
@@ -2148,25 +2148,24 @@
 %7D%0A        %7D%0A
-%0A
         retu

make ReadOnlyTrieMap concurrent