e98ab2661575366b27484db20b15e867f5777e02
[mdsal.git] / binding2 / mdsal-binding2-dom-codec / src / main / java / org / opendaylight / mdsal / binding / javav2 / dom / codec / impl / context / ListNodeCodecContext.java
1 /*
2  * Copyright (c) 2017 Pantheon Technologies s.r.o. 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.javav2.dom.codec.impl.context;
9
10 import com.google.common.annotations.Beta;
11 import java.util.ArrayList;
12 import java.util.List;
13 import javax.annotation.Nonnull;
14 import org.opendaylight.mdsal.binding.javav2.dom.codec.impl.context.base.DataContainerCodecPrototype;
15 import org.opendaylight.mdsal.binding.javav2.dom.codec.impl.context.base.TreeNodeCodecContext;
16 import org.opendaylight.mdsal.binding.javav2.spec.base.TreeNode;
17 import org.opendaylight.yangtools.yang.data.api.schema.MapEntryNode;
18 import org.opendaylight.yangtools.yang.data.api.schema.MapNode;
19 import org.opendaylight.yangtools.yang.data.api.schema.NormalizedNode;
20 import org.opendaylight.yangtools.yang.data.api.schema.UnkeyedListEntryNode;
21 import org.opendaylight.yangtools.yang.data.api.schema.UnkeyedListNode;
22 import org.opendaylight.yangtools.yang.model.api.ListSchemaNode;
23
24 /**
25  * Codec context for serializing and deserializing list node.
26  *
27  * @param <D>
28  *            - type of tree node
29  */
30 @Beta
31 public class ListNodeCodecContext<D extends TreeNode> extends TreeNodeCodecContext<D, ListSchemaNode> {
32
33     /**
34      * Prepare context for list node from prototype.
35      *
36      * @param prototype
37      *            - codec prototype of list node
38      */
39     public ListNodeCodecContext(final DataContainerCodecPrototype<ListSchemaNode> prototype) {
40         super(prototype);
41     }
42
43     @Nonnull
44     @Override
45     public D deserialize(@Nonnull final NormalizedNode<?, ?> node) {
46         if (node instanceof MapEntryNode) {
47             return fromMapEntry((MapEntryNode) node);
48         } else if (node instanceof UnkeyedListEntryNode) {
49             return fromUnkeyedListEntry((UnkeyedListEntryNode) node);
50         } else {
51             throw new IllegalStateException("Unsupported data type " + node.getClass());
52         }
53     }
54
55     @Override
56     protected Object deserializeObject(final NormalizedNode<?, ?> node) {
57         if (node instanceof MapNode) {
58             return fromMap((MapNode) node);
59         } else if (node instanceof MapEntryNode) {
60             return fromMapEntry((MapEntryNode) node);
61         } else if (node instanceof UnkeyedListNode) {
62             return fromUnkeyedList((UnkeyedListNode) node);
63         } else if (node instanceof UnkeyedListEntryNode) {
64             return fromUnkeyedListEntry((UnkeyedListEntryNode) node);
65         } else {
66             throw new IllegalStateException("Unsupported data type " + node.getClass());
67         }
68     }
69
70     private List<D> fromMap(final MapNode nodes) {
71         final List<D> ret = new ArrayList<>(nodes.getValue().size());
72         for (final MapEntryNode node : nodes.getValue()) {
73             ret.add(fromMapEntry(node));
74         }
75         return ret;
76     }
77
78     private D fromMapEntry(final MapEntryNode node) {
79         return createBindingProxy(node);
80     }
81
82     private D fromUnkeyedListEntry(final UnkeyedListEntryNode node) {
83         return createBindingProxy(node);
84     }
85
86     private List<D> fromUnkeyedList(final UnkeyedListNode nodes) {
87         // FIXME: Could be this lazy transformed list?
88         final List<D> ret = new ArrayList<>(nodes.getValue().size());
89         for (final UnkeyedListEntryNode node : nodes.getValue()) {
90             ret.add(fromUnkeyedListEntry(node));
91         }
92         return ret;
93     }
94 }