Bug 4295: Fixed incorrectly introduced nodes when MERGE was followed by DELETE
[yangtools.git] / yang / yang-data-impl / src / main / java / org / opendaylight / yangtools / yang / data / impl / schema / tree / AbstractCursor.java
1 /*
2  * Copyright (c) 2015 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.yangtools.yang.data.impl.schema.tree;
9
10 import com.google.common.base.Preconditions;
11 import java.util.Arrays;
12 import java.util.concurrent.atomic.AtomicIntegerFieldUpdater;
13 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier;
14 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.PathArgument;
15 import org.opendaylight.yangtools.yang.data.api.schema.tree.DataTreeSnapshotCursor;
16
17 abstract class AbstractCursor<T extends AbstractCursorAware> implements DataTreeSnapshotCursor {
18     @SuppressWarnings("rawtypes")
19     private static final AtomicIntegerFieldUpdater<AbstractCursor> CLOSED_UPDATER =
20             AtomicIntegerFieldUpdater.newUpdater(AbstractCursor.class, "closed");
21     private final YangInstanceIdentifier rootPath;
22     private final T parent;
23     private volatile int closed;
24
25     AbstractCursor(final T parent, final YangInstanceIdentifier rootPath) {
26         this.rootPath = Preconditions.checkNotNull(rootPath);
27         this.parent = Preconditions.checkNotNull(parent);
28     }
29
30     final T getParent() {
31         return parent;
32     }
33
34     final YangInstanceIdentifier getRootPath() {
35         return rootPath;
36     }
37
38
39     final void ensureNotClosed() {
40         Preconditions.checkState(closed == 0, "Modification cursor has been closed");
41     }
42
43     @Override
44     public final void enter(final PathArgument... path) {
45         enter(Arrays.asList(path));
46     }
47
48     @Override
49     public final void exit() {
50         exit(1);
51     }
52
53     @Override
54     public final void close() {
55         if (CLOSED_UPDATER.compareAndSet(this, 0, 1)) {
56             parent.closeCursor(this);
57         }
58     }
59
60 }