Do not pretty-print body class
[yangtools.git] / data / 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, final PathArgument key) {
34         final Object value = map.get(key);
35         return value == null ? null : decodeChild(key, value);
36     }
37
38     public static void putChild(final Map<PathArgument, Object> map, final DataContainerChild child) {
39         final DataContainerChild node = requireNonNull(child);
40         map.put(node.getIdentifier(), encodeExpendableChild(node));
41     }
42
43     static @NonNull LeafNode<?> coerceLeaf(final PathArgument key, final Object value) {
44         verify(key instanceof NodeIdentifier, "Unexpected value %s for child %s", value, key);
45         return ImmutableNodes.leafNode((NodeIdentifier) key, value);
46     }
47
48     private static @Nullable DataContainerChild decodeChild(final PathArgument key, final @NonNull Object value) {
49         return decodeExpendableChild(key, value);
50     }
51
52     private static @NonNull DataContainerChild decodeExpendableChild(final PathArgument key,
53             final @NonNull Object value) {
54         return value instanceof DataContainerChild ? (DataContainerChild) value : coerceLeaf(key, value);
55     }
56
57     private static @NonNull Object encodeExpendableChild(final @NonNull DataContainerChild node) {
58         return node instanceof LeafNode ? verifyEncode(((LeafNode<?>) node).body()) : node;
59     }
60
61     private static @NonNull Object verifyEncode(final @NonNull Object value) {
62         verify(!(value instanceof DataContainerChild), "Unexpected leaf value %s", value);
63         return value;
64     }
65 }