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.yangtools.yang.data.tree.impl;
10 import static com.google.common.base.Preconditions.checkState;
11 import static java.util.Objects.requireNonNull;
13 import java.lang.invoke.MethodHandles;
14 import java.lang.invoke.VarHandle;
15 import java.util.Arrays;
16 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier;
17 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.PathArgument;
18 import org.opendaylight.yangtools.yang.data.tree.api.DataTreeSnapshotCursor;
20 abstract class AbstractCursor<T extends AbstractCursorAware> implements DataTreeSnapshotCursor {
21 private static final VarHandle CLOSED;
25 CLOSED = MethodHandles.lookup().findVarHandle(AbstractCursor.class, "closed", boolean.class);
26 } catch (NoSuchFieldException | IllegalAccessException e) {
27 throw new ExceptionInInitializerError(e);
31 private final YangInstanceIdentifier rootPath;
32 private final T parent;
34 private volatile boolean closed;
36 AbstractCursor(final T parent, final YangInstanceIdentifier rootPath) {
37 this.rootPath = requireNonNull(rootPath);
38 this.parent = requireNonNull(parent);
45 final YangInstanceIdentifier getRootPath() {
50 final void ensureNotClosed() {
51 checkState(!closed, "Modification cursor has been closed");
55 public final void enter(final PathArgument... path) {
56 enter(Arrays.asList(path));
60 public final void exit() {
65 public final void close() {
66 if (CLOSED.compareAndSet(this, false, true)) {
67 parent.closeCursor(this);