Optimize ImmutableNodes.fromInstanceId()
[yangtools.git] / yang / yang-data-impl / src / main / java / org / opendaylight / yangtools / yang / data / impl / schema / InstanceIdToSimpleNodes.java
1 /*
2  * Copyright (c) 2015 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.impl.schema;
9
10 import static com.google.common.base.Preconditions.checkArgument;
11
12 import java.util.Iterator;
13 import java.util.Map.Entry;
14 import java.util.Optional;
15 import org.opendaylight.yangtools.yang.common.QName;
16 import org.opendaylight.yangtools.yang.data.api.ModifyAction;
17 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.NodeIdentifier;
18 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.NodeWithValue;
19 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.PathArgument;
20 import org.opendaylight.yangtools.yang.data.api.schema.LeafNode;
21 import org.opendaylight.yangtools.yang.data.api.schema.LeafSetEntryNode;
22 import org.opendaylight.yangtools.yang.data.api.schema.NormalizedNode;
23 import org.opendaylight.yangtools.yang.data.impl.schema.builder.api.NormalizedNodeAttrBuilder;
24 import org.opendaylight.yangtools.yang.model.api.LeafListSchemaNode;
25 import org.opendaylight.yangtools.yang.model.api.LeafSchemaNode;
26
27 /**
28 * Base strategy for converting an instance identifier into a normalized node structure for leaf and leaf-list types.
29 */
30 abstract class InstanceIdToSimpleNodes<T extends PathArgument> extends InstanceIdToNodes<T> {
31
32     InstanceIdToSimpleNodes(final T identifier) {
33         super(identifier);
34     }
35
36     @Override
37     final NormalizedNode<?, ?> create(final PathArgument first, final Iterator<PathArgument> others,
38             final Optional<NormalizedNode<?, ?>> deepestChild, final Optional<Entry<QName, ModifyAction>> operation) {
39         final NormalizedNodeAttrBuilder<? extends PathArgument, Object,
40                 ? extends NormalizedNode<? extends PathArgument, Object>> builder = getBuilder(first);
41
42         if (deepestChild.isPresent()) {
43             builder.withValue(deepestChild.get().getValue());
44         }
45
46         addModifyOpIfPresent(operation, builder);
47         return builder.build();
48     }
49
50     @Override
51     final InstanceIdToNodes<?> getChild(final PathArgument child) {
52         return null;
53     }
54
55     @Override
56     final boolean isMixin() {
57         return false;
58     }
59
60     abstract NormalizedNodeAttrBuilder<? extends PathArgument, Object,
61             ? extends NormalizedNode<? extends PathArgument, Object>> getBuilder(PathArgument node);
62
63     static final class LeafNormalization extends InstanceIdToSimpleNodes<NodeIdentifier> {
64         LeafNormalization(final LeafSchemaNode potential) {
65             super(new NodeIdentifier(potential.getQName()));
66         }
67
68         @Override
69         NormalizedNodeAttrBuilder<NodeIdentifier, Object, LeafNode<Object>> getBuilder(final PathArgument node) {
70             return Builders.leafBuilder().withNodeIdentifier(getIdentifier());
71         }
72     }
73
74     static final class LeafListEntryNormalization extends InstanceIdToSimpleNodes<NodeWithValue> {
75         LeafListEntryNormalization(final LeafListSchemaNode potential) {
76             super(new NodeWithValue<>(potential.getQName(), null));
77         }
78
79         @Override
80         NormalizedNodeAttrBuilder<NodeWithValue, Object, LeafSetEntryNode<Object>> getBuilder(final PathArgument node) {
81             checkArgument(node instanceof NodeWithValue);
82             return Builders.leafSetEntryBuilder().withNodeIdentifier((NodeWithValue<?>) node)
83                     .withValue(((NodeWithValue<?>) node).getValue());
84         }
85     }
86 }