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