2 * Copyright (c) 2015 Cisco Systems, Inc. and others. All rights reserved.
4 * This program and the accompanying materials are made available under the
5 * terms of the Eclipse Public License v1.0 which accompanies this distribution,
6 * and is available at http://www.eclipse.org/legal/epl-v10.html
8 package org.opendaylight.controller.md.sal.dom.spi;
10 import com.google.common.base.Preconditions;
11 import java.util.concurrent.atomic.AtomicIntegerFieldUpdater;
12 import java.util.concurrent.locks.Lock;
15 * A stable read-only snapshot of a {@link AbstractRegistrationTree}.
17 * @author Robert Varga
19 public final class RegistrationTreeSnapshot<T> implements AutoCloseable {
20 @SuppressWarnings("rawtypes")
21 private static final AtomicIntegerFieldUpdater<RegistrationTreeSnapshot> CLOSED_UPDATER = AtomicIntegerFieldUpdater.newUpdater(RegistrationTreeSnapshot.class, "closed");
22 private final RegistrationTreeNode<T> node;
23 private final Lock lock;
25 // Used via CLOSED_UPDATER
26 @SuppressWarnings("unused")
27 private volatile int closed = 0;
29 RegistrationTreeSnapshot(final Lock lock, final RegistrationTreeNode<T> node) {
30 this.lock = Preconditions.checkNotNull(lock);
31 this.node = Preconditions.checkNotNull(node);
34 public RegistrationTreeNode<T> getRootNode() {
40 if (CLOSED_UPDATER.compareAndSet(this, 0, 1)) {