Fixed incorrect test location.
[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     @Override
23     public SimpleNode<?> getFirstSimpleByName(final QName leaf) {
24         List<SimpleNode<?>> list = getSimpleNodesByName(leaf);
25         if (list.size() == 0) {
26             return null;
27         }
28         return list.get(0);
29     }
30
31     protected AbstractContainerNode(final QName name, final CompositeNode parent) {
32         super(name, parent);
33     }
34
35     public AbstractContainerNode(final QName name) {
36         super(name, null);
37     }
38
39     @Override
40     public List<Node<?>> getChildren() {
41         return getValue();
42     }
43
44     @Override
45     public List<Node<?>> getValue() {
46         Map<QName, List<Node<?>>> map = getNodeMap();
47         if (map == null) {
48             throw new IllegalStateException("nodeMap should not be null");
49         }
50         List<Node<?>> ret = new ArrayList<Node<?>>();
51         Collection<List<Node<?>>> values = map.values();
52         for (List<Node<?>> list : values) {
53             ret.addAll(list);
54         }
55         return ret;
56     }
57
58     protected abstract Map<QName, List<Node<?>>> getNodeMap();
59
60     @Override
61     public List<CompositeNode> getCompositesByName(final QName children) {
62         Map<QName, List<Node<?>>> map = getNodeMap();
63         if (map == null) {
64             throw new IllegalStateException("nodeMap should not be null");
65         }
66         List<Node<?>> toFilter = map.get(children);
67         List<CompositeNode> list = new ArrayList<CompositeNode>();
68         for (Node<?> node : toFilter) {
69             if (node instanceof CompositeNode) {
70                 list.add((CompositeNode) node);
71             }
72         }
73         return list;
74     }
75
76     @Override
77     public List<SimpleNode<?>> getSimpleNodesByName(final QName children) {
78         Map<QName, List<Node<?>>> map = getNodeMap();
79         if (map == null) {
80             throw new IllegalStateException("nodeMap should not be null");
81         }
82         List<Node<?>> toFilter = map.get(children);
83         List<SimpleNode<?>> list = new ArrayList<SimpleNode<?>>();
84
85         for (Node<?> node : toFilter) {
86             if (node instanceof SimpleNode<?>) {
87                 list.add((SimpleNode<?>) node);
88             }
89         }
90         return list;
91     }
92
93     @Override
94     public CompositeNode getFirstCompositeByName(final QName container) {
95         List<CompositeNode> list = getCompositesByName(container);
96         if (list.size() == 0) {
97             return null;
98         }
99         return list.get(0);
100     }
101
102     public SimpleNode<?> getFirstLeafByName(final QName leaf) {
103         List<SimpleNode<?>> list = getSimpleNodesByName(leaf);
104         if (list.size() == 0) {
105             return null;
106         }
107         return list.get(0);
108     }
109
110     @Override
111     public List<CompositeNode> getCompositesByName(final String children) {
112         return getCompositesByName(localQName(children));
113     }
114
115     @Override
116     public List<SimpleNode<?>> getSimpleNodesByName(final String children) {
117         return getSimpleNodesByName(localQName(children));
118     }
119
120     private QName localQName(final String str) {
121         return QName.create(getNodeType(), str);
122     }
123 }