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