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.impl;
10 import java.io.IOException;
11 import java.io.ObjectInputStream;
12 import java.io.ObjectOutputStream;
13 import java.io.Serializable;
14 import java.util.ArrayList;
15 import java.util.Collection;
16 import java.util.Collections;
17 import java.util.HashMap;
18 import java.util.List;
22 import org.opendaylight.yangtools.yang.common.QName;
23 import org.opendaylight.yangtools.yang.data.api.CompositeNode;
24 import org.opendaylight.yangtools.yang.data.api.ModifyAction;
25 import org.opendaylight.yangtools.yang.data.api.MutableCompositeNode;
26 import org.opendaylight.yangtools.yang.data.api.Node;
27 import org.opendaylight.yangtools.yang.data.api.SimpleNode;
30 * @author michal.rehak
33 public class CompositeNodeTOImpl extends AbstractNodeTO<List<Node<?>>> implements CompositeNode, Serializable {
35 private static final long serialVersionUID = 100L;
37 private Map<QName, List<Node<?>>> nodeMap = new HashMap<>();
42 * use null to create top composite node (without parent)
45 public CompositeNodeTOImpl(final QName qname, final CompositeNode parent, final List<Node<?>> value) {
46 super(qname, parent, value);
53 * use null to create top composite node (without parent)
57 public CompositeNodeTOImpl(final QName qname, final CompositeNode parent, final List<Node<?>> value, final ModifyAction modifyAction) {
58 super(qname, parent, value, modifyAction);
62 protected void init() {
63 if (getValue() != null) {
64 nodeMap = NodeUtils.buildNodeMap(getValue());
68 protected Map<QName, List<Node<?>>> getNodeMap() {
73 public List<Node<?>> getChildren() {
74 return Collections.unmodifiableList(getValue() == null ? new ArrayList<Node<?>>() : getValue());
78 public SimpleNode<?> getFirstSimpleByName(final QName leafQName) {
79 List<SimpleNode<?>> list = getSimpleNodesByName(leafQName);
87 public List<CompositeNode> getCompositesByName(final QName children) {
88 List<Node<?>> toFilter = getNodeMap().get(children);
89 if(toFilter == null) {
90 return Collections.emptyList();
92 List<CompositeNode> list = new ArrayList<CompositeNode>();
93 for (Node<?> node : toFilter) {
94 if (node instanceof CompositeNode) {
95 list.add((CompositeNode) node);
102 public List<SimpleNode<?>> getSimpleNodesByName(final QName children) {
103 List<Node<?>> toFilter = getNodeMap().get(children);
104 if(toFilter == null) {
105 return Collections.emptyList();
107 List<SimpleNode<?>> list = new ArrayList<SimpleNode<?>>();
109 for (Node<?> node : toFilter) {
110 if (node instanceof SimpleNode<?>) {
111 list.add((SimpleNode<?>) node);
118 public CompositeNode getFirstCompositeByName(final QName container) {
119 List<CompositeNode> list = getCompositesByName(container);
120 if (list.isEmpty()) {
128 * @return TODO:: do we need this method?
130 public SimpleNode<?> getFirstLeafByName(final QName leaf) {
131 List<SimpleNode<?>> list = getSimpleNodesByName(leaf);
132 if (list.isEmpty()) {
139 public List<CompositeNode> getCompositesByName(final String children) {
140 return getCompositesByName(new QName(getNodeType(), children));
144 public List<SimpleNode<?>> getSimpleNodesByName(final String children) {
145 return getSimpleNodesByName(new QName(getNodeType(), children));
149 public MutableCompositeNode asMutable() {
150 throw new IllegalAccessError("cast to mutable is not supported - " + getClass().getSimpleName());
154 public String toString() {
155 return super.toString() + ", children.size = " + (getChildren() != null ? getChildren().size() : "n/a");
159 public void clear() {
164 public boolean containsKey(final Object key) {
165 return nodeMap.containsKey(key);
169 public boolean containsValue(final Object value) {
170 return nodeMap.containsValue(value);
174 public Set<java.util.Map.Entry<QName, List<Node<?>>>> entrySet() {
175 return nodeMap.entrySet();
180 return nodeMap.size();
184 public boolean isEmpty() {
185 return nodeMap.isEmpty();
189 public List<Node<?>> get(final Object key) {
190 return nodeMap.get(key);
194 public List<Node<?>> put(final QName key, final List<Node<?>> value) {
195 return nodeMap.put(key, value);
199 public List<Node<?>> remove(final Object key) {
200 return nodeMap.remove(key);
204 public void putAll(final Map<? extends QName, ? extends List<Node<?>>> m) {
209 public Set<QName> keySet() {
210 return nodeMap.keySet();
214 public Collection<List<Node<?>>> values() {
215 return nodeMap.values();
218 // Serialization related
220 private void readObject(final ObjectInputStream aStream) throws IOException, ClassNotFoundException {
221 aStream.defaultReadObject();
222 QName qName = (QName)aStream.readObject();
223 CompositeNode parent = (CompositeNode) aStream.readObject();
224 @SuppressWarnings("unchecked")
225 List<Node<?>> value = (List<Node<?>>) aStream.readObject();
226 ModifyAction modifyAction = (ModifyAction) aStream.readObject();
228 init(qName, parent, value, modifyAction);
231 private void writeObject(final ObjectOutputStream aStream) throws IOException {
232 aStream.defaultWriteObject();
233 //manually serialize superclass
234 aStream.writeObject(getQName());
235 aStream.writeObject(getParent());
236 aStream.writeObject(getValue());
237 aStream.writeObject(getModificationAction());