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 MutableCompositeNodeTOImpl extends AbstractNodeTO<List<Node<?>>> implements MutableCompositeNode, Serializable {
35 private static final long serialVersionUID = 100L;
37 private Map<QName, List<Node<?>>> nodeMap = new HashMap<>();
38 private CompositeNode original;
40 public MutableCompositeNodeTOImpl(final QName qname, final CompositeNode parent, final List<Node<?>> value, final ModifyAction modifyAction) {
41 super(qname, parent, value, modifyAction);
46 * update nodeMap - it should be invoked after children was changed
50 if (!getChildren().isEmpty()) {
51 nodeMap = NodeUtils.buildNodeMap(getChildren());
56 public List<Node<?>> getChildren() {
57 return getValue() == null ? new ArrayList<Node<?>>() : getValue();
61 public List<Node<?>> setValue(final List<Node<?>> value) {
62 List<Node<?>> oldVal = super.setValue(value);
68 public void setModifyAction(final ModifyAction action) {
69 super.setModificationAction(action);
72 protected Map<QName, List<Node<?>>> getNodeMap() {
77 public MutableCompositeNode asMutable() {
82 public CompositeNode getOriginal() {
90 public void setOriginal(final CompositeNode original) {
91 this.original = original;
95 public SimpleNode<?> getFirstSimpleByName(final QName leafQName) {
96 List<SimpleNode<?>> list = getSimpleNodesByName(leafQName);
104 public List<CompositeNode> getCompositesByName(final QName children) {
105 List<Node<?>> toFilter = getNodeMap().get(children);
106 if (toFilter == null) {
107 return Collections.emptyList();
109 List<CompositeNode> list = new ArrayList<CompositeNode>();
110 for (Node<?> node : toFilter) {
111 if (node instanceof CompositeNode) {
112 list.add((CompositeNode) node);
119 public List<SimpleNode<?>> getSimpleNodesByName(final QName children) {
120 List<Node<?>> toFilter = getNodeMap().get(children);
121 if (toFilter == null) {
122 return Collections.emptyList();
124 List<SimpleNode<?>> list = new ArrayList<SimpleNode<?>>();
126 for (Node<?> node : toFilter) {
127 if (node instanceof SimpleNode<?>) {
128 list.add((SimpleNode<?>) node);
135 public CompositeNode getFirstCompositeByName(final QName container) {
136 List<CompositeNode> list = getCompositesByName(container);
137 if (list.isEmpty()) {
145 * @return TODO:: do we need this method?
147 public SimpleNode<?> getFirstLeafByName(final QName leaf) {
148 List<SimpleNode<?>> list = getSimpleNodesByName(leaf);
149 if (list.isEmpty()) {
156 public List<CompositeNode> getCompositesByName(final String children) {
157 return getCompositesByName(new QName(getNodeType(), children));
161 public List<SimpleNode<?>> getSimpleNodesByName(final String children) {
162 return getSimpleNodesByName(new QName(getNodeType(), children));
166 public String toString() {
167 return super.toString() + ", children.size = " + (getChildren() != null ? getChildren().size() : "n/a");
171 public void clear() {
176 public boolean containsKey(final Object key) {
177 return nodeMap.containsKey(key);
181 public boolean containsValue(final Object value) {
182 return nodeMap.containsValue(value);
186 public Set<java.util.Map.Entry<QName, List<Node<?>>>> entrySet() {
187 return nodeMap.entrySet();
192 return nodeMap.size();
196 public boolean isEmpty() {
197 return nodeMap.isEmpty();
201 public List<Node<?>> get(final Object key) {
202 return nodeMap.get(key);
206 public List<Node<?>> put(final QName key, final List<Node<?>> value) {
207 return nodeMap.put(key, value);
211 public List<Node<?>> remove(final Object key) {
212 return nodeMap.remove(key);
216 public void putAll(final Map<? extends QName, ? extends List<Node<?>>> m) {
221 public Set<QName> keySet() {
222 return nodeMap.keySet();
226 public Collection<List<Node<?>>> values() {
227 return nodeMap.values();
230 // Serialization related
232 private void readObject(final ObjectInputStream aStream) throws IOException, ClassNotFoundException {
233 aStream.defaultReadObject();
234 QName qName = (QName)aStream.readObject();
235 CompositeNode parent = (CompositeNode) aStream.readObject();
236 @SuppressWarnings("unchecked")
237 List<Node<?>> value = (List<Node<?>>) aStream.readObject();
238 ModifyAction modifyAction = (ModifyAction) aStream.readObject();
240 init(qName, parent, value, modifyAction);
243 private void writeObject(final ObjectOutputStream aStream) throws IOException {
244 aStream.defaultWriteObject();
245 //manually serialize superclass
246 aStream.writeObject(getQName());
247 aStream.writeObject(getParent());
248 aStream.writeObject(getValue());
249 aStream.writeObject(getModificationAction());