3ac347ca844cd784889c054a43c3fdb34a564521
[yangtools.git] / yang / yang-data-impl / src / main / java / org / opendaylight / yangtools / yang / data / impl / CompositeNodeTOImpl.java
1 /*
2  * Copyright (c) 2013 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;
9
10 import java.util.ArrayList;
11 import java.util.List;
12 import java.util.Map;
13
14 import org.opendaylight.yangtools.yang.common.QName;
15 import org.opendaylight.yangtools.yang.data.api.CompositeNode;
16 import org.opendaylight.yangtools.yang.data.api.ModifyAction;
17 import org.opendaylight.yangtools.yang.data.api.MutableCompositeNode;
18 import org.opendaylight.yangtools.yang.data.api.Node;
19 import org.opendaylight.yangtools.yang.data.api.SimpleNode;
20
21 /**
22  * @author michal.rehak
23  * 
24  */
25 public class CompositeNodeTOImpl extends AbstractNodeTO<List<Node<?>>> implements CompositeNode {
26
27     private Map<QName, List<Node<?>>> nodeMap;
28
29     /**
30      * @param qname
31      * @param parent
32      *            use null to create top composite node (without parent)
33      * @param value
34      */
35     public CompositeNodeTOImpl(QName qname, CompositeNode parent, List<Node<?>> value) {
36         super(qname, parent, value);
37         if (value != null) {
38             nodeMap = NodeUtils.buildNodeMap(getValue());
39         }
40         init();
41     }
42
43     /**
44      * @param qname
45      * @param parent
46      *            use null to create top composite node (without parent)
47      * @param value
48      * @param modifyAction
49      */
50     public CompositeNodeTOImpl(QName qname, CompositeNode parent, List<Node<?>> value, ModifyAction modifyAction) {
51         super(qname, parent, value, modifyAction);
52         init();
53     }
54
55     /**
56      * @return the nodeMap
57      */
58     protected Map<QName, List<Node<?>>> getNodeMap() {
59         return nodeMap;
60     }
61
62     @Override
63     public List<Node<?>> getChildren() {
64         return getValue();
65     }
66
67     @Override
68     public SimpleNode<?> getFirstSimpleByName(QName leafQName) {
69         List<SimpleNode<?>> list = getSimpleNodesByName(leafQName);
70         if (list.isEmpty()) {
71             return null;
72         }
73         return list.get(0);
74     }
75
76     @Override
77     public List<CompositeNode> getCompositesByName(QName children) {
78         List<Node<?>> toFilter = getNodeMap().get(children);
79         List<CompositeNode> list = new ArrayList<CompositeNode>();
80         for (Node<?> node : toFilter) {
81             if (node instanceof CompositeNode) {
82                 list.add((CompositeNode) node);
83             }
84         }
85         return list;
86     }
87
88     @Override
89     public List<SimpleNode<?>> getSimpleNodesByName(QName children) {
90         List<Node<?>> toFilter = getNodeMap().get(children);
91         List<SimpleNode<?>> list = new ArrayList<SimpleNode<?>>();
92
93         for (Node<?> node : toFilter) {
94             if (node instanceof SimpleNode<?>) {
95                 list.add((SimpleNode<?>) node);
96             }
97         }
98         return list;
99     }
100
101     @Override
102     public CompositeNode getFirstCompositeByName(QName container) {
103         List<CompositeNode> list = getCompositesByName(container);
104         if (list.isEmpty()) {
105             return null;
106         }
107         return list.get(0);
108     }
109
110     /**
111      * @param leaf
112      * @return TODO:: do we need this method?
113      */
114     public SimpleNode<?> getFirstLeafByName(QName leaf) {
115         List<SimpleNode<?>> list = getSimpleNodesByName(leaf);
116         if (list.isEmpty()) {
117             return null;
118         }
119         return list.get(0);
120     }
121
122     @Override
123     public List<CompositeNode> getCompositesByName(String children) {
124         return getCompositesByName(new QName(getNodeType(), children));
125     }
126
127     @Override
128     public List<SimpleNode<?>> getSimpleNodesByName(String children) {
129         return getSimpleNodesByName(new QName(getNodeType(), children));
130     }
131
132     protected void init() {
133         if (getValue() != null) {
134             nodeMap = NodeUtils.buildNodeMap(getValue());
135         }
136     }
137
138     @Override
139     public MutableCompositeNode asMutable() {
140         throw new IllegalAccessError("cast to mutable is not supported - " + getClass().getSimpleName());
141     }
142
143     @Override
144     public String toString() {
145         return super.toString() + ", children.size = " + (getChildren() != null ? getChildren().size() : "n/a");
146     }
147
148 }