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