Fixing sonar issues 3
[yangtools.git] / yang / yang-data-util / src / main / java / org / opendaylight / yangtools / yang / data / util / AbstractContainerNode.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.util;
9
10 import java.util.ArrayList;
11 import java.util.Collection;
12 import java.util.List;
13 import java.util.Map;
14
15 import org.opendaylight.yangtools.yang.common.QName;
16 import org.opendaylight.yangtools.yang.data.api.CompositeNode;
17 import org.opendaylight.yangtools.yang.data.api.Node;
18 import org.opendaylight.yangtools.yang.data.api.SimpleNode;
19
20 public abstract class AbstractContainerNode extends AbstractNode<List<Node<?>>> implements CompositeNode {
21
22     public SimpleNode<?> getFirstSimpleByName(QName leaf) {
23         List<SimpleNode<?>> list = getSimpleNodesByName(leaf);
24         if (list.size() == 0) {
25             return null;
26         }
27         return list.get(0);
28     }
29
30     protected AbstractContainerNode(QName name, CompositeNode parent) {
31         super(name, parent);
32     }
33
34     public AbstractContainerNode(QName name) {
35         super(name, null);
36     }
37
38     public List<Node<?>> getChildren() {
39         return getValue();
40     }
41
42     public List<Node<?>> getValue() {
43         Map<QName, List<Node<?>>> map = getNodeMap();
44         if (map == null) {
45             throw new IllegalStateException("nodeMap should not be null");
46         }
47         List<Node<?>> ret = new ArrayList<Node<?>>();
48         Collection<List<Node<?>>> values = map.values();
49         for (List<Node<?>> list : values) {
50             ret.addAll(list);
51         }
52         return ret;
53     }
54
55     protected abstract Map<QName, List<Node<?>>> getNodeMap();
56
57     public List<CompositeNode> getCompositesByName(QName children) {
58         Map<QName, List<Node<?>>> map = getNodeMap();
59         if (map == null) {
60             throw new IllegalStateException("nodeMap should not be null");
61         }
62         List<Node<?>> toFilter = map.get(children);
63         List<CompositeNode> list = new ArrayList<CompositeNode>();
64         for (Node<?> node : toFilter) {
65             if (node instanceof CompositeNode) {
66                 list.add((CompositeNode) node);
67             }
68         }
69         return list;
70     }
71
72     public List<SimpleNode<?>> getSimpleNodesByName(QName children) {
73         Map<QName, List<Node<?>>> map = getNodeMap();
74         if (map == null) {
75             throw new IllegalStateException("nodeMap should not be null");
76         }
77         List<Node<?>> toFilter = map.get(children);
78         List<SimpleNode<?>> list = new ArrayList<SimpleNode<?>>();
79
80         for (Node<?> node : toFilter) {
81             if (node instanceof SimpleNode<?>) {
82                 list.add((SimpleNode<?>) node);
83             }
84         }
85         return list;
86     }
87
88     public CompositeNode getFirstCompositeByName(QName container) {
89         List<CompositeNode> list = getCompositesByName(container);
90         if (list.size() == 0) {
91             return null;
92         }
93         return list.get(0);
94     }
95
96     public SimpleNode<?> getFirstLeafByName(QName leaf) {
97         List<SimpleNode<?>> list = getSimpleNodesByName(leaf);
98         if (list.size() == 0) {
99             return null;
100         }
101         return list.get(0);
102     }
103
104     public List<CompositeNode> getCompositesByName(String children) {
105         return getCompositesByName(localQName(children));
106     }
107
108     public List<SimpleNode<?>> getSimpleNodesByName(String children) {
109         return getSimpleNodesByName(localQName(children));
110     }
111
112     private QName localQName(String str) {
113         return new QName(getNodeType(), str);
114     }
115 }