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 {
23 public SimpleNode<?> getFirstSimpleByName(final QName leaf) {
24 List<SimpleNode<?>> list = getSimpleNodesByName(leaf);
25 if (list.size() == 0) {
31 protected AbstractContainerNode(final QName name, final CompositeNode parent) {
35 public AbstractContainerNode(final QName name) {
40 public List<Node<?>> getChildren() {
45 public List<Node<?>> getValue() {
46 Map<QName, List<Node<?>>> map = getNodeMap();
48 throw new IllegalStateException("nodeMap should not be null");
50 List<Node<?>> ret = new ArrayList<Node<?>>();
51 Collection<List<Node<?>>> values = map.values();
52 for (List<Node<?>> list : values) {
58 protected abstract Map<QName, List<Node<?>>> getNodeMap();
61 public List<CompositeNode> getCompositesByName(final QName children) {
62 Map<QName, List<Node<?>>> map = getNodeMap();
64 throw new IllegalStateException("nodeMap should not be null");
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);
77 public List<SimpleNode<?>> getSimpleNodesByName(final QName children) {
78 Map<QName, List<Node<?>>> map = getNodeMap();
80 throw new IllegalStateException("nodeMap should not be null");
82 List<Node<?>> toFilter = map.get(children);
83 List<SimpleNode<?>> list = new ArrayList<SimpleNode<?>>();
85 for (Node<?> node : toFilter) {
86 if (node instanceof SimpleNode<?>) {
87 list.add((SimpleNode<?>) node);
94 public CompositeNode getFirstCompositeByName(final QName container) {
95 List<CompositeNode> list = getCompositesByName(container);
96 if (list.size() == 0) {
102 public SimpleNode<?> getFirstLeafByName(final QName leaf) {
103 List<SimpleNode<?>> list = getSimpleNodesByName(leaf);
104 if (list.size() == 0) {
111 public List<CompositeNode> getCompositesByName(final String children) {
112 return getCompositesByName(localQName(children));
116 public List<SimpleNode<?>> getSimpleNodesByName(final String children) {
117 return getSimpleNodesByName(localQName(children));
120 private QName localQName(final String str) {
121 return QName.create(getNodeType(), str);