Expose all child annotations
[yangtools.git] / yang / rfc7952-data-util / src / main / java / org / opendaylight / yangtools / rfc7952 / data / util / 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.rfc7952.data.util;
9
10 import static com.google.common.base.Preconditions.checkArgument;
11 import static java.util.Objects.requireNonNull;
12
13 import com.google.common.annotations.Beta;
14 import com.google.common.collect.ImmutableMap;
15 import java.util.Collection;
16 import java.util.HashMap;
17 import java.util.Map;
18 import org.eclipse.jdt.annotation.NonNull;
19 import org.opendaylight.yangtools.rfc7952.data.api.NormalizedMetadata;
20 import org.opendaylight.yangtools.yang.common.QName;
21 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.PathArgument;
22
23 /**
24  * Immutable implementation of {@link NormalizedMetadata}.
25  */
26 @Beta
27 public class ImmutableNormalizedMetadata implements NormalizedMetadata {
28     private static final class Container extends ImmutableNormalizedMetadata {
29         private final ImmutableMap<PathArgument, NormalizedMetadata> children;
30
31         Container(final PathArgument identifier, final Map<QName, Object> annotations,
32                 final Map<PathArgument, ImmutableNormalizedMetadata> children) {
33             super(identifier, annotations);
34             this.children = ImmutableMap.copyOf(children);
35         }
36
37         @Override
38         public ImmutableMap<PathArgument, NormalizedMetadata> getChildren() {
39             return children;
40         }
41     }
42
43     private final @NonNull PathArgument identifier;
44     private final @NonNull ImmutableMap<QName, Object> annotations;
45
46     ImmutableNormalizedMetadata(final PathArgument identifier, final Map<QName, Object> annotations) {
47         this.identifier = requireNonNull(identifier);
48         this.annotations = ImmutableMap.copyOf(annotations);
49     }
50
51     /**
52      * Return a new {@link Builder}.
53      *
54      * @return A new Builder.
55      */
56     public static final @NonNull Builder builder() {
57         return new Builder();
58     }
59
60     @Override
61     public final PathArgument getIdentifier() {
62         return identifier;
63     }
64
65     @Override
66     public final ImmutableMap<QName, Object> getAnnotations() {
67         return annotations;
68     }
69
70     /**
71      * {@link org.opendaylight.yangtools.concepts.Builder} of {@link ImmutableNormalizedMetadata} instances.
72      */
73     public static final class Builder
74             implements org.opendaylight.yangtools.concepts.Builder<ImmutableNormalizedMetadata> {
75         private final Map<PathArgument, ImmutableNormalizedMetadata> children = new HashMap<>();
76         private final Map<QName, Object> annotations = new HashMap<>();
77         private PathArgument identifier;
78
79         Builder() {
80             // Hidden to prevent instantiation
81         }
82
83         @SuppressWarnings("checkstyle:hiddenField")
84         public Builder withIdentifier(final PathArgument identifier) {
85             this.identifier = requireNonNull(identifier);
86             return this;
87         }
88
89         public Builder withAnnotation(final QName type, final Object value) {
90             annotations.put(requireNonNull(type, "type"), requireNonNull(value, "value"));
91             return this;
92         }
93
94         @SuppressWarnings("checkstyle:hiddenField")
95         public Builder withAnnotations(final Map<QName, Object> annotations) {
96             annotations.forEach(this::withAnnotation);
97             return this;
98         }
99
100         public Builder withChild(final ImmutableNormalizedMetadata child) {
101             children.put(child.getIdentifier(), child);
102             return this;
103         }
104
105         @SuppressWarnings("checkstyle:hiddenField")
106         public Builder withChildren(final Collection<ImmutableNormalizedMetadata> children) {
107             children.forEach(this::withChild);
108             return this;
109         }
110
111         @Override
112         public ImmutableNormalizedMetadata build() {
113             final PathArgument id = identifier;
114             checkArgument(id != null, "Identifier has not been set");
115             return children.isEmpty() ? new ImmutableNormalizedMetadata(id, annotations)
116                     : new Container(id, annotations, children);
117         }
118     }
119 }