Ignore empty augmentations at runtime
[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.ImmutableCollection;
11 import com.google.common.collect.ImmutableList;
12 import com.google.common.collect.ImmutableSet;
13 import java.util.function.IntFunction;
14 import org.opendaylight.yangtools.concepts.IllegalArgumentCodec;
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     private final IntFunction<ImmutableCollection.Builder<Object>> builderFactory;
21
22     LeafSetNodeCodecContext(final LeafListSchemaNode schema, final IllegalArgumentCodec<Object, Object> codec,
23             final String getterName) {
24         // FIXME: add support for defaults
25         super(schema, codec, getterName, null);
26         builderFactory = schema.isUserOrdered() ? ImmutableList::builderWithExpectedSize
27             : ImmutableSet::builderWithExpectedSize;
28     }
29
30     @Override
31     protected ImmutableCollection<?> deserializeObject(final NormalizedNode normalizedNode) {
32         if (normalizedNode instanceof LeafSetNode<?>) {
33             @SuppressWarnings("unchecked")
34             final var domValues = ((LeafSetNode<Object>) normalizedNode).body();
35             final var codec = getValueCodec();
36             final var builder = builderFactory.apply(domValues.size());
37             for (var valueNode : domValues) {
38                 builder.add(codec.deserialize(valueNode.body()));
39             }
40             return builder.build();
41         }
42         return null;
43     }
44 }