Split LeafNodeCodeContext and LeafSetNodeCodecContext
[mdsal.git] / binding / mdsal-binding-dom-codec / src / main / java / org / opendaylight / mdsal / binding / dom / codec / impl / LeafSetNodeCodecContext.java
1 /*
2  * Copyright (c) 2019 PANTHEON.tech, s.r.o. 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.ArrayList;
12 import java.util.Collection;
13 import java.util.List;
14 import org.opendaylight.yangtools.concepts.Codec;
15 import org.opendaylight.yangtools.yang.data.api.schema.LeafSetEntryNode;
16 import org.opendaylight.yangtools.yang.data.api.schema.LeafSetNode;
17 import org.opendaylight.yangtools.yang.data.api.schema.NormalizedNode;
18 import org.opendaylight.yangtools.yang.model.api.LeafListSchemaNode;
19
20 final class LeafSetNodeCodecContext extends ValueNodeCodecContext {
21     LeafSetNodeCodecContext(final LeafListSchemaNode schema, final Codec<Object, Object> codec,
22         final Method getter) {
23         // FIXME: add support for defaults
24         super(schema, codec, getter, null);
25     }
26
27     @Override
28     protected Object deserializeObject(final NormalizedNode<?, ?> normalizedNode) {
29         if (normalizedNode instanceof LeafSetNode<?>) {
30             @SuppressWarnings("unchecked")
31             final Collection<LeafSetEntryNode<Object>> domValues = ((LeafSetNode<Object>) normalizedNode).getValue();
32             final List<Object> result = new ArrayList<>(domValues.size());
33             for (final LeafSetEntryNode<Object> valueNode : domValues) {
34                 result.add(getValueCodec().deserialize(valueNode.getValue()));
35             }
36             return result;
37         }
38         return null;
39     }
40 }