3122b00860e42ac1bc7473c7c6aaf08613958f00
[mdsal.git] / dom / mdsal-dom-inmemory-datastore / src / main / java / org / opendaylight / mdsal / dom / store / inmemory / DelegatingWriteCursorStrategy.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
9 package org.opendaylight.mdsal.dom.store.inmemory;
10
11 import com.google.common.collect.ForwardingObject;
12 import org.opendaylight.mdsal.dom.api.DOMDataTreeWriteCursor;
13 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.PathArgument;
14 import org.opendaylight.yangtools.yang.data.api.schema.NormalizedNode;
15 import org.opendaylight.yangtools.yang.data.api.schema.NormalizedNodeContainer;
16
17 abstract class DelegatingWriteCursorStrategy extends ForwardingObject implements WriteCursorStrategy {
18
19     @Override
20     protected abstract DOMDataTreeWriteCursor delegate();
21
22     /**
23      * Returns strategy to be used on child nodes. Default implementation is to reuse same instance.
24      *
25      * @return Strategy to be used for child nodes.
26      */
27     protected DelegatingWriteCursorStrategy childStrategy() {
28         return this;
29     }
30
31     @Override
32     public WriteCursorStrategy enter(final PathArgument arg) {
33         delegate().enter(arg);
34         return childStrategy();
35     }
36
37     @Override
38     public void delete(final PathArgument arg) {
39         delegate().delete(arg);
40     }
41
42     @Override
43     public void merge(final PathArgument arg, final NormalizedNode<?, ?> data) {
44         delegate().merge(arg, data);
45     }
46
47     @Override
48     public void write(final PathArgument arg, final NormalizedNode<?, ?> data) {
49         delegate().write(arg, data);
50     }
51
52     @Override
53     public void mergeToCurrent(final NormalizedNodeContainer<?, ?, ?> data) {
54         for (NormalizedNode<?, ?> child : data.getValue()) {
55             delegate().merge(child.getIdentifier(), child);
56         }
57     }
58
59     @Override
60     public void writeToCurrent(final NormalizedNodeContainer<?, ?, ?> data) {
61         for (NormalizedNode<?, ?> child : data.getValue()) {
62             delegate().write(child.getIdentifier(), child);
63         }
64     }
65
66     /**
67      * Operation performed to exit current logical level, default implementation calls
68      * {@link DOMDataTreeWriteCursor#exit()} on underlaying cursor.
69      *
70      * Subclasses may override this to customize exit strategy.
71      *
72      */
73     @Override
74     public void exit() {
75         delegate().exit();
76     }
77 }