a93d1f1883b8b3900dea5fe6351b434687ae3517
[yangtools.git] / data / yang-data-util / src / main / java / org / opendaylight / yangtools / yang / data / util / ChoiceNodeContextNode.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.util;
9
10 import com.google.common.collect.ImmutableMap;
11 import java.util.Set;
12 import org.opendaylight.yangtools.yang.common.QName;
13 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.NodeIdentifier;
14 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.PathArgument;
15 import org.opendaylight.yangtools.yang.model.api.CaseSchemaNode;
16 import org.opendaylight.yangtools.yang.model.api.ChoiceSchemaNode;
17 import org.opendaylight.yangtools.yang.model.api.DataSchemaNode;
18
19 final class ChoiceNodeContextNode extends AbstractMixinContextNode<NodeIdentifier> {
20     private final ImmutableMap<QName, DataSchemaContextNode<?>> byQName;
21     private final ImmutableMap<PathArgument, DataSchemaContextNode<?>> byArg;
22
23     ChoiceNodeContextNode(final ChoiceSchemaNode schema) {
24         super(NodeIdentifier.create(schema.getQName()), schema);
25         ImmutableMap.Builder<QName, DataSchemaContextNode<?>> byQNameBuilder = ImmutableMap.builder();
26         ImmutableMap.Builder<PathArgument, DataSchemaContextNode<?>> byArgBuilder = ImmutableMap.builder();
27
28         for (CaseSchemaNode caze : schema.getCases()) {
29             for (DataSchemaNode cazeChild : caze.getChildNodes()) {
30                 DataSchemaContextNode<?> childOp = DataSchemaContextNode.of(cazeChild);
31                 byArgBuilder.put(childOp.getIdentifier(), childOp);
32                 for (QName qname : childOp.getQNameIdentifiers()) {
33                     byQNameBuilder.put(qname, childOp);
34                 }
35             }
36         }
37         byQName = byQNameBuilder.build();
38         byArg = byArgBuilder.build();
39     }
40
41     @Override
42     public DataSchemaContextNode<?> getChild(final PathArgument child) {
43         return byArg.get(child);
44     }
45
46     @Override
47     public DataSchemaContextNode<?> getChild(final QName child) {
48         return byQName.get(child);
49     }
50
51     @Override
52     protected Set<QName> getQNameIdentifiers() {
53         return byQName.keySet();
54     }
55 }