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