Migrate from YangInstanceIdentifier.EMPTY
[controller.git] / opendaylight / md-sal / sal-clustering-commons / src / main / java / org / opendaylight / controller / cluster / datastore / util / AbstractDataTreeModificationCursor.java
1 /*
2  * Copyright (c) 2016 Brocade Communications 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.cluster.datastore.util;
9
10 import static com.google.common.base.Preconditions.checkArgument;
11 import static com.google.common.base.Preconditions.checkState;
12 import static com.google.common.base.Verify.verifyNotNull;
13
14 import com.google.common.annotations.Beta;
15 import java.util.Optional;
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.api.schema.NormalizedNode;
19 import org.opendaylight.yangtools.yang.data.api.schema.tree.DataTreeModificationCursor;
20
21 /**
22  * Abstract {@link DataTreeModificationCursor} which tracks the current path. Subclasses can get the current path
23  * via {@link #current()}. This class is NOT thread-safe.
24  *
25  * @author Thomas Pantelis
26  */
27 @Beta
28 public abstract class AbstractDataTreeModificationCursor implements DataTreeModificationCursor {
29     private YangInstanceIdentifier current = YangInstanceIdentifier.empty();
30
31     protected final YangInstanceIdentifier current() {
32         return current;
33     }
34
35     @Override
36     public final void enter(final PathArgument child) {
37         current = current.node(child);
38     }
39
40     @Override
41     public final void enter(final PathArgument... path) {
42         for (PathArgument arg : path) {
43             enter(arg);
44         }
45     }
46
47     @Override
48     public final void enter(final Iterable<PathArgument> path) {
49         for (PathArgument arg : path) {
50             enter(arg);
51         }
52     }
53
54     @Override
55     public final void exit() {
56         checkState(!current.isEmpty());
57         current = verifyNotNull(current.getParent());
58     }
59
60     @Override
61     public final void exit(final int depth) {
62         checkArgument(depth >= 0);
63
64         YangInstanceIdentifier next = current;
65         for (int i = 0; i < depth; ++i) {
66             next = next.getParent();
67             checkState(next != null);
68         }
69
70         current = next;
71     }
72
73     @Override
74     public final Optional<NormalizedNode<?, ?>> readNode(final PathArgument child) {
75         throw new UnsupportedOperationException("Not implemented");
76     }
77
78     @Override
79     public void close() {
80         // No-op
81     }
82 }