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.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;
19 final class LeafSetNodeCodecContext extends ValueNodeCodecContext.WithCodec {
20 private final IntFunction<ImmutableCollection.Builder<Object>> builderFactory;
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;
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()));
40 return builder.build();