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