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.impl.util.AbstractCompositeNodeBuilder;
34 import org.opendaylight.yangtools.yang.data.impl.util.CompositeNodeBuilder;
36 public final class ImmutableCompositeNode extends AbstractNodeTO<List<Node<?>>> implements //
39 AttributesContainer, //
42 private static final long serialVersionUID = 100L;
44 private Map<QName, List<Node<?>>> nodeMap = new HashMap<>();
46 private final Map<QName, String> attributes;
53 * use null to create top composite node (without parent)
56 private ImmutableCompositeNode(final QName qname, final Map<QName,String> attributes,final List<Node<?>> value) {
57 super(qname, null, ImmutableList.copyOf(value));
58 if(attributes == null) {
59 this.attributes = ImmutableMap.<QName, String>of();
61 this.attributes = ImmutableMap.copyOf(attributes);
69 * use null to create top composite node (without parent)
73 public ImmutableCompositeNode(final QName qname, final List<Node<?>> value, final ModifyAction modifyAction) {
74 super(qname, null, value, modifyAction);
75 attributes = ImmutableMap.of();
79 protected void init() {
80 if (getValue() != null) {
81 nodeMap = NodeUtils.buildNodeMap(getValue());
85 protected Map<QName, List<Node<?>>> getNodeMap() {
90 public List<Node<?>> getChildren() {
91 return Collections.unmodifiableList(getValue() == null ? new ArrayList<Node<?>>() : getValue());
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()) {
144 public Map<QName, String> getAttributes() {
149 public String getAttributeValue(final QName key) {
150 return attributes.get(key);
155 * @return TODO:: do we need this method?
157 public SimpleNode<?> getFirstLeafByName(final QName leaf) {
158 List<SimpleNode<?>> list = getSimpleNodesByName(leaf);
159 if (list.isEmpty()) {
166 public List<CompositeNode> getCompositesByName(final String children) {
167 return getCompositesByName(QName.create(getNodeType(), children));
171 public List<SimpleNode<?>> getSimpleNodesByName(final String children) {
172 return getSimpleNodesByName(QName.create(getNodeType(), children));
176 public MutableCompositeNode asMutable() {
177 throw new IllegalAccessError("cast to mutable is not supported - " + getClass().getSimpleName());
181 public String toString() {
182 return super.toString() + ", children.size = " + (getChildren() != null ? getChildren().size() : "n/a");
186 public void clear() {
191 public boolean containsKey(final Object key) {
192 return nodeMap.containsKey(key);
196 public boolean containsValue(final Object value) {
197 return nodeMap.containsValue(value);
201 public Set<java.util.Map.Entry<QName, List<Node<?>>>> entrySet() {
202 return nodeMap.entrySet();
207 return nodeMap.size();
211 public boolean isEmpty() {
212 return nodeMap.isEmpty();
216 public List<Node<?>> get(final Object key) {
217 return nodeMap.get(key);
221 public List<Node<?>> put(final QName key, final List<Node<?>> value) {
222 return nodeMap.put(key, value);
226 public List<Node<?>> remove(final Object key) {
227 return nodeMap.remove(key);
231 public void putAll(final Map<? extends QName, ? extends List<Node<?>>> m) {
236 public Set<QName> keySet() {
237 return nodeMap.keySet();
241 public Collection<List<Node<?>>> values() {
242 return nodeMap.values();
245 // Serialization related
247 private void readObject(final ObjectInputStream aStream) throws IOException, ClassNotFoundException {
248 aStream.defaultReadObject();
249 QName qName = (QName) aStream.readObject();
250 CompositeNode parent = (CompositeNode) aStream.readObject();
251 @SuppressWarnings("unchecked")
252 List<Node<?>> value = (List<Node<?>>) aStream.readObject();
253 ModifyAction modifyAction = (ModifyAction) aStream.readObject();
255 init(qName, parent, value, modifyAction);
258 private void writeObject(final ObjectOutputStream aStream) throws IOException {
259 aStream.defaultWriteObject();
260 // manually serialize superclass
261 aStream.writeObject(getQName());
262 aStream.writeObject(getParent());
263 aStream.writeObject(getValue());
264 aStream.writeObject(getModificationAction());
267 public static CompositeNodeBuilder<ImmutableCompositeNode> builder() {
268 return new ImmutableCompositeNodeBuilder();
271 private static class ImmutableCompositeNodeBuilder extends AbstractCompositeNodeBuilder<ImmutableCompositeNode> {
274 public AbstractCompositeNodeBuilder<ImmutableCompositeNode> addLeaf(final QName leafName, final Object leafValue) {
275 add(new SimpleNodeTOImpl<Object>(leafName, null, leafValue));
280 public ImmutableCompositeNode toInstance() {
281 return ImmutableCompositeNode.create(this.getQName(), this.getAttributes(), this.getChildNodes());
286 public static ImmutableCompositeNode create(final QName qName, final List<Node<?>> childNodes) {
287 return new ImmutableCompositeNode(qName, ImmutableMap.<QName, String>of(),childNodes);
290 public static ImmutableCompositeNode create(final QName qName, final Map<QName, String> attributes, final List<Node<?>> childNodes) {
291 return new ImmutableCompositeNode(qName, attributes,childNodes);
294 public static ImmutableCompositeNode create(final QName qName, final List<Node<?>> childNodes, final ModifyAction modifyAction) {
295 return new ImmutableCompositeNode(qName, childNodes, modifyAction);