Merge branch 'master' of ../controller
[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.concepts.AbstractIdentifiable;
20 import org.opendaylight.yangtools.rfc7952.data.api.NormalizedMetadata;
21 import org.opendaylight.yangtools.yang.common.QName;
22 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.PathArgument;
23
24 /**
25  * Immutable implementation of {@link NormalizedMetadata}.
26  */
27 @Beta
28 public class ImmutableNormalizedMetadata extends AbstractIdentifiable<PathArgument> implements NormalizedMetadata {
29     private static final class Container extends ImmutableNormalizedMetadata {
30         private final @NonNull ImmutableMap<PathArgument, NormalizedMetadata> children;
31
32         Container(final PathArgument identifier, final Map<QName, Object> annotations,
33                 final Map<PathArgument, ImmutableNormalizedMetadata> children) {
34             super(identifier, annotations);
35             this.children = ImmutableMap.copyOf(children);
36         }
37
38         @Override
39         public ImmutableMap<PathArgument, NormalizedMetadata> getChildren() {
40             return children;
41         }
42     }
43
44     private final @NonNull ImmutableMap<QName, Object> annotations;
45
46     ImmutableNormalizedMetadata(final @NonNull PathArgument identifier, final Map<QName, Object> annotations) {
47         super(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 ImmutableMap<QName, Object> getAnnotations() {
62         return annotations;
63     }
64
65     /**
66      * {@link org.opendaylight.yangtools.concepts.Builder} of {@link ImmutableNormalizedMetadata} instances.
67      */
68     public static final class Builder
69             implements org.opendaylight.yangtools.concepts.Builder<ImmutableNormalizedMetadata> {
70         private final Map<PathArgument, ImmutableNormalizedMetadata> children = new HashMap<>();
71         private final Map<QName, Object> annotations = new HashMap<>();
72         private PathArgument identifier;
73
74         Builder() {
75             // Hidden to prevent instantiation
76         }
77
78         @SuppressWarnings("checkstyle:hiddenField")
79         public Builder withIdentifier(final PathArgument identifier) {
80             this.identifier = requireNonNull(identifier);
81             return this;
82         }
83
84         public Builder withAnnotation(final QName type, final Object value) {
85             annotations.put(requireNonNull(type, "type"), requireNonNull(value, "value"));
86             return this;
87         }
88
89         @SuppressWarnings("checkstyle:hiddenField")
90         public Builder withAnnotations(final Map<QName, Object> annotations) {
91             annotations.forEach(this::withAnnotation);
92             return this;
93         }
94
95         public Builder withChild(final ImmutableNormalizedMetadata child) {
96             children.put(child.getIdentifier(), child);
97             return this;
98         }
99
100         @SuppressWarnings("checkstyle:hiddenField")
101         public Builder withChildren(final Collection<ImmutableNormalizedMetadata> children) {
102             children.forEach(this::withChild);
103             return this;
104         }
105
106         @Override
107         public ImmutableNormalizedMetadata build() {
108             final PathArgument id = identifier;
109             checkArgument(id != null, "Identifier has not been set");
110             return children.isEmpty() ? new ImmutableNormalizedMetadata(id, annotations)
111                     : new Container(id, annotations, children);
112         }
113     }
114 }