2 * Copyright (c) 2014 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.impl.schema.tree;
10 import static java.util.Objects.requireNonNull;
12 import com.google.common.base.MoreObjects;
13 import org.eclipse.jdt.annotation.NonNull;
14 import org.opendaylight.yangtools.yang.data.api.schema.NormalizedNodes;
15 import org.opendaylight.yangtools.yang.data.spi.tree.TreeNode;
16 import org.opendaylight.yangtools.yang.model.api.EffectiveModelContext;
19 * Instances of this class hold the current state of a DataTree instance.
20 * The need for encapsulation stems from atomic updates, which potentially change
21 * multiple fields in one go.
23 final class DataTreeState {
24 private final LatestOperationHolder holder;
25 private final EffectiveModelContext schemaContext;
26 private final @NonNull TreeNode root;
28 private DataTreeState(final TreeNode root) {
29 this.root = requireNonNull(root);
30 holder = new LatestOperationHolder();
34 private DataTreeState(final TreeNode root, final LatestOperationHolder holder,
35 final EffectiveModelContext schemaContext) {
36 // It should be impossible to instantiate a new root without a SchemaContext
37 this.schemaContext = requireNonNull(schemaContext);
38 this.holder = requireNonNull(holder);
39 this.root = requireNonNull(root);
42 static DataTreeState createInitial(final TreeNode root) {
43 return new DataTreeState(root);
46 @NonNull TreeNode getRoot() {
50 InMemoryDataTreeSnapshot newSnapshot() {
51 return new InMemoryDataTreeSnapshot(schemaContext, root, holder.newSnapshot());
54 DataTreeState withSchemaContext(final EffectiveModelContext newSchemaContext,
55 final ModificationApplyOperation operation) {
56 holder.setCurrent(operation);
57 return new DataTreeState(root, holder, newSchemaContext);
60 DataTreeState withRoot(final TreeNode newRoot) {
61 return new DataTreeState(newRoot, holder, schemaContext);
65 public String toString() {
66 final TreeNode r = root;
67 return MoreObjects.toStringHelper(this).add("data", NormalizedNodes.toStringTree(r.getData())).toString();