5ba7ed8b3c2853c3fc2f4786a8faa2ea7e58c25f
[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         return list;
98     }
99
100     @Override
101     public CompositeNode getFirstCompositeByName(QName container) {
102         List<CompositeNode> list = getCompositesByName(container);
103         if (list.isEmpty()) {
104             return null;
105         }
106         return list.get(0);
107     }
108
109     /**
110      * @param leaf
111      * @return TODO:: do we need this method?
112      */
113     public SimpleNode<?> getFirstLeafByName(QName leaf) {
114         List<SimpleNode<?>> list = getSimpleNodesByName(leaf);
115         if (list.isEmpty()) {
116             return null;
117         }
118         return list.get(0);
119     }
120
121     @Override
122     public List<CompositeNode> getCompositesByName(String children) {
123         return getCompositesByName(new QName(getNodeType(), children));
124     }
125
126     @Override
127     public List<SimpleNode<?>> getSimpleNodesByName(String children) {
128         return getSimpleNodesByName(new QName(getNodeType(), children));
129     }
130
131     /**
132      * @param value
133      */
134     protected void init() {
135         if (getValue() != null) {
136             nodeMap = NodeUtils.buildNodeMap(getValue());
137         }
138     }
139
140     @Override
141     public MutableCompositeNode asMutable() {
142         throw new IllegalAccessError("cast to mutable is not supported - " + getClass().getSimpleName());
143     }
144
145     @Override
146     public String toString() {
147         return super.toString() + ", children.size = " + (getChildren() != null ? getChildren().size() : "n/a");
148     }
149
150 }