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 org.opendaylight.yangtools.yang.common.QName;
11 import org.opendaylight.yangtools.yang.data.api.CompositeNode;
12 import org.opendaylight.yangtools.yang.data.api.ModifyAction;
13 import org.opendaylight.yangtools.yang.data.api.MutableCompositeNode;
14 import org.opendaylight.yangtools.yang.data.api.Node;
15 import org.opendaylight.yangtools.yang.data.api.SimpleNode;
17 import java.io.IOException;
18 import java.io.ObjectInputStream;
19 import java.io.ObjectOutputStream;
20 import java.io.Serializable;
21 import java.util.ArrayList;
22 import java.util.Collection;
23 import java.util.Collections;
24 import java.util.HashMap;
25 import java.util.List;
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(QName qname, CompositeNode parent, List<Node<?>> value, 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(List<Node<?>> value) {
62 List<Node<?>> oldVal = super.setValue(value);
68 public void setModifyAction(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(CompositeNode original) {
91 this.original = original;
95 public SimpleNode<?> getFirstSimpleByName(QName leafQName) {
96 List<SimpleNode<?>> list = getSimpleNodesByName(leafQName);
104 public List<CompositeNode> getCompositesByName(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(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(QName container) {
136 List<CompositeNode> list = getCompositesByName(container);
137 if (list.isEmpty()) {
145 * @return TODO:: do we need this method?
147 public SimpleNode<?> getFirstLeafByName(QName leaf) {
148 List<SimpleNode<?>> list = getSimpleNodesByName(leaf);
149 if (list.isEmpty()) {
156 public List<CompositeNode> getCompositesByName(String children) {
157 return getCompositesByName(new QName(getNodeType(), children));
161 public List<SimpleNode<?>> getSimpleNodesByName(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(Object key) {
177 return nodeMap.containsKey(key);
181 public boolean containsValue(Object value) {
182 return nodeMap.containsValue(value);
186 public Set<java.util.Map.Entry<QName, List<Node<?>>>> entrySet() {
187 return nodeMap.entrySet();
191 public boolean equals(Object obj) {
192 return super.equals(obj);
197 return nodeMap.size();
201 public boolean isEmpty() {
202 return nodeMap.isEmpty();
206 public List<Node<?>> get(Object key) {
207 return nodeMap.get(key);
211 public List<Node<?>> put(QName key, List<Node<?>> value) {
212 return nodeMap.put(key, value);
216 public List<Node<?>> remove(Object key) {
217 return nodeMap.remove(key);
221 public void putAll(Map<? extends QName, ? extends List<Node<?>>> m) {
226 public Set<QName> keySet() {
227 return nodeMap.keySet();
231 public Collection<List<Node<?>>> values() {
232 return nodeMap.values();
235 // Serialization related
237 private void readObject(ObjectInputStream aStream) throws IOException, ClassNotFoundException {
238 aStream.defaultReadObject();
239 QName qName = (QName)aStream.readObject();
240 CompositeNode parent = (CompositeNode) aStream.readObject();
241 List<Node<?>> value = (List<Node<?>>) aStream.readObject();
242 ModifyAction modifyAction = (ModifyAction) aStream.readObject();
244 init(qName, parent, value, modifyAction);
247 private void writeObject(ObjectOutputStream aStream) throws IOException {
248 aStream.defaultWriteObject();
249 //manually serialize superclass
250 aStream.writeObject(getQName());
251 aStream.writeObject(getParent());
252 aStream.writeObject(getValue());
253 aStream.writeObject(getModificationAction());