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.yangtools.yang.data.impl.schema.nodes;
10 import static com.google.common.base.Verify.verify;
11 import static java.util.Objects.requireNonNull;
13 import com.google.common.annotations.Beta;
15 import org.eclipse.jdt.annotation.NonNull;
16 import org.eclipse.jdt.annotation.Nullable;
17 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.NodeIdentifier;
18 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.PathArgument;
19 import org.opendaylight.yangtools.yang.data.api.schema.DataContainerChild;
20 import org.opendaylight.yangtools.yang.data.api.schema.LeafNode;
21 import org.opendaylight.yangtools.yang.data.impl.schema.ImmutableNodes;
24 * Support utilities for dealing with Maps which would normally hold {@link DataContainerChild} values, but are modified
25 * to eliminate {@link LeafNode} instances.
28 public final class LazyLeafOperations {
29 private LazyLeafOperations() {
33 public static @Nullable DataContainerChild<?, ?> getChild(final Map<PathArgument, Object> map,
34 final PathArgument key) {
35 final Object value = map.get(key);
36 return value == null ? null : decodeChild(key, value);
39 public static void putChild(final Map<PathArgument, Object> map, final DataContainerChild<?, ?> child) {
40 final DataContainerChild<?, ?> node = requireNonNull(child);
41 map.put(node.getIdentifier(), encodeExpendableChild(node));
44 static @NonNull LeafNode<?> coerceLeaf(final PathArgument key, final Object value) {
45 verify(key instanceof NodeIdentifier, "Unexpected value %s for child %s", value, key);
46 return ImmutableNodes.leafNode((NodeIdentifier) key, value);
49 private static @Nullable DataContainerChild<?, ?> decodeChild(final PathArgument key, final @NonNull Object value) {
50 return decodeExpendableChild(key, value);
53 private static @NonNull DataContainerChild<?, ?> decodeExpendableChild(final PathArgument key,
54 @NonNull final Object value) {
55 return value instanceof DataContainerChild ? (DataContainerChild<?, ?>) value : coerceLeaf(key, value);
58 private static @NonNull Object encodeExpendableChild(final @NonNull DataContainerChild<?, ?> node) {
59 return node instanceof LeafNode ? verifyEncode(((LeafNode<?>) node).getValue()) : node;
62 private static @NonNull Object verifyEncode(final @NonNull Object value) {
63 verify(!(value instanceof DataContainerChild), "Unexpected leaf value %s", value);