2577b615f15d7233cab31bb30178860b841ca9fa
[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.eclipse.jdt.annotation.NonNull;
16 import org.opendaylight.yangtools.yang.binding.DataObject;
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 class ListNodeCodecContext<D extends DataObject> extends DataObjectCodecContext<D, ListSchemaNode> {
25     ListNodeCodecContext(final DataContainerCodecPrototype<ListSchemaNode> prototype) {
26         super(prototype);
27     }
28
29     ListNodeCodecContext(final DataContainerCodecPrototype<ListSchemaNode> prototype,
30             final Method keyMethod) {
31         super(prototype, keyMethod);
32     }
33
34     @Override
35     public D deserialize(final NormalizedNode<?, ?> node) {
36         if (node instanceof MapEntryNode) {
37             return fromMapEntry((MapEntryNode) node);
38         } else if (node instanceof UnkeyedListEntryNode) {
39             return fromUnkeyedListEntry((UnkeyedListEntryNode) node);
40         } else {
41             throw new IllegalStateException("Unsupported data type " + node.getClass());
42         }
43     }
44
45     @Override
46     protected Object deserializeObject(final NormalizedNode<?, ?> node) {
47         if (node instanceof MapNode) {
48             return fromMap((MapNode) node);
49         } else if (node instanceof MapEntryNode) {
50             return fromMapEntry((MapEntryNode) node);
51         } else if (node instanceof UnkeyedListNode) {
52             return fromUnkeyedList((UnkeyedListNode) node);
53         } else if (node instanceof UnkeyedListEntryNode) {
54             return fromUnkeyedListEntry((UnkeyedListEntryNode) node);
55         } else {
56             throw new IllegalStateException("Unsupported data type " + node.getClass());
57         }
58     }
59
60     private Object fromMap(final MapNode map) {
61         final int size;
62         // This should never happen, but we do need to ensure users never see an empty Map
63         return (size = map.size()) == 0 ? null : fromMap(map, size);
64     }
65
66     @NonNull Object fromMap(final MapNode map, final int size) {
67         // FIXME: Make this a lazily-populated list
68         final Builder<D> builder = ImmutableList.builderWithExpectedSize(size);
69         for (MapEntryNode node : map.getValue()) {
70             builder.add(createBindingProxy(node));
71         }
72         return builder.build();
73     }
74
75     final @NonNull D fromMapEntry(final MapEntryNode node) {
76         return createBindingProxy(node);
77     }
78
79     private @NonNull D fromUnkeyedListEntry(final UnkeyedListEntryNode node) {
80         return createBindingProxy(node);
81     }
82
83     private List<D> fromUnkeyedList(final UnkeyedListNode nodes) {
84         final Collection<UnkeyedListEntryNode> value = nodes.getValue();
85         if (value.isEmpty()) {
86             // This should never happen, but we do need to ensure users never see an empty List
87             return null;
88         }
89
90         // FIXME: Could be this lazy transformed list?
91         final Builder<D> builder = ImmutableList.builderWithExpectedSize(value.size());
92         for (UnkeyedListEntryNode node : value) {
93             builder.add(fromUnkeyedListEntry(node));
94         }
95         return builder.build();
96     }
97 }