e69b6e9281c5c45750dfa1b02b22564589a15f99
[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 java.lang.reflect.Method;
11 import java.util.List;
12 import org.eclipse.jdt.annotation.NonNull;
13 import org.opendaylight.mdsal.binding.runtime.api.ListRuntimeType;
14 import org.opendaylight.yangtools.yang.binding.DataObject;
15 import org.opendaylight.yangtools.yang.data.api.schema.MapEntryNode;
16 import org.opendaylight.yangtools.yang.data.api.schema.MapNode;
17 import org.opendaylight.yangtools.yang.data.api.schema.NormalizedNode;
18 import org.opendaylight.yangtools.yang.data.api.schema.UnkeyedListEntryNode;
19 import org.opendaylight.yangtools.yang.data.api.schema.UnkeyedListNode;
20
21 class ListNodeCodecContext<D extends DataObject> extends DataObjectCodecContext<D, ListRuntimeType> {
22     ListNodeCodecContext(final DataContainerCodecPrototype<ListRuntimeType> prototype) {
23         super(prototype);
24     }
25
26     ListNodeCodecContext(final DataContainerCodecPrototype<ListRuntimeType> prototype,
27             final Method keyMethod) {
28         super(prototype, keyMethod);
29     }
30
31     @Override
32     public D deserialize(final NormalizedNode node) {
33         if (node instanceof MapEntryNode) {
34             return createBindingProxy((MapEntryNode) node);
35         } else if (node instanceof UnkeyedListEntryNode) {
36             return createBindingProxy((UnkeyedListEntryNode) node);
37         } else {
38             throw new IllegalStateException("Unsupported data type " + node.getClass());
39         }
40     }
41
42     @Override
43     protected Object deserializeObject(final NormalizedNode node) {
44         if (node instanceof MapNode) {
45             return fromMap((MapNode) node);
46         } else if (node instanceof MapEntryNode) {
47             return createBindingProxy((MapEntryNode) node);
48         } else if (node instanceof UnkeyedListNode) {
49             return fromUnkeyedList((UnkeyedListNode) node);
50         } else if (node instanceof UnkeyedListEntryNode) {
51             return createBindingProxy((UnkeyedListEntryNode) node);
52         } else {
53             throw new IllegalStateException("Unsupported data type " + node.getClass());
54         }
55     }
56
57     @NonNull Object fromMap(final MapNode map, final int size) {
58         return LazyBindingList.create(this, size, map.body());
59     }
60
61     private Object fromMap(final MapNode map) {
62         final int size;
63         // This should never happen, but we do need to ensure users never see an empty Map
64         return (size = map.size()) == 0 ? null : fromMap(map, size);
65     }
66
67     private List<D> fromUnkeyedList(final UnkeyedListNode node) {
68         final int size;
69         // This should never happen, but we do need to ensure users never see an empty List
70         return (size = node.size()) == 0 ? null : LazyBindingList.create(this, size, node.body());
71     }
72 }