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