Add ReusableNormalizedNodePruner
[controller.git] / opendaylight / md-sal / sal-clustering-commons / src / main / java / org / opendaylight / controller / cluster / datastore / node / utils / transformer / ReusableNormalizedNodePruner.java
1 /*
2  * Copyright (c) 2019 PANTHEON.tech, s.r.o. 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.node.utils.transformer;
9
10 import com.google.common.annotations.Beta;
11 import org.eclipse.jdt.annotation.NonNull;
12 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier;
13 import org.opendaylight.yangtools.yang.data.util.DataSchemaContextTree;
14 import org.opendaylight.yangtools.yang.model.api.SchemaContext;
15
16 /**
17  * The NormalizedNodePruner removes all nodes from the input NormalizedNode that do not have a corresponding
18  * schema element in the passed in SchemaContext.
19  *
20  * <p>
21  * Unlike {@link NormalizedNodePruner}, this class can be reused multiple times and must be initialized before each use
22  * through {@link #initializeForPath(YangInstanceIdentifier)}.
23  */
24 @Beta
25 public final class ReusableNormalizedNodePruner extends AbstractNormalizedNodePruner {
26     private ReusableNormalizedNodePruner(final SchemaContext schemaContext) {
27         super(schemaContext);
28     }
29
30     private ReusableNormalizedNodePruner(final DataSchemaContextTree tree) {
31         super(tree);
32     }
33
34     /**
35      * Create a new pruner bound to a SchemaContext.
36      *
37      * @param schemaContext SchemaContext to use
38      * @return A new uninitialized pruner
39      * @throws NullPointerException if {@code schemaContext} is null
40      */
41     public static @NonNull ReusableNormalizedNodePruner forSchemaContext(final SchemaContext schemaContext) {
42         return new ReusableNormalizedNodePruner(schemaContext);
43     }
44
45     /**
46      * Create a new pruner bound to a DataSchemaContextTree. This is a more efficient alternative of
47      * {@link #forSchemaContext(SchemaContext)}.
48      *
49      * @param tree DataSchemaContextTree to use
50      * @return A new uninitialized pruner
51      * @throws NullPointerException if {@code schemaContext} is null
52      */
53     public static @NonNull ReusableNormalizedNodePruner forDataSchemaContext(final DataSchemaContextTree tree) {
54         return new ReusableNormalizedNodePruner(tree);
55     }
56
57     /**
58      * Return a new instance, which is backed but the same DataSchemaContextTree, but does not share any state and is
59      * uninitialized. This is equivalent to {@link #forDataSchemaContext(DataSchemaContextTree)} and is provided for
60      * convenience.
61      *
62      * @return A new uninitialized pruner bound to the same SchemaContext as this one.
63      */
64     public @NonNull ReusableNormalizedNodePruner duplicate() {
65         return new ReusableNormalizedNodePruner(getTree());
66     }
67
68     /**
69      * Initialize this pruner for processing a node at specified path.
70      *
71      * @param path Path that will be processed next
72      * @throws NullPointerException if {@code path} is null
73      */
74     public void initializeForPath(final YangInstanceIdentifier path) {
75         initialize(path);
76     }
77 }