Merge "Fixed bug in antlr grammar to enable empty rpc input or output."
[yangtools.git] / yang / yang-data-impl / src / main / java / org / opendaylight / yangtools / yang / data / impl / ImmutableCompositeNode.java
1 /*
2  * Copyright (c) 2013 Cisco Systems, Inc. and others.  All rights reserved.
3  *
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
7  */
8 package org.opendaylight.yangtools.yang.data.impl;
9
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;
21
22 import com.google.common.collect.ImmutableList;
23 import com.google.common.collect.ImmutableMap;
24
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;
34 import java.util.Map;
35 import java.util.Set;
36
37 public final class ImmutableCompositeNode extends AbstractNodeTO<List<Node<?>>> implements //
38         Immutable, //
39         CompositeNode, //
40         AttributesContainer, //
41         Serializable {
42
43     private static final long serialVersionUID = 100L;
44
45     private Map<QName, List<Node<?>>> nodeMap = new HashMap<>();
46
47     private Map<QName, String> attributes;
48
49
50
51     /**
52      * @param qname
53      * @param parent
54      *            use null to create top composite node (without parent)
55      * @param value
56      */
57     private ImmutableCompositeNode(QName qname, Map<QName,String> attributes,List<Node<?>> value) {
58         super(qname, null, ImmutableList.copyOf(value));
59         attributes = ImmutableMap.copyOf(attributes);
60         init();
61     }
62
63     /**
64      * @param qname
65      * @param parent
66      *            use null to create top composite node (without parent)
67      * @param value
68      */
69     private ImmutableCompositeNode(QName qname, List<Node<?>> value, QName a1, String av) {
70         super(qname, null, value);
71         attributes = ImmutableMap.of(a1, av);
72         init();
73     }
74
75     /**
76      * @param qname
77      * @param parent
78      *            use null to create top composite node (without parent)
79      * @param value
80      * @param modifyAction
81      */
82     public ImmutableCompositeNode(QName qname, List<Node<?>> value, ModifyAction modifyAction) {
83         super(qname, null, value, modifyAction);
84         init();
85     }
86
87     protected void init() {
88         if (getValue() != null) {
89             nodeMap = NodeUtils.buildNodeMap(getValue());
90         }
91     }
92
93     protected Map<QName, List<Node<?>>> getNodeMap() {
94         return nodeMap;
95     }
96
97     @Override
98     public List<Node<?>> getChildren() {
99         return Collections.unmodifiableList(getValue() == null ? new ArrayList<Node<?>>() : getValue());
100     }
101
102     @Override
103     public SimpleNode<?> getFirstSimpleByName(QName leafQName) {
104         List<SimpleNode<?>> list = getSimpleNodesByName(leafQName);
105         if (list.isEmpty()) {
106             return null;
107         }
108         return list.get(0);
109     }
110
111     @Override
112     public List<CompositeNode> getCompositesByName(QName children) {
113         List<Node<?>> toFilter = getNodeMap().get(children);
114         if (toFilter == null) {
115             return Collections.emptyList();
116         }
117         List<CompositeNode> list = new ArrayList<CompositeNode>();
118         for (Node<?> node : toFilter) {
119             if (node instanceof CompositeNode) {
120                 list.add((CompositeNode) node);
121             }
122         }
123         return list;
124     }
125
126     @Override
127     public List<SimpleNode<?>> getSimpleNodesByName(QName children) {
128         List<Node<?>> toFilter = getNodeMap().get(children);
129         if (toFilter == null) {
130             return Collections.emptyList();
131         }
132         List<SimpleNode<?>> list = new ArrayList<SimpleNode<?>>();
133
134         for (Node<?> node : toFilter) {
135             if (node instanceof SimpleNode<?>) {
136                 list.add((SimpleNode<?>) node);
137             }
138         }
139         return list;
140     }
141
142     @Override
143     public CompositeNode getFirstCompositeByName(QName container) {
144         List<CompositeNode> list = getCompositesByName(container);
145         if (list.isEmpty()) {
146             return null;
147         }
148         return list.get(0);
149     }
150
151     @Override
152     public Map<QName, String> getAttributes() {
153         return attributes;
154     }
155
156     @Override
157     public String getAttributeValue(QName key) {
158         return attributes.get(key);
159     }
160
161     /**
162      * @param leaf
163      * @return TODO:: do we need this method?
164      */
165     public SimpleNode<?> getFirstLeafByName(QName leaf) {
166         List<SimpleNode<?>> list = getSimpleNodesByName(leaf);
167         if (list.isEmpty()) {
168             return null;
169         }
170         return list.get(0);
171     }
172
173     @Override
174     public List<CompositeNode> getCompositesByName(String children) {
175         return getCompositesByName(new QName(getNodeType(), children));
176     }
177
178     @Override
179     public List<SimpleNode<?>> getSimpleNodesByName(String children) {
180         return getSimpleNodesByName(new QName(getNodeType(), children));
181     }
182
183     @Override
184     public MutableCompositeNode asMutable() {
185         throw new IllegalAccessError("cast to mutable is not supported - " + getClass().getSimpleName());
186     }
187
188     @Override
189     public String toString() {
190         return super.toString() + ", children.size = " + (getChildren() != null ? getChildren().size() : "n/a");
191     }
192
193     @Override
194     public void clear() {
195         nodeMap.clear();
196     }
197
198     @Override
199     public boolean containsKey(Object key) {
200         return nodeMap.containsKey(key);
201     }
202
203     @Override
204     public boolean containsValue(Object value) {
205         return nodeMap.containsValue(value);
206     }
207
208     @Override
209     public Set<java.util.Map.Entry<QName, List<Node<?>>>> entrySet() {
210         return nodeMap.entrySet();
211     }
212
213     @Override
214     public boolean equals(Object obj) {
215         return super.equals(obj);
216     }
217
218     @Override
219     public int size() {
220         return nodeMap.size();
221     }
222
223     @Override
224     public boolean isEmpty() {
225         return nodeMap.isEmpty();
226     }
227
228     @Override
229     public List<Node<?>> get(Object key) {
230         return nodeMap.get(key);
231     }
232
233     @Override
234     public List<Node<?>> put(QName key, List<Node<?>> value) {
235         return nodeMap.put(key, value);
236     }
237
238     @Override
239     public List<Node<?>> remove(Object key) {
240         return nodeMap.remove(key);
241     }
242
243     @Override
244     public void putAll(Map<? extends QName, ? extends List<Node<?>>> m) {
245         nodeMap.putAll(m);
246     }
247
248     @Override
249     public Set<QName> keySet() {
250         return nodeMap.keySet();
251     }
252
253     @Override
254     public Collection<List<Node<?>>> values() {
255         return nodeMap.values();
256     }
257
258     // Serialization related
259
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();
266
267         init(qName, parent, value, modifyAction);
268     }
269
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());
277     }
278
279     public static CompositeNodeBuilder<ImmutableCompositeNode> builder() {
280         return new ImmutableCompositeNodeBuilder();
281     }
282
283     private static class ImmutableCompositeNodeBuilder extends AbstractCompositeNodeBuilder<ImmutableCompositeNode> {
284
285         @Override
286         public AbstractCompositeNodeBuilder<ImmutableCompositeNode> addLeaf(QName leafName, Object leafValue) {
287             add(new SimpleNodeTOImpl<Object>(leafName, null, leafValue));
288             return this;
289         }
290
291         @Override
292         public ImmutableCompositeNode toInstance() {
293             return ImmutableCompositeNode.create(this.getQName(), this.getAttributes(), this.getChildNodes());
294         }
295
296     }
297
298     public static ImmutableCompositeNode create(QName qName, List<Node<?>> childNodes) {
299         return new ImmutableCompositeNode(qName, ImmutableMap.<QName, String>of(),childNodes);
300     }
301     
302     public static ImmutableCompositeNode create(QName qName, Map<QName, String> attributes, List<Node<?>> childNodes) {
303         return new ImmutableCompositeNode(qName, attributes,childNodes);
304     }
305 }