2 * Copyright (c) 2019 PANTHEON.tech, s.r.o. and others. All rights reserved.
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
8 package org.opendaylight.mdsal.binding.dom.codec.impl;
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;
18 final class LeafSetNodeCodecContext extends ValueNodeCodecContext.WithCodec {
19 private final IntFunction<ImmutableCollection.Builder<Object>> builderFactory;
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;
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()));
39 return builder.build();