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 com.google.common.collect.ImmutableList;
11 import com.google.common.collect.ImmutableMap;
13 import java.io.IOException;
14 import java.io.ObjectInputStream;
15 import java.io.ObjectOutputStream;
16 import java.io.Serializable;
17 import java.util.ArrayList;
18 import java.util.Collection;
19 import java.util.Collections;
20 import java.util.HashMap;
21 import java.util.List;
25 import org.opendaylight.yangtools.concepts.Immutable;
26 import org.opendaylight.yangtools.yang.common.QName;
27 import org.opendaylight.yangtools.yang.data.api.AttributesContainer;
28 import org.opendaylight.yangtools.yang.data.api.CompositeNode;
29 import org.opendaylight.yangtools.yang.data.api.ModifyAction;
30 import org.opendaylight.yangtools.yang.data.api.MutableCompositeNode;
31 import org.opendaylight.yangtools.yang.data.api.Node;
32 import org.opendaylight.yangtools.yang.data.api.SimpleNode;
33 import org.opendaylight.yangtools.yang.data.api.schema.NormalizedNodeContainer;
34 import org.opendaylight.yangtools.yang.data.impl.util.AbstractCompositeNodeBuilder;
35 import org.opendaylight.yangtools.yang.data.impl.util.CompositeNodeBuilder;
38 * @deprecated Use one of the {@link NormalizedNodeContainer} implementations.
41 public final class ImmutableCompositeNode extends AbstractNodeTO<List<Node<?>>> implements Immutable, CompositeNode, AttributesContainer, Serializable {
43 private static final long serialVersionUID = 100L;
45 private Map<QName, List<Node<?>>> nodeMap = new HashMap<>();
47 private final Map<QName, String> attributes;
54 * use null to create top composite node (without parent)
57 private ImmutableCompositeNode(final QName qname, final Map<QName,String> attributes,final List<Node<?>> value) {
58 super(qname, null, ImmutableList.copyOf(value));
59 if(attributes == null) {
60 this.attributes = ImmutableMap.<QName, String>of();
62 this.attributes = ImmutableMap.copyOf(attributes);
70 * use null to create top composite node (without parent)
74 public ImmutableCompositeNode(final QName qname, final List<Node<?>> value, final ModifyAction modifyAction) {
75 super(qname, null, value, modifyAction);
76 attributes = ImmutableMap.of();
80 protected void init() {
81 if (getValue() != null) {
82 nodeMap = NodeUtils.buildNodeMap(getValue());
86 protected Map<QName, List<Node<?>>> getNodeMap() {
91 public List<Node<?>> getChildren() {
92 return Collections.unmodifiableList(getValue() == null ? new ArrayList<Node<?>>() : getValue());
96 public SimpleNode<?> getFirstSimpleByName(final QName leafQName) {
97 List<SimpleNode<?>> list = getSimpleNodesByName(leafQName);
105 public List<CompositeNode> getCompositesByName(final QName children) {
106 List<Node<?>> toFilter = getNodeMap().get(children);
107 if (toFilter == null) {
108 return Collections.emptyList();
110 List<CompositeNode> list = new ArrayList<CompositeNode>();
111 for (Node<?> node : toFilter) {
112 if (node instanceof CompositeNode) {
113 list.add((CompositeNode) node);
120 public List<SimpleNode<?>> getSimpleNodesByName(final QName children) {
121 List<Node<?>> toFilter = getNodeMap().get(children);
122 if (toFilter == null) {
123 return Collections.emptyList();
125 List<SimpleNode<?>> list = new ArrayList<SimpleNode<?>>();
127 for (Node<?> node : toFilter) {
128 if (node instanceof SimpleNode<?>) {
129 list.add((SimpleNode<?>) node);
136 public CompositeNode getFirstCompositeByName(final QName container) {
137 List<CompositeNode> list = getCompositesByName(container);
138 if (list.isEmpty()) {
145 public Map<QName, String> getAttributes() {
150 public String getAttributeValue(final QName key) {
151 return attributes.get(key);
156 * @return TODO:: do we need this method?
158 public SimpleNode<?> getFirstLeafByName(final QName leaf) {
159 List<SimpleNode<?>> list = getSimpleNodesByName(leaf);
160 if (list.isEmpty()) {
167 public List<CompositeNode> getCompositesByName(final String children) {
168 return getCompositesByName(QName.create(getNodeType(), children));
172 public List<SimpleNode<?>> getSimpleNodesByName(final String children) {
173 return getSimpleNodesByName(QName.create(getNodeType(), children));
177 public MutableCompositeNode asMutable() {
178 throw new IllegalAccessError("cast to mutable is not supported - " + getClass().getSimpleName());
182 public String toString() {
183 return super.toString() + ", children.size = " + (getChildren() != null ? getChildren().size() : "n/a");
187 public void clear() {
192 public boolean containsKey(final Object key) {
193 return nodeMap.containsKey(key);
197 public boolean containsValue(final Object value) {
198 return nodeMap.containsValue(value);
202 public Set<java.util.Map.Entry<QName, List<Node<?>>>> entrySet() {
203 return nodeMap.entrySet();
208 return nodeMap.size();
212 public boolean isEmpty() {
213 return nodeMap.isEmpty();
217 public List<Node<?>> get(final Object key) {
218 return nodeMap.get(key);
222 public List<Node<?>> put(final QName key, final List<Node<?>> value) {
223 return nodeMap.put(key, value);
227 public List<Node<?>> remove(final Object key) {
228 return nodeMap.remove(key);
232 public void putAll(final Map<? extends QName, ? extends List<Node<?>>> m) {
237 public Set<QName> keySet() {
238 return nodeMap.keySet();
242 public Collection<List<Node<?>>> values() {
243 return nodeMap.values();
246 // Serialization related
248 private void readObject(final ObjectInputStream aStream) throws IOException, ClassNotFoundException {
249 aStream.defaultReadObject();
250 QName qName = (QName) aStream.readObject();
251 CompositeNode parent = (CompositeNode) aStream.readObject();
252 @SuppressWarnings("unchecked")
253 List<Node<?>> value = (List<Node<?>>) aStream.readObject();
254 ModifyAction modifyAction = (ModifyAction) aStream.readObject();
256 init(qName, parent, value, modifyAction);
259 private void writeObject(final ObjectOutputStream aStream) throws IOException {
260 aStream.defaultWriteObject();
261 // manually serialize superclass
262 aStream.writeObject(getQName());
263 aStream.writeObject(getParent());
264 aStream.writeObject(getValue());
265 aStream.writeObject(getModificationAction());
268 public static CompositeNodeBuilder<ImmutableCompositeNode> builder() {
269 return new ImmutableCompositeNodeBuilder();
272 private static class ImmutableCompositeNodeBuilder extends AbstractCompositeNodeBuilder<ImmutableCompositeNode> {
275 public AbstractCompositeNodeBuilder<ImmutableCompositeNode> addLeaf(final QName leafName, final Object leafValue) {
276 add(new SimpleNodeTOImpl<Object>(leafName, null, leafValue));
281 public ImmutableCompositeNode toInstance() {
282 return ImmutableCompositeNode.create(this.getQName(), this.getAttributes(), this.getChildNodes());
287 public static ImmutableCompositeNode create(final QName qName, final List<Node<?>> childNodes) {
288 return new ImmutableCompositeNode(qName, ImmutableMap.<QName, String>of(),childNodes);
291 public static ImmutableCompositeNode create(final QName qName, final Map<QName, String> attributes, final List<Node<?>> childNodes) {
292 return new ImmutableCompositeNode(qName, attributes,childNodes);
295 public static ImmutableCompositeNode create(final QName qName, final List<Node<?>> childNodes, final ModifyAction modifyAction) {
296 return new ImmutableCompositeNode(qName, childNodes, modifyAction);