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.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;
24 class ListNodeCodecContext<D extends DataObject> extends DataObjectCodecContext<D, ListSchemaNode> {
25 ListNodeCodecContext(final DataContainerCodecPrototype<ListSchemaNode> prototype) {
29 ListNodeCodecContext(final DataContainerCodecPrototype<ListSchemaNode> prototype,
30 final Method keyMethod) {
31 super(prototype, keyMethod);
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);
41 throw new IllegalStateException("Unsupported data type " + node.getClass());
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);
56 throw new IllegalStateException("Unsupported data type " + node.getClass());
60 private Object fromMap(final MapNode map) {
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);
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));
72 return builder.build();
75 final @NonNull D fromMapEntry(final MapEntryNode node) {
76 return createBindingProxy(node);
79 private @NonNull D fromUnkeyedListEntry(final UnkeyedListEntryNode node) {
80 return createBindingProxy(node);
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
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));
95 return builder.build();