Merge "Startup archetype: remove 'Impl' from config subsystem Module name."
[controller.git] / opendaylight / md-sal / sal-inmemory-datastore / src / main / java / org / opendaylight / controller / md / sal / dom / store / impl / tree / ListenerWalker.java
1 /**
2  * Copyright (c) 2014 Cisco Systems, Inc. and others.  All rights reserved.
3  *
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
7  */
8 package org.opendaylight.controller.md.sal.dom.store.impl.tree;
9
10 import com.google.common.base.Preconditions;
11 import java.util.concurrent.atomic.AtomicIntegerFieldUpdater;
12 import java.util.concurrent.locks.Lock;
13
14 /**
15  * A walking context, pretty much equivalent to an iterator, but it
16  * exposes the underlying tree structure.
17  *
18  * @author Robert Varga
19  */
20 public class ListenerWalker implements AutoCloseable {
21     private static final AtomicIntegerFieldUpdater<ListenerWalker> CLOSED_UPDATER = AtomicIntegerFieldUpdater.newUpdater(ListenerWalker.class, "closed");
22     private final Lock lock;
23     private final ListenerNode node;
24
25     // Used via CLOSED_UPDATER
26     @SuppressWarnings("unused")
27     private volatile int closed = 0;
28
29     ListenerWalker(final Lock lock, final ListenerNode node) {
30         this.lock = Preconditions.checkNotNull(lock);
31         this.node = Preconditions.checkNotNull(node);
32     }
33
34     public ListenerNode getRootNode() {
35         return node;
36     }
37
38     @Override
39     public void close() {
40         if (CLOSED_UPDATER.compareAndSet(this, 0, 1)) {
41             lock.unlock();
42         }
43     }
44 }