Rename ContainerNodeCodecContext
[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 static java.util.Objects.requireNonNull;
11
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;
22
23 sealed class ListNodeCodecContext<D extends DataObject> extends DataObjectCodecContext<D, ListRuntimeType>
24         permits KeyedListNodeCodecContext {
25     ListNodeCodecContext(final DataContainerCodecPrototype<ListRuntimeType> prototype) {
26         super(prototype);
27     }
28
29     ListNodeCodecContext(final DataContainerCodecPrototype<ListRuntimeType> prototype,
30             final Method keyMethod) {
31         super(prototype, keyMethod);
32     }
33
34     @Override
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);
41         } else {
42             throw new IllegalArgumentException("Expecting either a MapEntryNode or an UnkeyedListEntryNode, not "
43                 + node.contract().getSimpleName());
44         }
45     }
46
47     @Override
48     protected Object deserializeObject(final NormalizedNode node) {
49         if (node instanceof MapNode map) {
50             return fromMap(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);
57         } else {
58             throw new IllegalStateException("Unsupported data type " + node.contract().getSimpleName());
59         }
60     }
61
62     @NonNull Object fromMap(final MapNode map, final int size) {
63         return LazyBindingList.create(this, size, map.body());
64     }
65
66     private Object fromMap(final MapNode map) {
67         final int size;
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);
70     }
71
72     private List<D> fromUnkeyedList(final UnkeyedListNode node) {
73         final int size;
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());
76     }
77 }