8ab953f844831c43cd109183f534862934262a3a
[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 com.google.common.collect.ImmutableList;
11 import com.google.common.collect.ImmutableList.Builder;
12 import java.util.Collection;
13 import org.opendaylight.yangtools.concepts.IllegalArgumentCodec;
14 import org.opendaylight.yangtools.yang.data.api.schema.LeafSetEntryNode;
15 import org.opendaylight.yangtools.yang.data.api.schema.LeafSetNode;
16 import org.opendaylight.yangtools.yang.data.api.schema.NormalizedNode;
17 import org.opendaylight.yangtools.yang.model.api.LeafListSchemaNode;
18
19 final class LeafSetNodeCodecContext extends ValueNodeCodecContext.WithCodec {
20     LeafSetNodeCodecContext(final LeafListSchemaNode schema, final IllegalArgumentCodec<Object, Object> codec,
21         final String getterName) {
22         // FIXME: add support for defaults
23         super(schema, codec, getterName, null);
24     }
25
26     @Override
27     protected Object deserializeObject(final NormalizedNode normalizedNode) {
28         if (normalizedNode instanceof LeafSetNode<?>) {
29             @SuppressWarnings("unchecked")
30             final Collection<LeafSetEntryNode<Object>> domValues = ((LeafSetNode<Object>) normalizedNode).body();
31             final IllegalArgumentCodec<Object, Object> codec = getValueCodec();
32             final Builder<Object> builder = ImmutableList.builderWithExpectedSize(domValues.size());
33             for (final LeafSetEntryNode<Object> valueNode : domValues) {
34                 builder.add(codec.deserialize(valueNode.body()));
35             }
36             return builder.build();
37         }
38         return null;
39     }
40 }