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