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 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));
66 return builder.build();
69 final D fromMapEntry(final MapEntryNode node) {
70 return createBindingProxy(node);
73 private D fromUnkeyedListEntry(final UnkeyedListEntryNode node) {
74 return createBindingProxy(node);
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));
84 return builder.build();