a8668def4e69129e58d7333b677769940fb20618
[yangtools.git] / yang / yang-data-impl / src / main / java / org / opendaylight / yangtools / yang / data / impl / schema / nodes / LazyLeafOperations.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.yangtools.yang.data.impl.schema.nodes;
9
10 import static com.google.common.base.Verify.verify;
11 import static java.util.Objects.requireNonNull;
12
13 import com.google.common.annotations.Beta;
14 import java.util.Map;
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;
22
23 /**
24  * Support utilities for dealing with Maps which would normally hold {@link DataContainerChild} values, but are modified
25  * to eliminate {@link LeafNode} instances.
26  */
27 @Beta
28 public final class LazyLeafOperations {
29     private LazyLeafOperations() {
30         // Hidden on purpose
31     }
32
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);
37     }
38
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));
42     }
43
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);
47     }
48
49     private static @Nullable DataContainerChild<?, ?> decodeChild(final PathArgument key, final @NonNull Object value) {
50         return decodeExpendableChild(key, value);
51     }
52
53     private static @NonNull DataContainerChild<?, ?> decodeExpendableChild(final PathArgument key,
54             @NonNull final Object value) {
55         return value instanceof DataContainerChild ? (DataContainerChild<?, ?>) value : coerceLeaf(key, value);
56     }
57
58     private static @NonNull Object encodeExpendableChild(final @NonNull DataContainerChild<?, ?> node) {
59         return node instanceof LeafNode ? verifyEncode(((LeafNode<?>) node).getValue()) : node;
60     }
61
62     private static @NonNull Object verifyEncode(final @NonNull Object value) {
63         verify(!(value instanceof DataContainerChild), "Unexpected leaf value %s", value);
64         return value;
65     }
66 }