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;
28 import org.opendaylight.yangtools.yang.data.api.schema.NormalizedNodeContainer;
31 * @author michal.rehak
33 * @deprecated Use one of the {@link NormalizedNodeContainer} implementations instead.
36 public class MutableCompositeNodeTOImpl extends AbstractNodeTO<List<Node<?>>> implements MutableCompositeNode, Serializable {
38 private static final long serialVersionUID = 100L;
40 private Map<QName, List<Node<?>>> nodeMap = new HashMap<>();
41 private CompositeNode original;
43 public MutableCompositeNodeTOImpl(final QName qname, final CompositeNode parent, final List<Node<?>> value, final ModifyAction modifyAction) {
44 super(qname, parent, value, modifyAction);
49 * update nodeMap - it should be invoked after children was changed
53 if (!getChildren().isEmpty()) {
54 nodeMap = NodeUtils.buildNodeMap(getChildren());
59 public List<Node<?>> getChildren() {
60 return getValue() == null ? new ArrayList<Node<?>>() : getValue();
64 public List<Node<?>> setValue(final List<Node<?>> value) {
65 List<Node<?>> oldVal = super.setValue(value);
71 public void setModifyAction(final ModifyAction action) {
72 super.setModificationAction(action);
75 protected Map<QName, List<Node<?>>> getNodeMap() {
80 public MutableCompositeNode asMutable() {
85 public CompositeNode getOriginal() {
93 public void setOriginal(final CompositeNode original) {
94 this.original = original;
98 public SimpleNode<?> getFirstSimpleByName(final QName leafQName) {
99 List<SimpleNode<?>> list = getSimpleNodesByName(leafQName);
100 if (list.isEmpty()) {
107 public List<CompositeNode> getCompositesByName(final QName children) {
108 List<Node<?>> toFilter = getNodeMap().get(children);
109 if (toFilter == null) {
110 return Collections.emptyList();
112 List<CompositeNode> list = new ArrayList<CompositeNode>();
113 for (Node<?> node : toFilter) {
114 if (node instanceof CompositeNode) {
115 list.add((CompositeNode) node);
122 public List<SimpleNode<?>> getSimpleNodesByName(final QName children) {
123 List<Node<?>> toFilter = getNodeMap().get(children);
124 if (toFilter == null) {
125 return Collections.emptyList();
127 List<SimpleNode<?>> list = new ArrayList<SimpleNode<?>>();
129 for (Node<?> node : toFilter) {
130 if (node instanceof SimpleNode<?>) {
131 list.add((SimpleNode<?>) node);
138 public CompositeNode getFirstCompositeByName(final QName container) {
139 List<CompositeNode> list = getCompositesByName(container);
140 if (list.isEmpty()) {
148 * @return TODO:: do we need this method?
150 public SimpleNode<?> getFirstLeafByName(final QName leaf) {
151 List<SimpleNode<?>> list = getSimpleNodesByName(leaf);
152 if (list.isEmpty()) {
159 public List<CompositeNode> getCompositesByName(final String children) {
160 return getCompositesByName(QName.create(getNodeType(), children));
164 public List<SimpleNode<?>> getSimpleNodesByName(final String children) {
165 return getSimpleNodesByName(QName.create(getNodeType(), children));
169 public String toString() {
170 return super.toString() + ", children.size = " + (getChildren() != null ? getChildren().size() : "n/a");
174 public void clear() {
179 public boolean containsKey(final Object key) {
180 return nodeMap.containsKey(key);
184 public boolean containsValue(final Object value) {
185 return nodeMap.containsValue(value);
189 public Set<java.util.Map.Entry<QName, List<Node<?>>>> entrySet() {
190 return nodeMap.entrySet();
195 return nodeMap.size();
199 public boolean isEmpty() {
200 return nodeMap.isEmpty();
204 public List<Node<?>> get(final Object key) {
205 return nodeMap.get(key);
209 public List<Node<?>> put(final QName key, final List<Node<?>> value) {
210 return nodeMap.put(key, value);
214 public List<Node<?>> remove(final Object key) {
215 return nodeMap.remove(key);
219 public void putAll(final Map<? extends QName, ? extends List<Node<?>>> m) {
224 public Set<QName> keySet() {
225 return nodeMap.keySet();
229 public Collection<List<Node<?>>> values() {
230 return nodeMap.values();
233 // Serialization related
235 private void readObject(final ObjectInputStream aStream) throws IOException, ClassNotFoundException {
236 aStream.defaultReadObject();
237 QName qName = (QName)aStream.readObject();
238 CompositeNode parent = (CompositeNode) aStream.readObject();
239 @SuppressWarnings("unchecked")
240 List<Node<?>> value = (List<Node<?>>) aStream.readObject();
241 ModifyAction modifyAction = (ModifyAction) aStream.readObject();
243 init(qName, parent, value, modifyAction);
246 private void writeObject(final ObjectOutputStream aStream) throws IOException {
247 aStream.defaultWriteObject();
248 //manually serialize superclass
249 aStream.writeObject(getQName());
250 aStream.writeObject(getParent());
251 aStream.writeObject(getValue());
252 aStream.writeObject(getModificationAction());