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.concepts.Immutable;
11 import org.opendaylight.yangtools.yang.common.QName;
12 import org.opendaylight.yangtools.yang.data.api.AttributesContainer;
13 import org.opendaylight.yangtools.yang.data.api.CompositeNode;
14 import org.opendaylight.yangtools.yang.data.api.ModifyAction;
15 import org.opendaylight.yangtools.yang.data.api.MutableCompositeNode;
16 import org.opendaylight.yangtools.yang.data.api.Node;
17 import org.opendaylight.yangtools.yang.data.api.SimpleNode;
18 import org.opendaylight.yangtools.yang.data.impl.util.AbstractCompositeNodeBuilder;
19 import org.opendaylight.yangtools.yang.data.impl.util.CompositeNodeBuilder;
20 import com.google.common.collect.ImmutableList;
21 import com.google.common.collect.ImmutableMap;
23 import java.io.IOException;
24 import java.io.ObjectInputStream;
25 import java.io.ObjectOutputStream;
26 import java.io.Serializable;
27 import java.util.ArrayList;
28 import java.util.Collection;
29 import java.util.Collections;
30 import java.util.HashMap;
31 import java.util.List;
35 public final class ImmutableCompositeNode extends AbstractNodeTO<List<Node<?>>> implements //
38 AttributesContainer, //
41 private static final long serialVersionUID = 100L;
43 private Map<QName, List<Node<?>>> nodeMap = new HashMap<>();
45 private final Map<QName, String> attributes;
52 * use null to create top composite node (without parent)
55 private ImmutableCompositeNode(QName qname, Map<QName,String> attributes,List<Node<?>> value) {
56 super(qname, null, ImmutableList.copyOf(value));
57 if(attributes == null) {
58 this.attributes = ImmutableMap.<QName, String>of();
60 this.attributes = ImmutableMap.copyOf(attributes);
68 * use null to create top composite node (without parent)
71 private ImmutableCompositeNode(QName qname, List<Node<?>> value, QName a1, String av) {
72 super(qname, null, value);
73 attributes = ImmutableMap.of(a1, av);
80 * use null to create top composite node (without parent)
84 public ImmutableCompositeNode(QName qname, List<Node<?>> value, ModifyAction modifyAction) {
85 super(qname, null, value, modifyAction);
86 attributes = ImmutableMap.of();
90 protected void init() {
91 if (getValue() != null) {
92 nodeMap = NodeUtils.buildNodeMap(getValue());
96 protected Map<QName, List<Node<?>>> getNodeMap() {
101 public List<Node<?>> getChildren() {
102 return Collections.unmodifiableList(getValue() == null ? new ArrayList<Node<?>>() : getValue());
106 public SimpleNode<?> getFirstSimpleByName(QName leafQName) {
107 List<SimpleNode<?>> list = getSimpleNodesByName(leafQName);
108 if (list.isEmpty()) {
115 public List<CompositeNode> getCompositesByName(QName children) {
116 List<Node<?>> toFilter = getNodeMap().get(children);
117 if (toFilter == null) {
118 return Collections.emptyList();
120 List<CompositeNode> list = new ArrayList<CompositeNode>();
121 for (Node<?> node : toFilter) {
122 if (node instanceof CompositeNode) {
123 list.add((CompositeNode) node);
130 public List<SimpleNode<?>> getSimpleNodesByName(QName children) {
131 List<Node<?>> toFilter = getNodeMap().get(children);
132 if (toFilter == null) {
133 return Collections.emptyList();
135 List<SimpleNode<?>> list = new ArrayList<SimpleNode<?>>();
137 for (Node<?> node : toFilter) {
138 if (node instanceof SimpleNode<?>) {
139 list.add((SimpleNode<?>) node);
146 public CompositeNode getFirstCompositeByName(QName container) {
147 List<CompositeNode> list = getCompositesByName(container);
148 if (list.isEmpty()) {
155 public Map<QName, String> getAttributes() {
160 public String getAttributeValue(QName key) {
161 return attributes.get(key);
166 * @return TODO:: do we need this method?
168 public SimpleNode<?> getFirstLeafByName(QName leaf) {
169 List<SimpleNode<?>> list = getSimpleNodesByName(leaf);
170 if (list.isEmpty()) {
177 public List<CompositeNode> getCompositesByName(String children) {
178 return getCompositesByName(new QName(getNodeType(), children));
182 public List<SimpleNode<?>> getSimpleNodesByName(String children) {
183 return getSimpleNodesByName(new QName(getNodeType(), children));
187 public MutableCompositeNode asMutable() {
188 throw new IllegalAccessError("cast to mutable is not supported - " + getClass().getSimpleName());
192 public String toString() {
193 return super.toString() + ", children.size = " + (getChildren() != null ? getChildren().size() : "n/a");
197 public void clear() {
202 public boolean containsKey(Object key) {
203 return nodeMap.containsKey(key);
207 public boolean containsValue(Object value) {
208 return nodeMap.containsValue(value);
212 public Set<java.util.Map.Entry<QName, List<Node<?>>>> entrySet() {
213 return nodeMap.entrySet();
217 public boolean equals(Object obj) {
218 return super.equals(obj);
223 return nodeMap.size();
227 public boolean isEmpty() {
228 return nodeMap.isEmpty();
232 public List<Node<?>> get(Object key) {
233 return nodeMap.get(key);
237 public List<Node<?>> put(QName key, List<Node<?>> value) {
238 return nodeMap.put(key, value);
242 public List<Node<?>> remove(Object key) {
243 return nodeMap.remove(key);
247 public void putAll(Map<? extends QName, ? extends List<Node<?>>> m) {
252 public Set<QName> keySet() {
253 return nodeMap.keySet();
257 public Collection<List<Node<?>>> values() {
258 return nodeMap.values();
261 // Serialization related
263 private void readObject(ObjectInputStream aStream) throws IOException, ClassNotFoundException {
264 aStream.defaultReadObject();
265 QName qName = (QName) aStream.readObject();
266 CompositeNode parent = (CompositeNode) aStream.readObject();
267 List<Node<?>> value = (List<Node<?>>) aStream.readObject();
268 ModifyAction modifyAction = (ModifyAction) aStream.readObject();
270 init(qName, parent, value, modifyAction);
273 private void writeObject(ObjectOutputStream aStream) throws IOException {
274 aStream.defaultWriteObject();
275 // manually serialize superclass
276 aStream.writeObject(getQName());
277 aStream.writeObject(getParent());
278 aStream.writeObject(getValue());
279 aStream.writeObject(getModificationAction());
282 public static CompositeNodeBuilder<ImmutableCompositeNode> builder() {
283 return new ImmutableCompositeNodeBuilder();
286 private static class ImmutableCompositeNodeBuilder extends AbstractCompositeNodeBuilder<ImmutableCompositeNode> {
289 public AbstractCompositeNodeBuilder<ImmutableCompositeNode> addLeaf(QName leafName, Object leafValue) {
290 add(new SimpleNodeTOImpl<Object>(leafName, null, leafValue));
295 public ImmutableCompositeNode toInstance() {
296 return ImmutableCompositeNode.create(this.getQName(), this.getAttributes(), this.getChildNodes());
301 public static ImmutableCompositeNode create(QName qName, List<Node<?>> childNodes) {
302 return new ImmutableCompositeNode(qName, ImmutableMap.<QName, String>of(),childNodes);
305 public static ImmutableCompositeNode create(QName qName, Map<QName, String> attributes, List<Node<?>> childNodes) {
306 return new ImmutableCompositeNode(qName, attributes,childNodes);