Bump upstreams for Silicon
[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.EffectiveModelContext;
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. Instances of this class can be reused multiple times and must be
19  * initialized before each use through {@link #initializeForPath(YangInstanceIdentifier)}.
20  */
21 @Beta
22 public abstract class ReusableNormalizedNodePruner extends AbstractNormalizedNodePruner {
23     private static final class SimplePruner extends ReusableNormalizedNodePruner {
24         SimplePruner(final EffectiveModelContext schemaContext) {
25             super(schemaContext);
26         }
27
28         SimplePruner(final DataSchemaContextTree tree) {
29             super(tree);
30         }
31
32         @Override
33         public ReusableNormalizedNodePruner duplicate() {
34             return new SimplePruner(getTree());
35         }
36     }
37
38     ReusableNormalizedNodePruner(final EffectiveModelContext schemaContext) {
39         super(schemaContext);
40     }
41
42     ReusableNormalizedNodePruner(final DataSchemaContextTree tree) {
43         super(tree);
44     }
45
46     /**
47      * Create a new pruner bound to a SchemaContext.
48      *
49      * @param schemaContext SchemaContext to use
50      * @return A new uninitialized pruner
51      * @throws NullPointerException if {@code schemaContext} is null
52      */
53     public static @NonNull ReusableNormalizedNodePruner forSchemaContext(final EffectiveModelContext schemaContext) {
54         return new SimplePruner(schemaContext);
55     }
56
57     /**
58      * Create a new pruner bound to a DataSchemaContextTree. This is a more efficient alternative of
59      * {@link #forSchemaContext(EffectiveModelContext)}.
60      *
61      * @param tree DataSchemaContextTree to use
62      * @return A new uninitialized pruner
63      * @throws NullPointerException if {@code schemaContext} is null
64      */
65     public static @NonNull ReusableNormalizedNodePruner forDataSchemaContext(final DataSchemaContextTree tree) {
66         return new SimplePruner(tree);
67     }
68
69     /**
70      * Return a new instance, which is backed but the same DataSchemaContextTree, but does not share any state and is
71      * uninitialized. This is equivalent to {@link #forDataSchemaContext(DataSchemaContextTree)} and is provided for
72      * convenience.
73      *
74      * @return A new uninitialized pruner bound to the same SchemaContext as this one.
75      */
76     public abstract @NonNull ReusableNormalizedNodePruner duplicate();
77
78     /**
79      * Initialize this pruner for processing a node at specified path.
80      *
81      * @param path Path that will be processed next
82      * @throws NullPointerException if {@code path} is null
83      */
84     public final void initializeForPath(final YangInstanceIdentifier path) {
85         initialize(path);
86     }
87
88     public final @NonNull ReusableNormalizedNodePruner withUintAdaption() {
89         return new UintAdaptingPruner(getTree());
90     }
91 }