/* * Copyright (c) 2013 Cisco Systems, Inc. and others. All rights reserved. * * This program and the accompanying materials are made available under the * terms of the Eclipse Public License v1.0 which accompanies this distribution, * and is available at http://www.eclipse.org/legal/epl-v10.html */ package org.opendaylight.controller.yang.data.util; import java.util.ArrayList; import java.util.Collection; import java.util.List; import java.util.Map; import org.opendaylight.controller.yang.common.QName; import org.opendaylight.controller.yang.data.api.CompositeNode; import org.opendaylight.controller.yang.data.api.Node; import org.opendaylight.controller.yang.data.api.SimpleNode; public abstract class AbstractContainerNode extends AbstractNode>> implements CompositeNode { public SimpleNode getFirstSimpleByName(QName leaf) { List> list = getSimpleNodesByName(leaf); if (list.size() == 0) return null; return list.get(0); } protected AbstractContainerNode(QName name, CompositeNode parent) { super(name, parent); } public AbstractContainerNode(QName name) { super(name, null); } public List> getChildren() { return getValue(); } public List> getValue() { Map>> map = getNodeMap(); if (map == null) throw new IllegalStateException("nodeMap should not be null"); List> ret = new ArrayList>(); Collection>> values = map.values(); for (List> list : values) { ret.addAll(list); } return ret; } protected abstract Map>> getNodeMap(); public List getCompositesByName(QName children) { Map>> map = getNodeMap(); if (map == null) throw new IllegalStateException("nodeMap should not be null"); List> toFilter = map.get(children); List list = new ArrayList(); for (Node node : toFilter) { if (node instanceof CompositeNode) list.add((CompositeNode) node); } return list; } public List> getSimpleNodesByName(QName children) { Map>> map = getNodeMap(); if (map == null) throw new IllegalStateException("nodeMap should not be null"); List> toFilter = map.get(children); List> list = new ArrayList>(); for (Node node : toFilter) { if (node instanceof SimpleNode) list.add((SimpleNode) node); } return list; } public CompositeNode getFirstCompositeByName(QName container) { List list = getCompositesByName(container); if (list.size() == 0) return null; return list.get(0); } public SimpleNode getFirstLeafByName(QName leaf) { List> list = getSimpleNodesByName(leaf); if (list.size() == 0) return null; return list.get(0); } public List getCompositesByName(String children) { return getCompositesByName(localQName(children)); } public List> getSimpleNodesByName(String children) { return getSimpleNodesByName(localQName(children)); } private QName localQName(String str) { return new QName(getNodeType(), str); } }