2 * Copyright (c) 2014 Cisco Systems, Inc. and others. All rights reserved.
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
8 package org.opendaylight.mdsal.binding.dom.codec.impl;
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;
23 class ListNodeCodecContext<D extends DataObject> extends DataObjectCodecContext<D, ListSchemaNode> {
24 ListNodeCodecContext(final DataContainerCodecPrototype<ListSchemaNode> prototype) {
28 ListNodeCodecContext(final DataContainerCodecPrototype<ListSchemaNode> prototype,
29 final Method keyMethod) {
30 super(prototype, keyMethod);
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);
40 throw new IllegalStateException("Unsupported data type " + node.getClass());
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);
55 throw new IllegalStateException("Unsupported data type " + node.getClass());
59 private Object fromMap(final MapNode map) {
60 final Collection<MapEntryNode> value = map.getValue();
61 // This should never happen, but we do need to ensure users never see an empty Map
62 return value.isEmpty() ? null : fromMap(map, value);
65 Object fromMap(final MapNode map, final Collection<MapEntryNode> value) {
66 // FIXME: Could be this lazy transformed list?
67 final Builder<D> builder = ImmutableList.builderWithExpectedSize(value.size());
68 for (MapEntryNode node : value) {
69 builder.add(createBindingProxy(node));
71 return builder.build();
74 final D fromMapEntry(final MapEntryNode node) {
75 return createBindingProxy(node);
78 private D fromUnkeyedListEntry(final UnkeyedListEntryNode node) {
79 return createBindingProxy(node);
82 private List<D> fromUnkeyedList(final UnkeyedListNode nodes) {
83 final Collection<UnkeyedListEntryNode> value = nodes.getValue();
84 if (value.isEmpty()) {
85 // This should never happen, but we do need to ensure users never see an empty List
89 // FIXME: Could be this lazy transformed list?
90 final Builder<D> builder = ImmutableList.builderWithExpectedSize(value.size());
91 for (UnkeyedListEntryNode node : value) {
92 builder.add(fromUnkeyedListEntry(node));
94 return builder.build();