Make (Mutable)TreeNode a class
[yangtools.git] / data / yang-data-tree-ri / src / main / java / org / opendaylight / yangtools / yang / data / tree / impl / node / TreeNode.java
1 /*
2  * Copyright (c) 2014 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.tree.impl.node;
9
10 import static java.util.Objects.requireNonNull;
11
12 import com.google.common.base.MoreObjects;
13 import com.google.common.base.MoreObjects.ToStringHelper;
14 import org.eclipse.jdt.annotation.NonNullByDefault;
15 import org.opendaylight.yangtools.concepts.Identifiable;
16 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.PathArgument;
17 import org.opendaylight.yangtools.yang.data.api.schema.NormalizedNode;
18 import org.opendaylight.yangtools.yang.data.api.schema.tree.StoreTreeNode;
19
20 /**
21  * A very basic data tree node. It has a version (when it was last modified), a subtree version (when any of its
22  * children were modified) and some read-only data.
23  *
24  * <p>
25  * Semantic difference between these two is important when dealing with modifications involving parent/child
26  * relationships and what operations can be execute concurrently without creating a data dependency conflict.
27  *
28  * <p>
29  * A replace/delete operation cannot be applied to this node if the subtree version does not match. This mismatch
30  * still allows modifications to its descendants.
31  *
32  * <p>
33  * A mismatch in node version indicates a replacement, preventing a modification of descendants or itself.
34  */
35 // FIXME: BUG-2399: clarify that versioning rules are not enforced for non-presence containers, as they are not
36 //                  considered to be data nodes.
37 @NonNullByDefault
38 public abstract class TreeNode implements Identifiable<PathArgument>, StoreTreeNode<TreeNode> {
39     private final NormalizedNode data;
40     private final Version version;
41
42     TreeNode(final NormalizedNode data, final Version version) {
43         this.data = requireNonNull(data);
44         this.version = requireNonNull(version);
45     }
46
47     @Override
48     public final PathArgument getIdentifier() {
49         return data.getIdentifier();
50     }
51
52     /**
53      * Get the data node version. This version is updated whenever the data representation of this particular node
54      * changes as a result of a direct write to this node or to its parent nodes -- thus indicating that this node
55      * was logically replaced.
56      *
57      * @return Current data node version.
58      */
59     public final Version getVersion() {
60         return version;
61     }
62
63     /**
64      * Get the subtree version. This version is updated whenever the data representation of this particular node
65      * changes as the result of a direct or indirect child node being created, replaced or removed.
66      *
67      * @return Current subtree version.
68      */
69     public abstract Version getSubtreeVersion();
70
71     /**
72      * Get a read-only view of the underlying data.
73      *
74      * @return Unmodifiable view of the underlying data.
75      */
76     public final NormalizedNode getData() {
77         return data;
78     }
79
80     /**
81      * Get a mutable, isolated copy of the node.
82      *
83      * @return Mutable copy
84      */
85     public abstract MutableTreeNode mutable();
86
87     @Override
88     public final String toString() {
89         return addToStringAttributes(MoreObjects.toStringHelper(this).add("version", version)).toString();
90     }
91
92     abstract ToStringHelper addToStringAttributes(ToStringHelper helper);
93 }