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 static java.util.Objects.requireNonNull;
12 import java.lang.reflect.Method;
13 import java.util.List;
14 import org.eclipse.jdt.annotation.NonNull;
15 import org.opendaylight.mdsal.binding.runtime.api.ListRuntimeType;
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;
23 sealed class ListNodeCodecContext<D extends DataObject> extends DataObjectCodecContext<D, ListRuntimeType>
24 permits KeyedListNodeCodecContext {
25 ListNodeCodecContext(final DataContainerCodecPrototype<ListRuntimeType> prototype) {
29 ListNodeCodecContext(final DataContainerCodecPrototype<ListRuntimeType> prototype,
30 final Method keyMethod) {
31 super(prototype, keyMethod);
35 public D deserialize(final NormalizedNode node) {
36 final var nonnull = requireNonNull(node);
37 if (nonnull instanceof MapEntryNode mapEntry) {
38 return createBindingProxy(mapEntry);
39 } else if (nonnull instanceof UnkeyedListEntryNode unkeyedEntry) {
40 return createBindingProxy(unkeyedEntry);
42 throw new IllegalArgumentException("Expecting either a MapEntryNode or an UnkeyedListEntryNode, not "
43 + node.contract().getSimpleName());
48 protected Object deserializeObject(final NormalizedNode node) {
49 if (node instanceof MapNode map) {
51 } else if (node instanceof MapEntryNode mapEntry) {
52 return createBindingProxy(mapEntry);
53 } else if (node instanceof UnkeyedListNode list) {
54 return fromUnkeyedList(list);
55 } else if (node instanceof UnkeyedListEntryNode listEntry) {
56 return createBindingProxy(listEntry);
58 throw new IllegalStateException("Unsupported data type " + node.contract().getSimpleName());
62 @NonNull Object fromMap(final MapNode map, final int size) {
63 return LazyBindingList.create(this, size, map.body());
66 private Object fromMap(final MapNode map) {
68 // This should never happen, but we do need to ensure users never see an empty Map
69 return (size = map.size()) == 0 ? null : fromMap(map, size);
72 private List<D> fromUnkeyedList(final UnkeyedListNode node) {
74 // This should never happen, but we do need to ensure users never see an empty List
75 return (size = node.size()) == 0 ? null : LazyBindingList.create(this, size, node.body());