a8851a69f8d08718947ecf0ddcfcb3f239ee2839
[mdsal.git] / dom / mdsal-dom-spi / src / main / java / org / opendaylight / mdsal / dom / spi / shard / AbstractDataModificationCursor.java
1 /*
2  * Copyright (c) 2016 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.mdsal.dom.spi.shard;
9
10 import com.google.common.annotations.Beta;
11 import java.util.ArrayDeque;
12 import java.util.Deque;
13 import org.opendaylight.mdsal.dom.api.DOMDataTreeWriteCursor;
14 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.PathArgument;
15 import org.opendaylight.yangtools.yang.data.api.schema.NormalizedNode;
16
17 /**
18  * Abstract base class for {@link DOMDataTreeWriteCursor} implementations.
19  *
20  * @param <M> Modification type
21  *
22  * @deprecated This interface is scheduled for removal in the next major release.
23  */
24 @Beta
25 @Deprecated(forRemoval = true)
26 public abstract class AbstractDataModificationCursor<M> implements DOMDataTreeWriteCursor {
27     private final Deque<WriteCursorStrategy> stack = new ArrayDeque<>();
28
29     public AbstractDataModificationCursor(final M root) {
30         stack.push(getRootOperation(root));
31     }
32
33     protected abstract WriteCursorStrategy getRootOperation(M root);
34
35     private WriteCursorStrategy getCurrent() {
36         return stack.peek();
37     }
38
39     @Override
40     public void enter(final PathArgument child) {
41         WriteCursorStrategy nextOp = getCurrent().enter(child);
42         stack.push(nextOp);
43     }
44
45     @Override
46     public void enter(final PathArgument... path) {
47         for (PathArgument pathArgument : path) {
48             enter(pathArgument);
49         }
50     }
51
52     @Override
53     public void enter(final Iterable<PathArgument> path) {
54         for (PathArgument pathArgument : path) {
55             enter(pathArgument);
56         }
57     }
58
59     @Override
60     public void exit() {
61         WriteCursorStrategy op = stack.pop();
62         op.exit();
63     }
64
65     @Override
66     public void exit(final int depth) {
67         for (int i = 0; i < depth; i++) {
68             exit();
69         }
70     }
71
72     @Override
73     public abstract void close();
74
75     @Override
76     public void delete(final PathArgument child) {
77         getCurrent().delete(child);
78     }
79
80     @Override
81     public void merge(final PathArgument child, final NormalizedNode<?, ?> data) {
82         getCurrent().merge(child, data);
83     }
84
85     @Override
86     public void write(final PathArgument child, final NormalizedNode<?, ?> data) {
87         getCurrent().write(child, data);
88     }
89
90 }