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