/*
 * Copyright (c) 2013 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.concepts;

import com.google.common.base.MoreObjects;
import com.google.common.base.MoreObjects.ToStringHelper;
import java.util.concurrent.atomic.AtomicIntegerFieldUpdater;

/**
 * Utility registration handle. It is a convenience for register-style method which can return an AutoCloseable realized
 * by a subclass of this class. Invoking the close() method triggers unregistration of the state the method installed.
 */
public abstract class AbstractRegistration implements Registration {
    private static final AtomicIntegerFieldUpdater<AbstractRegistration> CLOSED_UPDATER =
            AtomicIntegerFieldUpdater.newUpdater(AbstractRegistration.class, "closed");
    private volatile int closed = 0;

    /**
     * Remove the state referenced by this registration. This method is guaranteed to be called at most once.
     * Referenced state must be retained until this method is invoked.
     */
    protected abstract void removeRegistration();

    /**
     * Query the state of this registration. Returns true if it was closed.
     *
     * @return true if the registration was closed, false otherwise.
     */
    public final boolean isClosed() {
        return closed != 0;
    }

    @Override
    public final void close() {
        if (CLOSED_UPDATER.compareAndSet(this, 0, 1)) {
            removeRegistration();
        }
    }

    @Override
    public final String toString() {
        return addToStringAttributes(MoreObjects.toStringHelper(this).omitNullValues()).toString();
    }

    protected ToStringHelper addToStringAttributes(final ToStringHelper toStringHelper) {
        return toStringHelper.add("closed", closed);
    }
}

@@ -464,56 +464,68 @@
 ava.
-util.concurrent.atomic.AtomicIntegerFieldUpdater
+lang.invoke.MethodHandles;%0Aimport java.lang.invoke.VarHandle
 ;%0A%0A/
@@ -869,197 +869,400 @@
 nal 
-AtomicIntegerFieldUpdater%3CAbstractRegi
+VarHandle CLOSED;%0A%0A    // All access needs to go through this handle%0A    @SuppressWarnings(%22unused%22)%0A    private volatile byte closed;%0A%0A    
 st
-r
 ati
-on%3E CLOSED_UPDATER =%0A            AtomicIntegerFieldUpdater.newUpdater(AbstractRegistration.class, %22closed%22);%0A    private volatile int closed = 0;
+c %7B%0A        try %7B%0A            CLOSED = MethodHandles.lookup().findVarHandle(AbstractRegistration.class, %22closed%22, byte.class);%0A        %7D catch (ReflectiveOperationException e) %7B%0A            throw new ExceptionInInitializerError(e);%0A        %7D%0A    %7D
 %0A%0A  
@@ -1728,22 +1728,46 @@
  return 
-closed
+(byte) CLOSED.getAcquire(this)
  != 0;%0A 
@@ -1814,24 +1814,156 @@
 d close() %7B%0A
+        // We want full setVolatile() memory semantics here, as all state before calling this method%0A        // needs to be visible%0A
         if (
@@ -1972,16 +1972,8 @@
 OSED
-_UPDATER
 .com
@@ -1993,10 +1993,24 @@
 is, 
-0,
+(byte) 0, (byte)
  1))
@@ -2355,22 +2355,26 @@
 losed%22, 
-c
+isC
 losed
+()
 );%0A    %7D

Improve AbstractRegistration.isClosed() performance