Cache NodeContextSuppliers in CodecDataObject
[mdsal.git] / binding / mdsal-binding-dom-codec / src / main / java / org / opendaylight / mdsal / binding / dom / codec / impl / ListNodeCodecContext.java
1 /*
2  * Copyright (c) 2014 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.mdsal.binding.dom.codec.impl;
9
10 import java.lang.reflect.Method;
11 import java.util.ArrayList;
12 import java.util.List;
13 import java.util.Map.Entry;
14 import org.opendaylight.yangtools.yang.binding.DataObject;
15 import org.opendaylight.yangtools.yang.data.api.schema.MapEntryNode;
16 import org.opendaylight.yangtools.yang.data.api.schema.MapNode;
17 import org.opendaylight.yangtools.yang.data.api.schema.NormalizedNode;
18 import org.opendaylight.yangtools.yang.data.api.schema.UnkeyedListEntryNode;
19 import org.opendaylight.yangtools.yang.data.api.schema.UnkeyedListNode;
20 import org.opendaylight.yangtools.yang.model.api.ListSchemaNode;
21
22 class ListNodeCodecContext<D extends DataObject> extends DataObjectCodecContext<D, ListSchemaNode> {
23     ListNodeCodecContext(final DataContainerCodecPrototype<ListSchemaNode> prototype) {
24         super(prototype);
25     }
26
27     ListNodeCodecContext(final DataContainerCodecPrototype<ListSchemaNode> prototype,
28             final Entry<Method, IdentifiableItemCodec> keyMethod) {
29         super(prototype, keyMethod);
30     }
31
32     @Override
33     public D deserialize(final NormalizedNode<?, ?> node) {
34         if (node instanceof MapEntryNode) {
35             return fromMapEntry((MapEntryNode) node);
36         } else if (node instanceof UnkeyedListEntryNode) {
37             return fromUnkeyedListEntry((UnkeyedListEntryNode) node);
38         } else {
39             throw new IllegalStateException("Unsupported data type " + node.getClass());
40         }
41     }
42
43     @Override
44     protected Object deserializeObject(final NormalizedNode<?, ?> node) {
45         if (node instanceof MapNode) {
46             return fromMap((MapNode) node);
47         } else if (node instanceof MapEntryNode) {
48             return fromMapEntry((MapEntryNode) node);
49         } else if (node instanceof UnkeyedListNode) {
50             return fromUnkeyedList((UnkeyedListNode) node);
51         } else if (node instanceof UnkeyedListEntryNode) {
52             return fromUnkeyedListEntry((UnkeyedListEntryNode) node);
53         } else {
54             throw new IllegalStateException("Unsupported data type " + node.getClass());
55         }
56     }
57
58     private List<D> fromMap(final MapNode nodes) {
59         List<D> ret = new ArrayList<>(nodes.getValue().size());
60         for (MapEntryNode node : nodes.getValue()) {
61             ret.add(fromMapEntry(node));
62         }
63         return ret;
64     }
65
66     private D fromMapEntry(final MapEntryNode node) {
67         return createBindingProxy(node);
68     }
69
70     private D fromUnkeyedListEntry(final UnkeyedListEntryNode node) {
71         return createBindingProxy(node);
72     }
73
74     private List<D> fromUnkeyedList(final UnkeyedListNode nodes) {
75         // FIXME: Could be this lazy transformed list?
76         List<D> ret = new ArrayList<>(nodes.getValue().size());
77         for (UnkeyedListEntryNode node : nodes.getValue()) {
78             ret.add(fromUnkeyedListEntry(node));
79         }
80         return ret;
81     }
82 }