BUG-2350: introduce KeyedListNodeCodecContext
[yangtools.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 com.google.common.collect.Iterables;
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 extends DataObjectCodecContext<ListSchemaNode> {
22     protected ListNodeCodecContext(final DataContainerCodecPrototype<ListSchemaNode> prototype) {
23         super(prototype);
24     }
25
26     @Override
27     protected Object dataFromNormalizedNode(final NormalizedNode<?, ?> node) {
28         if (node instanceof MapNode) {
29             return fromMap((MapNode) node);
30         } else if (node instanceof MapEntryNode) {
31             return fromMapEntry((MapEntryNode) node);
32         } else if (node instanceof UnkeyedListNode) {
33             return fromUnkeyedList((UnkeyedListNode) 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     private List<DataObject> fromMap(final MapNode nodes) {
42         List<DataObject> ret = new ArrayList<>(Iterables.size(nodes.getValue()));
43         for(MapEntryNode node : nodes.getValue()) {
44             ret.add(fromMapEntry(node));
45         }
46         return ret;
47     }
48
49     private DataObject fromMapEntry(final MapEntryNode node) {
50         return createBindingProxy(node);
51     }
52
53     private DataObject fromUnkeyedListEntry(final UnkeyedListEntryNode node) {
54         return createBindingProxy(node);
55     }
56
57     private List<DataObject> fromUnkeyedList(final UnkeyedListNode nodes) {
58         // FIXME: Could be this lazy transformed list?
59         List<DataObject> ret = new ArrayList<>(Iterables.size(nodes.getValue()));
60         for (UnkeyedListEntryNode node : nodes.getValue()) {
61             ret.add(fromUnkeyedListEntry(node));
62         }
63         return ret;
64     }
65 }