2 * Copyright (c) 2013 Cisco Systems, Inc. and others. All rights reserved.
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
8 package org.opendaylight.yangtools.yang.data.util;
10 import java.util.ArrayList;
11 import java.util.Collection;
12 import java.util.List;
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;
20 public abstract class AbstractContainerNode extends AbstractNode<List<Node<?>>> implements CompositeNode {
22 public SimpleNode<?> getFirstSimpleByName(QName leaf) {
23 List<SimpleNode<?>> list = getSimpleNodesByName(leaf);
24 if (list.size() == 0) {
30 protected AbstractContainerNode(QName name, CompositeNode parent) {
34 public AbstractContainerNode(QName name) {
38 public List<Node<?>> getChildren() {
42 public List<Node<?>> getValue() {
43 Map<QName, List<Node<?>>> map = getNodeMap();
45 throw new IllegalStateException("nodeMap should not be null");
47 List<Node<?>> ret = new ArrayList<Node<?>>();
48 Collection<List<Node<?>>> values = map.values();
49 for (List<Node<?>> list : values) {
55 protected abstract Map<QName, List<Node<?>>> getNodeMap();
57 public List<CompositeNode> getCompositesByName(QName children) {
58 Map<QName, List<Node<?>>> map = getNodeMap();
60 throw new IllegalStateException("nodeMap should not be null");
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);
72 public List<SimpleNode<?>> getSimpleNodesByName(QName children) {
73 Map<QName, List<Node<?>>> map = getNodeMap();
75 throw new IllegalStateException("nodeMap should not be null");
77 List<Node<?>> toFilter = map.get(children);
78 List<SimpleNode<?>> list = new ArrayList<SimpleNode<?>>();
80 for (Node<?> node : toFilter) {
81 if (node instanceof SimpleNode<?>) {
82 list.add((SimpleNode<?>) node);
88 public CompositeNode getFirstCompositeByName(QName container) {
89 List<CompositeNode> list = getCompositesByName(container);
90 if (list.size() == 0) {
96 public SimpleNode<?> getFirstLeafByName(QName leaf) {
97 List<SimpleNode<?>> list = getSimpleNodesByName(leaf);
98 if (list.size() == 0) {
104 public List<CompositeNode> getCompositesByName(String children) {
105 return getCompositesByName(localQName(children));
108 public List<SimpleNode<?>> getSimpleNodesByName(String children) {
109 return getSimpleNodesByName(localQName(children));
112 private QName localQName(String str) {
113 return new QName(getNodeType(), str);