Hide CodecContext methods
[mdsal.git] / binding / mdsal-binding-dom-codec / src / main / java / org / opendaylight / mdsal / binding / dom / codec / impl / ListCodecContext.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.binding.NodeStep;
18 import org.opendaylight.yangtools.yang.data.api.schema.MapEntryNode;
19 import org.opendaylight.yangtools.yang.data.api.schema.MapNode;
20 import org.opendaylight.yangtools.yang.data.api.schema.NormalizedNode;
21 import org.opendaylight.yangtools.yang.data.api.schema.UnkeyedListEntryNode;
22 import org.opendaylight.yangtools.yang.data.api.schema.UnkeyedListNode;
23
24 sealed class ListCodecContext<D extends DataObject> extends DataObjectCodecContext<D, ListRuntimeType>
25         permits MapCodecContext {
26     ListCodecContext(final Class<D> cls, final ListRuntimeType list, final CodecContextFactory factory) {
27         this(new ListCodecPrototype(new NodeStep<>(cls), list, factory));
28     }
29
30     ListCodecContext(final ListCodecPrototype prototype) {
31         super(prototype);
32     }
33
34     ListCodecContext(final ListCodecPrototype prototype, final Method keyMethod) {
35         super(prototype, keyMethod);
36     }
37
38     @Override
39     public D deserialize(final NormalizedNode node) {
40         final var nonnull = requireNonNull(node);
41         if (nonnull instanceof MapEntryNode mapEntry) {
42             return createBindingProxy(mapEntry);
43         } else if (nonnull instanceof UnkeyedListEntryNode unkeyedEntry) {
44             return createBindingProxy(unkeyedEntry);
45         } else {
46             throw new IllegalArgumentException("Expecting either a MapEntryNode or an UnkeyedListEntryNode, not "
47                 + node.contract().getSimpleName());
48         }
49     }
50
51     @Override
52     Object deserializeObject(final NormalizedNode node) {
53         if (node instanceof MapNode map) {
54             return fromMap(map);
55         } else if (node instanceof MapEntryNode mapEntry) {
56             return createBindingProxy(mapEntry);
57         } else if (node instanceof UnkeyedListNode list) {
58             return fromUnkeyedList(list);
59         } else if (node instanceof UnkeyedListEntryNode listEntry) {
60             return createBindingProxy(listEntry);
61         } else {
62             throw new IllegalStateException("Unsupported data type " + node.contract().getSimpleName());
63         }
64     }
65
66     @NonNull Object fromMap(final MapNode map, final int size) {
67         return LazyBindingList.of(this, size, map.body());
68     }
69
70     private Object fromMap(final MapNode map) {
71         final int size;
72         // This should never happen, but we do need to ensure users never see an empty Map
73         return (size = map.size()) == 0 ? null : fromMap(map, size);
74     }
75
76     private List<D> fromUnkeyedList(final UnkeyedListNode node) {
77         final int size;
78         // This should never happen, but we do need to ensure users never see an empty List
79         return (size = node.size()) == 0 ? null : LazyBindingList.of(this, size, node.body());
80     }
81 }