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 org.opendaylight.yangtools.yang.data.impl.util.NodeBuilder;
22 import com.google.common.collect.ImmutableList;
23 import com.google.common.collect.ImmutableMap;
25 import java.io.IOException;
26 import java.io.ObjectInputStream;
27 import java.io.ObjectOutputStream;
28 import java.io.Serializable;
29 import java.util.ArrayList;
30 import java.util.Collection;
31 import java.util.Collections;
32 import java.util.HashMap;
33 import java.util.List;
37 public final class ImmutableCompositeNode extends AbstractNodeTO<List<Node<?>>> implements //
40 AttributesContainer, //
43 private static final long serialVersionUID = 100L;
45 private Map<QName, List<Node<?>>> nodeMap = new HashMap<>();
47 private Map<QName, String> attributes;
54 * use null to create top composite node (without parent)
57 private ImmutableCompositeNode(QName qname, Map<QName,String> attributes,List<Node<?>> value) {
58 super(qname, null, ImmutableList.copyOf(value));
59 attributes = ImmutableMap.copyOf(attributes);
66 * use null to create top composite node (without parent)
69 private ImmutableCompositeNode(QName qname, List<Node<?>> value, QName a1, String av) {
70 super(qname, null, value);
71 attributes = ImmutableMap.of(a1, av);
78 * use null to create top composite node (without parent)
82 public ImmutableCompositeNode(QName qname, List<Node<?>> value, ModifyAction modifyAction) {
83 super(qname, null, value, modifyAction);
87 protected void init() {
88 if (getValue() != null) {
89 nodeMap = NodeUtils.buildNodeMap(getValue());
93 protected Map<QName, List<Node<?>>> getNodeMap() {
98 public List<Node<?>> getChildren() {
99 return Collections.unmodifiableList(getValue() == null ? new ArrayList<Node<?>>() : getValue());
103 public SimpleNode<?> getFirstSimpleByName(QName leafQName) {
104 List<SimpleNode<?>> list = getSimpleNodesByName(leafQName);
105 if (list.isEmpty()) {
112 public List<CompositeNode> getCompositesByName(QName children) {
113 List<Node<?>> toFilter = getNodeMap().get(children);
114 if (toFilter == null) {
115 return Collections.emptyList();
117 List<CompositeNode> list = new ArrayList<CompositeNode>();
118 for (Node<?> node : toFilter) {
119 if (node instanceof CompositeNode) {
120 list.add((CompositeNode) node);
127 public List<SimpleNode<?>> getSimpleNodesByName(QName children) {
128 List<Node<?>> toFilter = getNodeMap().get(children);
129 if (toFilter == null) {
130 return Collections.emptyList();
132 List<SimpleNode<?>> list = new ArrayList<SimpleNode<?>>();
134 for (Node<?> node : toFilter) {
135 if (node instanceof SimpleNode<?>) {
136 list.add((SimpleNode<?>) node);
143 public CompositeNode getFirstCompositeByName(QName container) {
144 List<CompositeNode> list = getCompositesByName(container);
145 if (list.isEmpty()) {
152 public Map<QName, String> getAttributes() {
157 public String getAttributeValue(QName key) {
158 return attributes.get(key);
163 * @return TODO:: do we need this method?
165 public SimpleNode<?> getFirstLeafByName(QName leaf) {
166 List<SimpleNode<?>> list = getSimpleNodesByName(leaf);
167 if (list.isEmpty()) {
174 public List<CompositeNode> getCompositesByName(String children) {
175 return getCompositesByName(new QName(getNodeType(), children));
179 public List<SimpleNode<?>> getSimpleNodesByName(String children) {
180 return getSimpleNodesByName(new QName(getNodeType(), children));
184 public MutableCompositeNode asMutable() {
185 throw new IllegalAccessError("cast to mutable is not supported - " + getClass().getSimpleName());
189 public String toString() {
190 return super.toString() + ", children.size = " + (getChildren() != null ? getChildren().size() : "n/a");
194 public void clear() {
199 public boolean containsKey(Object key) {
200 return nodeMap.containsKey(key);
204 public boolean containsValue(Object value) {
205 return nodeMap.containsValue(value);
209 public Set<java.util.Map.Entry<QName, List<Node<?>>>> entrySet() {
210 return nodeMap.entrySet();
214 public boolean equals(Object obj) {
215 return super.equals(obj);
220 return nodeMap.size();
224 public boolean isEmpty() {
225 return nodeMap.isEmpty();
229 public List<Node<?>> get(Object key) {
230 return nodeMap.get(key);
234 public List<Node<?>> put(QName key, List<Node<?>> value) {
235 return nodeMap.put(key, value);
239 public List<Node<?>> remove(Object key) {
240 return nodeMap.remove(key);
244 public void putAll(Map<? extends QName, ? extends List<Node<?>>> m) {
249 public Set<QName> keySet() {
250 return nodeMap.keySet();
254 public Collection<List<Node<?>>> values() {
255 return nodeMap.values();
258 // Serialization related
260 private void readObject(ObjectInputStream aStream) throws IOException, ClassNotFoundException {
261 aStream.defaultReadObject();
262 QName qName = (QName) aStream.readObject();
263 CompositeNode parent = (CompositeNode) aStream.readObject();
264 List<Node<?>> value = (List<Node<?>>) aStream.readObject();
265 ModifyAction modifyAction = (ModifyAction) aStream.readObject();
267 init(qName, parent, value, modifyAction);
270 private void writeObject(ObjectOutputStream aStream) throws IOException {
271 aStream.defaultWriteObject();
272 // manually serialize superclass
273 aStream.writeObject(getQName());
274 aStream.writeObject(getParent());
275 aStream.writeObject(getValue());
276 aStream.writeObject(getModificationAction());
279 public static CompositeNodeBuilder<ImmutableCompositeNode> builder() {
280 return new ImmutableCompositeNodeBuilder();
283 private static class ImmutableCompositeNodeBuilder extends AbstractCompositeNodeBuilder<ImmutableCompositeNode> {
286 public AbstractCompositeNodeBuilder<ImmutableCompositeNode> addLeaf(QName leafName, Object leafValue) {
287 add(new SimpleNodeTOImpl<Object>(leafName, null, leafValue));
292 public ImmutableCompositeNode toInstance() {
293 return ImmutableCompositeNode.create(this.getQName(), this.getAttributes(), this.getChildNodes());
298 public static ImmutableCompositeNode create(QName qName, List<Node<?>> childNodes) {
299 return new ImmutableCompositeNode(qName, ImmutableMap.<QName, String>of(),childNodes);
302 public static ImmutableCompositeNode create(QName qName, Map<QName, String> attributes, List<Node<?>> childNodes) {
303 return new ImmutableCompositeNode(qName, attributes,childNodes);