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