/*
 * Copyright (c) 2015 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.yang.data.tree.impl;

import com.google.common.base.Preconditions;
import java.util.concurrent.atomic.AtomicReferenceFieldUpdater;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

abstract class AbstractCursorAware {
    private static final Logger LOG = LoggerFactory.getLogger(AbstractCursorAware.class);
    @SuppressWarnings("rawtypes")
    private static final AtomicReferenceFieldUpdater<AbstractCursorAware, AbstractCursor> CURSOR_UPDATER =
            AtomicReferenceFieldUpdater.newUpdater(AbstractCursorAware.class, AbstractCursor.class, "cursor");
    private volatile AbstractCursor<?> cursor = null;

    protected <T extends AbstractCursor<?>> T openCursor(final T cursorToOpen) {
        final boolean success = CURSOR_UPDATER.compareAndSet(this, null, cursorToOpen);
        Preconditions.checkState(success, "Modification %s has cursor attached at path %s", this,
            this.cursor.getRootPath());
        return cursorToOpen;
    }

    final void closeCursor(final AbstractCursor<?> cursorToClose) {
        final boolean success = CURSOR_UPDATER.compareAndSet(this, cursorToClose, null);
        if (!success) {
            LOG.warn("Attempted to close cursor {} while {} is open", cursorToClose, this.cursor);
        }
    }
}

@@ -369,108 +369,73 @@
 ort 
-com.google.common.base.Preconditions;%0Aimport java.util.concurrent.atomic.AtomicReferenceFieldUpdater
+java.lang.invoke.MethodHandles;%0Aimport java.lang.invoke.VarHandle
 ;%0Aim
@@ -625,195 +625,132 @@
     
-@SuppressWarnings(%22rawtypes%22)%0A    private static final AtomicReferenceFieldUpdater%3CAbstractCursorAware, AbstractCursor%3E CURSOR_UPDATER =%0A            AtomicReferenceFieldUpdater.newUpdater
+private static final VarHandle CURSOR;%0A%0A    static %7B%0A        try %7B%0A            CURSOR = MethodHandles.lookup().findVarHandle
 (Abs
@@ -772,16 +772,26 @@
 e.class,
+ %22cursor%22,
  Abstrac
@@ -807,20 +807,181 @@
 lass
-, %22cursor%22);
+);%0A        %7D catch (NoSuchFieldException %7C IllegalAccessException e) %7B%0A            throw new ExceptionInInitializerError(e);%0A        %7D%0A    %7D%0A%0A    @SuppressWarnings(%22unused%22)
 %0A   
@@ -1026,15 +1026,8 @@
 rsor
- = null
 ;%0A%0A 
@@ -1124,39 +1124,47 @@
 nal 
-boolean success = CURSOR_UPDATE
+var witness = (AbstractCursor%3C?%3E) CURSO
 R.co
@@ -1167,27 +1167,32 @@
 R.compareAnd
-Set
+Exchange
 (this, null,
@@ -1219,41 +1219,91 @@
     
-Preconditions.checkState(success,
+if (witness != null) %7B%0A            throw new IllegalStateException(%0A               
  %22Mo
@@ -1313,18 +1313,28 @@
 ication 
-%25s
+%22 + this + %22
  has cur
@@ -1358,42 +1358,19 @@
 ath 
-%25s%22, this,%0A            this.cursor
+%22 + witness
 .get
@@ -1382,16 +1382,26 @@
 ath());%0A
+        %7D%0A
         
@@ -1514,39 +1514,47 @@
 nal 
-boolean success = CURSOR_UPDATE
+var witness = (AbstractCursor%3C?%3E) CURSO
 R.co
@@ -1565,11 +1565,16 @@
 eAnd
-Set
+Exchange
 (thi
@@ -1614,16 +1614,32 @@
 if (
-!success
+witness != cursorToClose
 ) %7B%0A
@@ -1727,19 +1727,15 @@
 se, 
-this.cursor
+witness
 );%0A 

Use VarHandle in AbstractCursorAware