Migrate ImmutableNodes users
[yangtools.git] / data / yang-data-impl / src / main / java / org / opendaylight / yangtools / yang / data / impl / schema / ImmutableNormalizedMetadata.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;
9
10 import static java.util.Objects.requireNonNull;
11
12 import com.google.common.collect.ImmutableMap;
13 import java.util.HashMap;
14 import java.util.Map;
15 import org.eclipse.jdt.annotation.NonNull;
16 import org.opendaylight.yangtools.concepts.Mutable;
17 import org.opendaylight.yangtools.yang.common.QName;
18 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.PathArgument;
19 import org.opendaylight.yangtools.yang.data.api.schema.NormalizedMetadata;
20
21 /**
22  * Immutable implementation of {@link NormalizedMetadata}.
23  */
24 public sealed class ImmutableNormalizedMetadata implements NormalizedMetadata {
25     private static final class Container extends ImmutableNormalizedMetadata {
26         private final @NonNull ImmutableMap<PathArgument, NormalizedMetadata> children;
27
28         Container(final Map<QName, Object> annotations,
29                 final Map<PathArgument, ImmutableNormalizedMetadata> children) {
30             super(annotations);
31             this.children = ImmutableMap.copyOf(children);
32         }
33
34         @Override
35         public ImmutableMap<PathArgument, NormalizedMetadata> getChildren() {
36             return children;
37         }
38     }
39
40     private final @NonNull ImmutableMap<QName, Object> annotations;
41
42     ImmutableNormalizedMetadata(final Map<QName, Object> annotations) {
43         this.annotations = ImmutableMap.copyOf(annotations);
44     }
45
46     /**
47      * Return a new {@link Builder}.
48      *
49      * @return A new Builder.
50      */
51     public static final @NonNull Builder builder() {
52         return new Builder();
53     }
54
55     @Override
56     public final ImmutableMap<QName, Object> getAnnotations() {
57         return annotations;
58     }
59
60     /**
61      * A Builder of {@link ImmutableNormalizedMetadata} instances.
62      */
63     public static final class Builder implements Mutable {
64         private final Map<PathArgument, ImmutableNormalizedMetadata> children = new HashMap<>();
65         private final Map<QName, Object> annotations = new HashMap<>();
66
67         Builder() {
68             // Hidden to prevent instantiation
69         }
70
71         public @NonNull Builder withAnnotation(final QName type, final Object value) {
72             annotations.put(requireNonNull(type, "type"), requireNonNull(value, "value"));
73             return this;
74         }
75
76         @SuppressWarnings("checkstyle:hiddenField")
77         public @NonNull Builder withAnnotations(final Map<QName, Object> annotations) {
78             annotations.forEach(this::withAnnotation);
79             return this;
80         }
81
82         public @NonNull Builder withChild(final PathArgument pathArgument, final ImmutableNormalizedMetadata child) {
83             children.put(requireNonNull(pathArgument, "pathArgument"), requireNonNull(child, "child"));
84             return this;
85         }
86
87         @SuppressWarnings("checkstyle:hiddenField")
88         public @NonNull Builder withChildren(final Map<PathArgument, ImmutableNormalizedMetadata> children) {
89             children.forEach(this::withChild);
90             return this;
91         }
92
93         /**
94          * Return an {@link ImmutableNormalizedMetadata} view of this builder's state.
95          *
96          * @return An ImmutableNormalizedMetadata instace
97          * @throws IllegalStateException if this builder does not have enough state
98          */
99         public @NonNull ImmutableNormalizedMetadata build() {
100             return children.isEmpty() ? new ImmutableNormalizedMetadata(annotations)
101                 : new Container(annotations, children);
102         }
103     }
104 }