Bump versions to 14.0.0-SNAPSHOT
[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.yang.data.api.schema.LeafSetNode;
15 import org.opendaylight.yangtools.yang.data.api.schema.NormalizedNode;
16 import org.opendaylight.yangtools.yang.model.api.LeafListSchemaNode;
17
18 final class LeafSetNodeCodecContext extends ValueNodeCodecContext.WithCodec {
19     private final IntFunction<ImmutableCollection.Builder<Object>> builderFactory;
20
21     LeafSetNodeCodecContext(final LeafListSchemaNode schema, final ValueCodec<Object, Object> codec,
22             final String getterName) {
23         // FIXME: add support for defaults
24         super(schema, codec, getterName, null);
25         builderFactory = schema.isUserOrdered() ? ImmutableList::builderWithExpectedSize
26             : ImmutableSet::builderWithExpectedSize;
27     }
28
29     @Override
30     protected ImmutableCollection<?> deserializeObject(final NormalizedNode normalizedNode) {
31         if (normalizedNode instanceof LeafSetNode<?>) {
32             @SuppressWarnings("unchecked")
33             final var domValues = ((LeafSetNode<Object>) normalizedNode).body();
34             final var codec = getValueCodec();
35             final var builder = builderFactory.apply(domValues.size());
36             for (var valueNode : domValues) {
37                 builder.add(codec.deserialize(valueNode.body()));
38             }
39             return builder.build();
40         }
41         return null;
42     }
43 }