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.*;
13 import org.opendaylight.yangtools.yang.data.api.ModifyAction;
14 import org.opendaylight.yangtools.yang.data.impl.util.AbstractCompositeNodeBuilder;
15 import org.opendaylight.yangtools.yang.data.impl.util.CompositeNodeBuilder;
16 import com.google.common.collect.ImmutableList;
17 import com.google.common.collect.ImmutableMap;
19 import java.io.IOException;
20 import java.io.ObjectInputStream;
21 import java.io.ObjectOutputStream;
22 import java.io.Serializable;
23 import java.util.ArrayList;
24 import java.util.Collection;
25 import java.util.Collections;
26 import java.util.HashMap;
27 import java.util.List;
31 public final class ImmutableCompositeNode extends AbstractNodeTO<List<Node<?>>> implements //
34 AttributesContainer, //
37 private static final long serialVersionUID = 100L;
39 private Map<QName, List<Node<?>>> nodeMap = new HashMap<>();
41 private final Map<QName, String> attributes;
48 * use null to create top composite node (without parent)
51 private ImmutableCompositeNode(QName qname, Map<QName,String> attributes,List<Node<?>> value) {
52 super(qname, null, ImmutableList.copyOf(value));
53 if(attributes == null) {
54 this.attributes = ImmutableMap.<QName, String>of();
56 this.attributes = ImmutableMap.copyOf(attributes);
64 * use null to create top composite node (without parent)
67 private ImmutableCompositeNode(QName qname, List<Node<?>> value, QName a1, String av) {
68 super(qname, null, value);
69 attributes = ImmutableMap.of(a1, av);
76 * use null to create top composite node (without parent)
80 public ImmutableCompositeNode(QName qname, List<Node<?>> value, ModifyAction modifyAction) {
81 super(qname, null, value, modifyAction);
82 attributes = ImmutableMap.of();
86 protected void init() {
87 if (getValue() != null) {
88 nodeMap = NodeUtils.buildNodeMap(getValue());
92 protected Map<QName, List<Node<?>>> getNodeMap() {
97 public List<Node<?>> getChildren() {
98 return Collections.unmodifiableList(getValue() == null ? new ArrayList<Node<?>>() : getValue());
102 public SimpleNode<?> getFirstSimpleByName(QName leafQName) {
103 List<SimpleNode<?>> list = getSimpleNodesByName(leafQName);
104 if (list.isEmpty()) {
111 public List<CompositeNode> getCompositesByName(QName children) {
112 List<Node<?>> toFilter = getNodeMap().get(children);
113 if (toFilter == null) {
114 return Collections.emptyList();
116 List<CompositeNode> list = new ArrayList<CompositeNode>();
117 for (Node<?> node : toFilter) {
118 if (node instanceof CompositeNode) {
119 list.add((CompositeNode) node);
126 public List<SimpleNode<?>> getSimpleNodesByName(QName children) {
127 List<Node<?>> toFilter = getNodeMap().get(children);
128 if (toFilter == null) {
129 return Collections.emptyList();
131 List<SimpleNode<?>> list = new ArrayList<SimpleNode<?>>();
133 for (Node<?> node : toFilter) {
134 if (node instanceof SimpleNode<?>) {
135 list.add((SimpleNode<?>) node);
142 public CompositeNode getFirstCompositeByName(QName container) {
143 List<CompositeNode> list = getCompositesByName(container);
144 if (list.isEmpty()) {
151 public Map<QName, String> getAttributes() {
156 public String getAttributeValue(QName key) {
157 return attributes.get(key);
162 * @return TODO:: do we need this method?
164 public SimpleNode<?> getFirstLeafByName(QName leaf) {
165 List<SimpleNode<?>> list = getSimpleNodesByName(leaf);
166 if (list.isEmpty()) {
173 public List<CompositeNode> getCompositesByName(String children) {
174 return getCompositesByName(new QName(getNodeType(), children));
178 public List<SimpleNode<?>> getSimpleNodesByName(String children) {
179 return getSimpleNodesByName(new QName(getNodeType(), children));
183 public MutableCompositeNode asMutable() {
184 throw new IllegalAccessError("cast to mutable is not supported - " + getClass().getSimpleName());
188 public String toString() {
189 return super.toString() + ", children.size = " + (getChildren() != null ? getChildren().size() : "n/a");
193 public void clear() {
198 public boolean containsKey(Object key) {
199 return nodeMap.containsKey(key);
203 public boolean containsValue(Object value) {
204 return nodeMap.containsValue(value);
208 public Set<java.util.Map.Entry<QName, List<Node<?>>>> entrySet() {
209 return nodeMap.entrySet();
213 public boolean equals(Object obj) {
214 return super.equals(obj);
219 return nodeMap.size();
223 public boolean isEmpty() {
224 return nodeMap.isEmpty();
228 public List<Node<?>> get(Object key) {
229 return nodeMap.get(key);
233 public List<Node<?>> put(QName key, List<Node<?>> value) {
234 return nodeMap.put(key, value);
238 public List<Node<?>> remove(Object key) {
239 return nodeMap.remove(key);
243 public void putAll(Map<? extends QName, ? extends List<Node<?>>> m) {
248 public Set<QName> keySet() {
249 return nodeMap.keySet();
253 public Collection<List<Node<?>>> values() {
254 return nodeMap.values();
257 // Serialization related
259 private void readObject(ObjectInputStream aStream) throws IOException, ClassNotFoundException {
260 aStream.defaultReadObject();
261 QName qName = (QName) aStream.readObject();
262 CompositeNode parent = (CompositeNode) aStream.readObject();
263 List<Node<?>> value = (List<Node<?>>) aStream.readObject();
264 ModifyAction modifyAction = (ModifyAction) aStream.readObject();
266 init(qName, parent, value, modifyAction);
269 private void writeObject(ObjectOutputStream aStream) throws IOException {
270 aStream.defaultWriteObject();
271 // manually serialize superclass
272 aStream.writeObject(getQName());
273 aStream.writeObject(getParent());
274 aStream.writeObject(getValue());
275 aStream.writeObject(getModificationAction());
278 public static CompositeNodeBuilder<ImmutableCompositeNode> builder() {
279 return new ImmutableCompositeNodeBuilder();
282 private static class ImmutableCompositeNodeBuilder extends AbstractCompositeNodeBuilder<ImmutableCompositeNode> {
285 public AbstractCompositeNodeBuilder<ImmutableCompositeNode> addLeaf(QName leafName, Object leafValue) {
286 add(new SimpleNodeTOImpl<Object>(leafName, null, leafValue));
291 public ImmutableCompositeNode toInstance() {
292 return ImmutableCompositeNode.create(this.getQName(), this.getAttributes(), this.getChildNodes());
297 public static ImmutableCompositeNode create(QName qName, List<Node<?>> childNodes) {
298 return new ImmutableCompositeNode(qName, ImmutableMap.<QName, String>of(),childNodes);
301 public static ImmutableCompositeNode create(QName qName, Map<QName, String> attributes, List<Node<?>> childNodes) {
302 return new ImmutableCompositeNode(qName, attributes,childNodes);
305 public static ImmutableCompositeNode create(QName qName, List<Node<?>> childNodes, ModifyAction modifyAction) {
306 return new ImmutableCompositeNode(qName, childNodes, modifyAction);